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 4, 2024
1 parent 6d62ac2 commit 3965883
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub use python_version::PythonVersion;
pub use semantic_model::{HasTy, SemanticModel};

pub mod ast_node_ref;
mod stdlib;
mod db;
mod module_name;
mod module_resolver;
Expand All @@ -20,6 +19,7 @@ mod python_version;
pub mod semantic_index;
mod semantic_model;
pub(crate) mod site_packages;
mod stdlib;
pub mod types;

type FxOrderSet<V> = ordermap::set::OrderSet<V, BuildHasherDefault<FxHasher>>;
Expand Down
39 changes: 39 additions & 0 deletions crates/red_knot_python_semantic/src/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,47 @@ use crate::module_name::ModuleName;
use crate::module_resolver::resolve_module;
use crate::semantic_index::global_scope;
use crate::semantic_index::symbol::ScopeId;
use crate::types::{symbol_ty_by_name, Type};
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
42 changes: 2 additions & 40 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 Expand Up @@ -632,7 +594,7 @@ impl<'db> UnionType<'db> {
self.elements(db)
.into_iter()
.fold(UnionBuilder::new(db), |builder, element| {
builder.add(transform_fn(&element))
builder.add(transform_fn(element))
})
.build()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use ruff_db::parsed::parsed_module;
use ruff_python_ast::{self as ast, AnyNodeRef, ExprContext, UnaryOp};
use ruff_text_size::Ranged;

use crate::stdlib::builtins_scope;
use crate::module_name::ModuleName;
use crate::module_resolver::{file_to_module, resolve_module};
use crate::semantic_index::ast_ids::{HasScopedAstId, HasScopedUseId, ScopedExpressionId};
Expand All @@ -46,6 +45,7 @@ use crate::semantic_index::expression::Expression;
use crate::semantic_index::semantic_index;
use crate::semantic_index::symbol::{NodeWithScopeKind, NodeWithScopeRef, ScopeId};
use crate::semantic_index::SemanticIndex;
use crate::stdlib::builtins_scope;
use crate::types::diagnostic::{TypeCheckDiagnostic, TypeCheckDiagnostics};
use crate::types::{
builtins_symbol_ty_by_name, definitions_ty, global_symbol_ty_by_name, symbol_ty,
Expand Down Expand Up @@ -2378,13 +2378,13 @@ mod tests {
use ruff_db::testing::assert_function_query_was_not_run;
use ruff_python_ast::name::Name;

use crate::stdlib::builtins_scope;
use crate::db::tests::TestDb;
use crate::program::{Program, SearchPathSettings};
use crate::python_version::PythonVersion;
use crate::semantic_index::definition::Definition;
use crate::semantic_index::symbol::FileScopeId;
use crate::semantic_index::{global_scope, semantic_index, symbol_table, use_def_map};
use crate::stdlib::builtins_scope;
use crate::types::{global_symbol_ty_by_name, infer_definition_types, symbol_ty_by_name};
use crate::{HasTy, ProgramSettings, SemanticModel};

Expand Down

0 comments on commit 3965883

Please sign in to comment.