Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions crates/ty_python_semantic/resources/mdtest/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -901,24 +901,21 @@ reveal_type(Derived().redeclared_with_wider_type) # revealed: str | int | None
reveal_type(Derived.overwritten_in_subclass_body) # revealed: Unknown | None
reveal_type(Derived().overwritten_in_subclass_body) # revealed: Unknown | None | str

# TODO: Both of these should be `str`
reveal_type(Derived.overwritten_in_subclass_method) # revealed: str
reveal_type(Derived().overwritten_in_subclass_method) # revealed: str | Unknown | None
reveal_type(Derived().overwritten_in_subclass_method) # revealed: str

reveal_type(Derived().pure_attribute) # revealed: str | None

# TODO: This should be `str`
reveal_type(Derived().pure_overwritten_in_subclass_body) # revealed: Unknown | None | str

# TODO: This should be `str`
reveal_type(Derived().pure_overwritten_in_subclass_method) # revealed: Unknown | None
reveal_type(Derived().pure_overwritten_in_subclass_method) # revealed: str

# TODO: Both of these should be `Unknown | Literal["intermediate", "base"]`
reveal_type(Derived.undeclared) # revealed: Unknown | Literal["intermediate"]
reveal_type(Derived().undeclared) # revealed: Unknown | Literal["intermediate"]

# TODO: This should be `Unknown | Literal["intermediate", "base"]`
reveal_type(Derived().pure_undeclared) # revealed: Unknown | Literal["intermediate"]
reveal_type(Derived().pure_undeclared) # revealed: Unknown | Literal["intermediate", "base"]
```

## Accessing attributes on class objects
Expand Down
1 change: 1 addition & 0 deletions crates/ty_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod db;
mod dunder_all;
pub mod lint;
pub(crate) mod list;
mod member;
mod module_name;
mod module_resolver;
mod node_key;
Expand Down
73 changes: 73 additions & 0 deletions crates/ty_python_semantic/src/member.rs
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> {
Copy link
Contributor Author

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_declared to PlaceAndQualifiers somehow, 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 to Member soon (or use bitflags) in order to convey even more metadata (e.g. is this an implicit instance attribute? is the attribute a data/non-data descriptor?).

Copy link
Contributor

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 Boundness part of Place currently 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 of Place?

Copy link
Contributor Author

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.

/// 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,
}
}
}
17 changes: 7 additions & 10 deletions crates/ty_python_semantic/src/place.rs
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};
Expand Down Expand Up @@ -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> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could think about moving this to the newly created member module...

place_table(db, scope)
.symbol_id(name)
.map(|symbol_id| {
Expand All @@ -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 {
Expand All @@ -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()
Expand Down
Loading
Loading