-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Prefer declared base class attribute over inferred attribute on subclass #20764
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| use crate::{ | ||
| place::{Place, PlaceAndQualifiers}, | ||
| types::Type, | ||
| }; | ||
|
|
||
| /// The return type of certain member-lookup operations. Contains information | ||
| /// about the type, type qualifiers, boundness/declaredness, and additional | ||
| /// metadata (e.g. whether or not the member was declared) | ||
| #[derive(Debug, Clone, PartialEq, Eq, salsa::Update, get_size2::GetSize)] | ||
| pub(crate) struct Member<'db> { | ||
| /// Type, qualifiers, and boundness information of this member | ||
| pub(crate) inner: PlaceAndQualifiers<'db>, | ||
|
|
||
| /// Whether or not this member was explicitly declared (e.g. `attr: int = 1` | ||
| /// on the class body or `self.attr: int = 1` in a class method), or if the | ||
| /// type was inferred (e.g. `attr = 1` on the class body or `self.attr = 1` | ||
| /// in a class method). | ||
| pub(crate) is_declared: bool, | ||
| } | ||
|
|
||
| impl Default for Member<'_> { | ||
| fn default() -> Self { | ||
| Member::inferred(PlaceAndQualifiers::default()) | ||
| } | ||
| } | ||
|
|
||
| impl<'db> Member<'db> { | ||
| /// Create a new [`Member`] whose type was inferred (rather than explicitly declared). | ||
| pub(crate) fn inferred(inner: PlaceAndQualifiers<'db>) -> Self { | ||
| Self { | ||
| inner, | ||
| is_declared: false, | ||
| } | ||
| } | ||
|
|
||
| /// Create a new [`Member`] whose type was explicitly declared (rather than inferred). | ||
| pub(crate) fn declared(inner: PlaceAndQualifiers<'db>) -> Self { | ||
| Self { | ||
| inner, | ||
| is_declared: true, | ||
| } | ||
| } | ||
|
|
||
| /// Create a new [`Member`] whose type was explicitly and definitively declared, i.e. | ||
| /// there is no control flow path in which it might be possibly undeclared. | ||
| pub(crate) fn definitely_declared(ty: Type<'db>) -> Self { | ||
| Self::declared(Place::bound(ty).into()) | ||
| } | ||
|
|
||
| /// Represents the absence of a member. | ||
| pub(crate) fn unbound() -> Self { | ||
| Self::inferred(PlaceAndQualifiers::default()) | ||
| } | ||
|
|
||
| /// Returns `true` if the inner place is unbound (i.e. there is no such member). | ||
| pub(crate) fn is_unbound(&self) -> bool { | ||
| self.inner.place.is_unbound() | ||
| } | ||
|
|
||
| /// Returns the inner type, unless it is definitely unbound. | ||
| pub(crate) fn ignore_possibly_unbound(&self) -> Option<Type<'db>> { | ||
| self.inner.place.ignore_possibly_unbound() | ||
| } | ||
|
|
||
| /// Map a type transformation function over the type of this member. | ||
| #[must_use] | ||
| pub(crate) fn map_type(self, f: impl FnOnce(Type<'db>) -> Type<'db>) -> Self { | ||
| Self { | ||
| inner: self.inner.map_type(f), | ||
| is_declared: self.is_declared, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| use ruff_db::files::File; | ||
|
|
||
| use crate::dunder_all::dunder_all_names; | ||
| use crate::member::Member; | ||
| use crate::module_resolver::{KnownModule, file_to_module}; | ||
| use crate::semantic_index::definition::{Definition, DefinitionState}; | ||
| use crate::semantic_index::place::{PlaceExprRef, ScopedPlaceId}; | ||
|
|
@@ -232,13 +233,9 @@ pub(crate) fn place<'db>( | |
| ) | ||
| } | ||
|
|
||
| /// Infer the public type of a class symbol (its type as seen from outside its scope) in the given | ||
| /// Infer the public type of a class member/symbol (its type as seen from outside its scope) in the given | ||
| /// `scope`. | ||
| pub(crate) fn class_symbol<'db>( | ||
| db: &'db dyn Db, | ||
| scope: ScopeId<'db>, | ||
| name: &str, | ||
| ) -> PlaceAndQualifiers<'db> { | ||
| pub(crate) fn class_member<'db>(db: &'db dyn Db, scope: ScopeId<'db>, name: &str) -> Member<'db> { | ||
|
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. We could think about moving this to the newly created |
||
| place_table(db, scope) | ||
| .symbol_id(name) | ||
| .map(|symbol_id| { | ||
|
|
@@ -252,7 +249,7 @@ pub(crate) fn class_symbol<'db>( | |
|
|
||
| if !place_and_quals.place.is_unbound() && !place_and_quals.is_init_var() { | ||
| // Trust the declared type if we see a class-level declaration | ||
| return place_and_quals; | ||
| return Member::declared(place_and_quals); | ||
| } | ||
|
|
||
| if let PlaceAndQualifiers { | ||
|
|
@@ -267,14 +264,14 @@ pub(crate) fn class_symbol<'db>( | |
|
|
||
| // TODO: we should not need to calculate inferred type second time. This is a temporary | ||
| // solution until the notion of Boundness and Declaredness is split. See #16036, #16264 | ||
| match inferred { | ||
| Member::inferred(match inferred { | ||
| Place::Unbound => Place::Unbound.with_qualifiers(qualifiers), | ||
| Place::Type(_, boundness) => { | ||
| Place::Type(ty, boundness).with_qualifiers(qualifiers) | ||
| } | ||
| } | ||
| }) | ||
| } else { | ||
| Place::Unbound.into() | ||
| Member::unbound() | ||
| } | ||
| }) | ||
| .unwrap_or_default() | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't love the fact that we now have this
Member(PlaceAndQualifiers(Place(Type)))onion structure, but each single layer has its own dedicated purpose and is used in a lot of places.I considered adding
is_declaredtoPlaceAndQualifierssomehow, as this flag might be relevant in other areas as well. This would be a huge refactoring, but more importantly, I think that we probably want to add new flags toMembersoon (or usebitflags) in order to convey even more metadata (e.g. is this an implicit instance attribute? is the attribute a data/non-data descriptor?).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.
This feels like it intersects with discussions we've had a long time ago already (and several TODO comments we have) about how the
Boundnesspart ofPlacecurrently conflates both "boundness" and "declaredness" in a way that isn't always consistent. I think we always vaguely had the intention of cleaning that up, but it never became a priority. it seems like this new flag is providing the same metadata that would be provided by that refactor, but at a new outer layer, mostly in order to avoid the need for a big refactor ofPlace?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.
I opened astral-sh/ty#1341 to track this.