Skip to content

Commit

Permalink
Move things around
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Sep 2, 2024
1 parent b5919c5 commit 9fdeb3e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
39 changes: 39 additions & 0 deletions crates/red_knot_python_semantic/src/stdlib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
use crate::module_name::ModuleName;
use crate::module_resolver::resolve_module;
use crate::types::{Type, symbol_ty_by_name};
use crate::semantic_index::global_scope;
use crate::semantic_index::symbol::ScopeId;
use crate::Db;

/// Enumeration of various core stdlib modules, for which we have dedicated Salsa queries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CoreStdlibModule {
Builtins,
Types,
Typeshed,
}

impl CoreStdlibModule {
/// Retrieve the global scope of the given module.
///
/// Returns `None` if the given module isn't available for some reason.
pub(crate) fn global_scope(self, db: &dyn Db) -> Option<ScopeId<'_>> {
match self {
Self::Builtins => builtins_scope(db),
Self::Types => types_scope(db),
Self::Typeshed => typeshed_scope(db),
}
}

/// Shorthand for `symbol_ty` that looks up a symbol in the scope of a given core module.
///
/// Returns `Unbound` if the given module isn't available for some reason.
pub(crate) fn symbol_ty_by_name<'db>(self, db: &'db dyn Db, name: &str) -> Type<'db> {
self.global_scope(db)
.map(|globals| symbol_ty_by_name(db, globals, name))
.unwrap_or(Type::Unbound)
}
}

/// Shorthand for `symbol_ty` that looks up a symbol in the `builtins` scope.
///
/// Returns `Unbound` if the `builtins` module isn't available for some reason.
#[inline]
pub(crate) fn builtins_symbol_ty_by_name<'db>(db: &'db dyn Db, name: &str) -> Type<'db> {
CoreStdlibModule::Builtins.symbol_ty_by_name(db, name)
}

/// Salsa query to get the builtins scope.
///
/// Can return None if a custom typeshed is used that is missing `builtins.pyi`.
Expand Down
40 changes: 1 addition & 39 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::semantic_index::{
global_scope, semantic_index, symbol_table, use_def_map, DefinitionWithConstraints,
DefinitionWithConstraintsIterator,
};
use crate::stdlib::{builtins_scope, types_scope, typeshed_scope};
use crate::stdlib::{builtins_symbol_ty_by_name, CoreStdlibModule};
use crate::types::narrow::narrowing_constraint;
use crate::{Db, FxOrderSet};

Expand Down Expand Up @@ -75,44 +75,6 @@ pub(crate) fn global_symbol_ty_by_name<'db>(db: &'db dyn Db, file: File, name: &
symbol_ty_by_name(db, global_scope(db, file), name)
}

/// Enumeration of various core stdlib modules, for which we have dedicated Salsa queries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CoreStdlibModule {
Builtins,
Types,
Typeshed,
}

impl CoreStdlibModule {
/// Retrieve the global scope of the given module.
///
/// Returns `None` if the given module isn't available for some reason.
pub(crate) fn global_scope(self, db: &dyn Db) -> Option<ScopeId<'_>> {
match self {
Self::Builtins => builtins_scope(db),
Self::Types => types_scope(db),
Self::Typeshed => typeshed_scope(db),
}
}

/// Shorthand for `symbol_ty` that looks up a symbol in the scope of a given core module.
///
/// Returns `Unbound` if the given module isn't available for some reason.
pub(crate) fn symbol_ty_by_name<'db>(self, db: &'db dyn Db, name: &str) -> Type<'db> {
self.global_scope(db)
.map(|globals| symbol_ty_by_name(db, globals, name))
.unwrap_or(Type::Unbound)
}
}

/// Shorthand for `symbol_ty` that looks up a symbol in the `builtins` scope.
///
/// Returns `Unbound` if the `builtins` module isn't available for some reason.
#[inline]
pub(crate) fn builtins_symbol_ty_by_name<'db>(db: &'db dyn Db, name: &str) -> Type<'db> {
CoreStdlibModule::Builtins.symbol_ty_by_name(db, name)
}

/// Infer the type of a [`Definition`].
pub(crate) fn definition_ty<'db>(db: &'db dyn Db, definition: Definition<'db>) -> Type<'db> {
let inference = infer_definition_types(db, definition);
Expand Down

0 comments on commit 9fdeb3e

Please sign in to comment.