Skip to content

Commit 05160d5

Browse files
[ty] Diagnose invalid descriptor __get__ calls (#27400)
## Summary This PR reports `invalid-attribute-access` when an attribute read implicitly calls a descriptor's `__get__` method with incompatible arguments. ```python class Descriptor: def __get__(self): pass class C: value = Descriptor() C().value # error: `__get__` receives the descriptor, instance, and owner ``` Previously, descriptor lookup recovered the inferred return type from a failed `__get__` call and discarded the call error. The core change here is straightforward, but it ends up being a significant diff because we need to preserve errors that we were previously discarding during attribute lookup, and that happens deep in the stack. Conceptually, we made this change: ```rust // Before member_lookup(...) -> PlaceAndQualifiers // After type MemberLookupResult = Result<PlaceAndQualifiers, MemberLookupError>; member_lookup(...) -> MemberLookupResult ``` The lookup error retains the recovered member type to avoid cascading diagnostics, and is threaded through the various lookup sites. Closes astral-sh/ty#4129.
1 parent baea3d0 commit 05160d5

13 files changed

Lines changed: 1289 additions & 262 deletions

File tree

‎crates/ty_python_semantic/resources/mdtest/descriptor_protocol.md‎

Lines changed: 552 additions & 23 deletions
Large diffs are not rendered by default.

‎crates/ty_python_semantic/resources/mdtest/properties.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,12 @@ class C:
243243
c = C()
244244
c.attr = 1
245245

246-
# TODO: An error should be emitted here.
247-
# See https://github.com/astral-sh/ruff/issues/16298 for more details.
246+
# error: [call-non-callable] "property has no getter"
247+
C.attr.__get__(c, C)
248+
# error: [call-non-callable] "property has no getter"
249+
type(C.attr).__get__(C.attr, c, C)
250+
251+
# error: [invalid-attribute-access] "Cannot read property `attr` on object of type `C` because it has no getter"
248252
reveal_type(c.attr) # revealed: Never
249253
```
250254

‎crates/ty_python_semantic/resources/mdtest/regression/3593_function_known_decorators_cycle.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ from typing import Self, overload, reveal_type
1313
class C:
1414
a: D
1515

16+
# error: [invalid-attribute-access]
1617
C.a
18+
# error: [invalid-attribute-access]
1719
reveal_type(C().a) # revealed: Unknown | D
1820

1921
class D:

‎crates/ty_python_semantic/src/place.rs‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ impl PublicTypePolicy {
103103
}
104104

105105
/// The source definition provenance for a place.
106-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, get_size2::GetSize, salsa::SalsaValue)]
106+
#[derive(
107+
Debug, Clone, Copy, Default, Hash, PartialEq, Eq, get_size2::GetSize, salsa::SalsaValue,
108+
)]
107109
pub(crate) enum Provenance<'db> {
108110
/// No source definition is known.
109111
#[default]
@@ -142,7 +144,7 @@ impl<'db> Provenance<'db> {
142144
}
143145

144146
/// A defined place with its raw type, origin, definedness, public-type policy, and provenance.
145-
#[derive(Debug, Clone, Copy, PartialEq, Eq, get_size2::GetSize, salsa::SalsaValue)]
147+
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, get_size2::GetSize, salsa::SalsaValue)]
146148
pub(crate) struct DefinedPlace<'db> {
147149
pub(crate) ty: Type<'db>,
148150
pub(crate) origin: TypeOrigin,
@@ -221,7 +223,9 @@ impl<'db> DefinedPlace<'db> {
221223
/// bound_or_declared: Place::Defined(DefinedPlace { ty: Literal[1], origin: TypeOrigin::Inferred, definedness: Definedness::PossiblyUndefined, .. }),
222224
/// non_existent: Place::Undefined,
223225
/// ```
224-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, get_size2::GetSize, salsa::SalsaValue)]
226+
#[derive(
227+
Debug, Clone, Copy, Default, Hash, PartialEq, Eq, get_size2::GetSize, salsa::SalsaValue,
228+
)]
225229
pub(crate) enum Place<'db> {
226230
Defined(DefinedPlace<'db>),
227231
#[default]
@@ -363,11 +367,13 @@ impl<'db> Place<'db> {
363367
}),
364368

365369
Place::Defined(defined) => {
366-
if let Some((dunder_get_return_ty, _)) =
367-
defined.ty.try_call_dunder_get(db, env, None, owner)
368-
{
370+
let result = defined
371+
.ty
372+
.try_call_dunder_get(db, env, None, owner)
373+
.unwrap_or_else(|error| Some(error.fallback()));
374+
if let Some(result) = result {
369375
Place::Defined(DefinedPlace {
370-
ty: dunder_get_return_ty,
376+
ty: result.return_type,
371377
provenance: Provenance::Unknown,
372378
..defined
373379
})
@@ -894,7 +900,9 @@ impl<'db> PlaceFromDeclarationsResult<'db> {
894900
/// that this comes with a [`CLASS_VAR`] type qualifier.
895901
///
896902
/// [`CLASS_VAR`]: crate::types::TypeQualifiers::CLASS_VAR
897-
#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, get_size2::GetSize, salsa::SalsaValue)]
903+
#[derive(
904+
Debug, Clone, Default, Copy, Hash, PartialEq, Eq, get_size2::GetSize, salsa::SalsaValue,
905+
)]
898906
pub(crate) struct PlaceAndQualifiers<'db> {
899907
pub(crate) place: Place<'db>,
900908
pub(crate) qualifiers: TypeQualifiers,

0 commit comments

Comments
 (0)