[ty] Fix Self resolution for classes nested within methods#22964
[ty] Fix Self resolution for classes nested within methods#22964charliermarsh merged 5 commits intomainfrom
Self resolution for classes nested within methods#22964Conversation
014829e to
60fb14b
Compare
Typing conformance resultsNo changes detected ✅ |
|
2142a91 to
6d4cfdb
Compare
6d4cfdb to
b5c7ced
Compare
AlexWaygood
left a comment
There was a problem hiding this comment.
thanks, that looks a lot more like what I'd expect here!
| let containing_scope = typevar_binding_context | ||
| .and_then(|def| { | ||
| let DefinitionKind::Function(func_ref) = def.kind(db) else { | ||
| return None; | ||
| }; | ||
| let module = parsed_module(db, file).load(db); | ||
| Some(index.node_scope(NodeWithScopeRef::Function(func_ref.node(&module)))) | ||
| }) | ||
| .unwrap_or_else(|| scope_id.file_scope_id(db)); |
There was a problem hiding this comment.
I think you can do this using higher-level APIs that mean we don't have to access the raw AST (which is always best avoided where possible, as it hurts our incrementality)
diff --git a/crates/ty_python_semantic/src/types/generics.rs b/crates/ty_python_semantic/src/types/generics.rs
index 79802a4fc3..c50b143f06 100644
--- a/crates/ty_python_semantic/src/types/generics.rs
+++ b/crates/ty_python_semantic/src/types/generics.rs
@@ -8,8 +8,8 @@ use ruff_python_ast as ast;
use ruff_python_ast::name::Name;
use rustc_hash::{FxHashMap, FxHashSet};
-use crate::semantic_index::definition::{Definition, DefinitionKind};
-use crate::semantic_index::scope::{FileScopeId, NodeWithScopeKind, NodeWithScopeRef, ScopeId};
+use crate::semantic_index::definition::Definition;
+use crate::semantic_index::scope::{FileScopeId, NodeWithScopeKind, ScopeId};
use crate::semantic_index::{SemanticIndex, semantic_index};
use crate::types::class::ClassType;
use crate::types::class_base::ClassBase;
@@ -27,10 +27,9 @@ use crate::types::{
ClassLiteral, FindLegacyTypeVarsVisitor, IntersectionType, KnownClass, KnownInstanceType,
MaterializationKind, NormalizedVisitor, Type, TypeContext, TypeMapping,
TypeVarBoundOrConstraints, TypeVarIdentity, TypeVarInstance, TypeVarKind, TypeVarVariance,
- UnionType, declaration_type, walk_type_var_bounds,
+ UnionType, binding_type, declaration_type, walk_type_var_bounds,
};
use crate::{Db, FxOrderMap, FxOrderSet};
-use ruff_db::parsed::parsed_module;
/// Returns an iterator of any generic context introduced by the given scope or any enclosing
/// scope.
@@ -163,19 +162,14 @@ pub(crate) fn typing_self<'db>(
//
// and the first match would be (method, Outer) -- wrong.
let containing_scope = typevar_binding_context
- .and_then(|def| {
- let DefinitionKind::Function(func_ref) = def.kind(db) else {
- return None;
- };
- let module = parsed_module(db, file).load(db);
- Some(index.node_scope(NodeWithScopeRef::Function(func_ref.node(&module))))
- })
- .unwrap_or_else(|| scope_id.file_scope_id(db));
+ .and_then(|def| binding_type(db, def).as_function_literal())
+ .map(|function| function.literal(db).last_definition(db).body_scope(db))
+ .unwrap_or(scope_id);
bind_typevar(
db,
index,
- containing_scope,
+ containing_scope.file_scope_id(db),
typevar_binding_context,
typevar,
)There was a problem hiding this comment.
I tried this but it hit me with a perf regression and a panic, so I backed out for now, but happy to revisit if it feels important!
There was a problem hiding this comment.
The .node() call means we probably need to add #[salsa::tracked] to the function it's in, or i think we'll have over-invalidation of Salsa queries, which is bad for our incrementality
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
## Summary See: #22964 (comment).
Summary
Closes astral-sh/ty#2643.