Skip to content

Commit

Permalink
Avoid module lookup for known classes when possible (#14343)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Nov 14, 2024
1 parent a40bc6a commit 24cd592
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
19 changes: 10 additions & 9 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ use itertools::Itertools;
use ruff_db::files::File;
use ruff_python_ast as ast;

pub(crate) use self::builder::{IntersectionBuilder, UnionBuilder};
pub use self::diagnostic::{TypeCheckDiagnostic, TypeCheckDiagnostics};
pub(crate) use self::display::TypeArrayDisplay;
pub(crate) use self::infer::{
infer_deferred_types, infer_definition_types, infer_expression_types, infer_scope_types,
};
use crate::module_resolver::file_to_module;
use crate::semantic_index::ast_ids::HasScopedAstId;
use crate::semantic_index::definition::Definition;
use crate::semantic_index::symbol::{self as symbol, ScopeId, ScopedSymbolId};
Expand All @@ -22,13 +29,6 @@ use crate::types::mro::{ClassBase, Mro, MroError, MroIterator};
use crate::types::narrow::narrowing_constraint;
use crate::{Db, FxOrderSet, Module, Program};

pub(crate) use self::builder::{IntersectionBuilder, UnionBuilder};
pub use self::diagnostic::{TypeCheckDiagnostic, TypeCheckDiagnostics};
pub(crate) use self::display::TypeArrayDisplay;
pub(crate) use self::infer::{
infer_deferred_types, infer_definition_types, infer_expression_types, infer_scope_types,
};

mod builder;
mod diagnostic;
mod display;
Expand Down Expand Up @@ -1720,7 +1720,7 @@ impl<'db> KnownClass {
}
}

pub fn try_from_module(module: &Module, class_name: &str) -> Option<Self> {
pub fn try_from_file(db: &dyn Db, file: File, class_name: &str) -> Option<Self> {
// Note: if this becomes hard to maintain (as rust can't ensure at compile time that all
// variants of `Self` are covered), we might use a macro (in-house or dependency)
// See: https://stackoverflow.com/q/39070244
Expand All @@ -1747,7 +1747,8 @@ impl<'db> KnownClass {
_ => return None,
};

candidate.check_module(module).then_some(candidate)
let module = file_to_module(db, file)?;
candidate.check_module(&module).then_some(candidate)
}

/// Return `true` if the module of `self` matches `module_name`
Expand Down
4 changes: 1 addition & 3 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,7 @@ impl<'db> TypeInferenceBuilder<'db> {
.node_scope(NodeWithScopeRef::Class(class_node))
.to_scope_id(self.db, self.file);

let maybe_known_class = file_to_module(self.db, self.file)
.as_ref()
.and_then(|module| KnownClass::try_from_module(module, name.as_str()));
let maybe_known_class = KnownClass::try_from_file(self.db, self.file, name);

let class = Class::new(self.db, &*name.id, body_scope, maybe_known_class);
let class_ty = Type::class_literal(class);
Expand Down

0 comments on commit 24cd592

Please sign in to comment.