-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Fix attribute access on TypedDicts
#19758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,7 +95,7 @@ impl<'db> AllMembers<'db> { | |
|
|
||
| Type::NominalInstance(instance) => { | ||
| let (class_literal, _specialization) = instance.class.class_literal(db); | ||
| self.extend_with_instance_members(db, class_literal); | ||
| self.extend_with_instance_members(db, ty, class_literal); | ||
| } | ||
|
|
||
| Type::ClassLiteral(class_literal) if class_literal.is_typed_dict(db) => { | ||
|
|
@@ -106,6 +106,10 @@ impl<'db> AllMembers<'db> { | |
| self.extend_with_type(db, KnownClass::TypedDictFallback.to_class_literal(db)); | ||
| } | ||
|
|
||
| Type::SubclassOf(subclass_of_type) if subclass_of_type.is_typed_dict(db) => { | ||
| self.extend_with_type(db, KnownClass::TypedDictFallback.to_class_literal(db)); | ||
| } | ||
|
|
||
| Type::ClassLiteral(class_literal) => { | ||
| self.extend_with_class_members(db, ty, class_literal); | ||
|
|
||
|
|
@@ -168,7 +172,11 @@ impl<'db> AllMembers<'db> { | |
| self.extend_with_class_members(db, ty, class_literal); | ||
| } | ||
|
|
||
| self.extend_with_type(db, KnownClass::TypedDictFallback.to_instance(db)); | ||
| if let Type::ClassLiteral(class) = | ||
| KnownClass::TypedDictFallback.to_class_literal(db) | ||
| { | ||
| self.extend_with_instance_members(db, ty, class); | ||
| } | ||
| } | ||
|
|
||
| Type::ModuleLiteral(literal) => { | ||
|
|
@@ -281,13 +289,17 @@ impl<'db> AllMembers<'db> { | |
| } | ||
| } | ||
|
|
||
| fn extend_with_instance_members(&mut self, db: &'db dyn Db, class_literal: ClassLiteral<'db>) { | ||
| fn extend_with_instance_members( | ||
| &mut self, | ||
| db: &'db dyn Db, | ||
| ty: Type<'db>, | ||
| class_literal: ClassLiteral<'db>, | ||
| ) { | ||
| for parent in class_literal | ||
| .iter_mro(db, None) | ||
| .filter_map(ClassBase::into_class) | ||
| .map(|class| class.class_literal(db).0) | ||
| { | ||
| let parent_instance = Type::instance(db, parent.default_specialization(db)); | ||
| let class_body_scope = parent.body_scope(db); | ||
| let file = class_body_scope.file(db); | ||
| let index = semantic_index(db, file); | ||
|
|
@@ -297,7 +309,7 @@ impl<'db> AllMembers<'db> { | |
| let Some(name) = place_expr.as_instance_attribute() else { | ||
| continue; | ||
| }; | ||
| let result = parent_instance.member(db, name.as_str()); | ||
| let result = ty.member(db, name.as_str()); | ||
| let Some(ty) = result.place.ignore_possibly_unbound() else { | ||
| continue; | ||
| }; | ||
|
|
@@ -314,7 +326,7 @@ impl<'db> AllMembers<'db> { | |
| // member, e.g., `SomeClass.__delattr__` is not a bound | ||
| // method, but `instance_of_SomeClass.__delattr__` is. | ||
| for Member { name, .. } in all_declarations_and_bindings(db, class_body_scope) { | ||
| let result = parent_instance.member(db, name.as_str()); | ||
| let result = ty.member(db, name.as_str()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a small bugfix. Instead of trying to access the member on instances of the parent class, just access them on the type that we're adding completions for — similar to how it's done in the method above. If we don't do this, the descriptor protocol will be invoked with the wrong instance type (which is why we see the changes in bound methods). It was also possible to change this for |
||
| let Some(ty) = result.place.ignore_possibly_unbound() else { | ||
| continue; | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were just (slightly) wrong before. FYI @BurntSushi.