mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:45:24 +00:00
Avoid module lookup for known classes when possible (#14343)
This commit is contained in:
parent
a40bc6a460
commit
24cd592a1d
2 changed files with 11 additions and 12 deletions
|
@ -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};
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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`
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue