Skip to content

Commit 2fc445f

Browse files
[ty] Diagnose invalid __getattr__ calls (#27502)
## Summary Previously, an invalid `__getattr__` method caused us to report that the requested attribute was missing, even though Python actually invokes the method and raises `TypeError`: ```py class Example: def __getattr__(self) -> str: return "fallback" Example().missing # error: [invalid-attribute-access] ``` We now propagate failed implicit `__getattr__` calls through the generalized member-lookup error introduced in #27400, so the diagnostic identifies the invalid call and preserves the method's return type for error recovery. This applies to instance and metaclass attribute lookup, including incompatible attribute-name parameters. Literal-restricted `__getattr__` methods continue to treat unsupported names as missing attributes, and already-defined attributes still bypass the fallback. Closes astral-sh/ty#135.
1 parent 22c7823 commit 2fc445f

3 files changed

Lines changed: 141 additions & 11 deletions

File tree

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

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,6 +2706,51 @@ accessed on the class itself:
27062706
CustomGetAttr.whatever
27072707
```
27082708

2709+
### Invalid `__getattr__` calls
2710+
2711+
If `__getattr__` cannot accept the attribute name that Python passes to it, the access is invalid.
2712+
The method's return type remains available for error recovery, while defined attributes do not
2713+
invoke the fallback.
2714+
2715+
```py
2716+
class InvalidGetAttr:
2717+
defined: bool = True
2718+
2719+
def __getattr__(self) -> str:
2720+
return "fallback"
2721+
2722+
InvalidGetAttr().missing # snapshot: invalid-attribute-access
2723+
2724+
# error: [invalid-attribute-access] "Invalid access to attribute `missing` on type `InvalidGetAttr`"
2725+
reveal_type(InvalidGetAttr().missing) # revealed: str
2726+
reveal_type(InvalidGetAttr().defined) # revealed: bool
2727+
```
2728+
2729+
```snapshot
2730+
error[invalid-attribute-access]: Invalid access to attribute `missing` on type `InvalidGetAttr`
2731+
--> src/mdtest_snippet.py:7:1
2732+
|
2733+
7 | InvalidGetAttr().missing # snapshot: invalid-attribute-access
2734+
| ^^^^^^^^^^^^^^^^^^^^^^^^ Too many positional arguments to bound method `InvalidGetAttr.__getattr__`: expected 1, got 2
2735+
info: This access implicitly calls `__getattr__`
2736+
info: Method signature here
2737+
--> src/mdtest_snippet.py:4:9
2738+
|
2739+
4 | def __getattr__(self) -> str:
2740+
| ^^^^^^^^^^^^^^^^^^^^^^^^
2741+
```
2742+
2743+
An incompatible type for the attribute name is also an invalid fallback call.
2744+
2745+
```py
2746+
class InvalidNameType:
2747+
def __getattr__(self, name: int) -> bytes:
2748+
return b"fallback"
2749+
2750+
# error: [invalid-attribute-access] "Invalid access to attribute `missing` on type `InvalidNameType`"
2751+
reveal_type(InvalidNameType().missing) # revealed: bytes
2752+
```
2753+
27092754
### Type of the `name` parameter
27102755

27112756
If the `name` parameter of the `__getattr__` method is annotated with a (union of) literal type(s),
@@ -2724,8 +2769,8 @@ reveal_type(date.day) # revealed: int
27242769
reveal_type(date.month) # revealed: int
27252770
reveal_type(date.year) # revealed: int
27262771

2727-
# error: [unresolved-attribute] "Object of type `Date` has no attribute `century`"
2728-
reveal_type(date.century) # revealed: Unknown
2772+
# error: [invalid-attribute-access] "Invalid access to attribute `century` on type `Date`"
2773+
reveal_type(date.century) # revealed: int
27292774
```
27302775

27312776
### `argparse.Namespace`
@@ -2817,6 +2862,22 @@ class Foo(metaclass=Meta): ...
28172862
reveal_type(Foo.whatever) # revealed: int
28182863
```
28192864

2865+
### Invalid `__getattr__` calls
2866+
2867+
Invalid metaclass `__getattr__` calls are reported on class attribute access while preserving the
2868+
method's return type for error recovery.
2869+
2870+
```py
2871+
class Meta(type):
2872+
def __getattr__(cls) -> int:
2873+
return 1
2874+
2875+
class Foo(metaclass=Meta): ...
2876+
2877+
# error: [invalid-attribute-access] "Invalid access to attribute `missing` on type `<class 'Foo'>`"
2878+
reveal_type(Foo.missing) # revealed: int
2879+
```
2880+
28202881
### Class attributes take precedence
28212882

28222883
If the class defines the attribute directly, it takes precedence over the metaclass `__getattr__`:

‎crates/ty_python_semantic/src/types.rs‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ pub(crate) use crate::types::callable::{CallableType, CallableTypes};
6666
pub(crate) use crate::types::class_base::ClassBase;
6767
use crate::types::constraints::ConstraintSetBuilder;
6868
use crate::types::context::{LintDiagnosticGuard, LintDiagnosticGuardBuilder};
69-
use crate::types::diagnostic::{INVALID_AWAIT, INVALID_TYPE_FORM, report_bad_dunder_get_call};
69+
use crate::types::diagnostic::{
70+
INVALID_AWAIT, INVALID_TYPE_FORM, report_bad_dunder_get_call, report_bad_dunder_getattr_call,
71+
};
7072
pub use crate::types::display::{DisplaySettings, TypeDetail, TypeDisplayDetails};
7173
pub(crate) use crate::types::enums::{EnumClassLiteral, EnumComplementType, enum_metadata};
7274
pub(crate) use crate::types::equality::{ComparisonSoundnessPolicy, equality_truthiness};
@@ -609,6 +611,14 @@ fn descriptor_get_result<'db>(
609611
#[derive(Clone, Debug, Copy, Hash, PartialEq, Eq, get_size2::GetSize, salsa::SalsaValue)]
610612
enum MemberLookupErrorKind<'db> {
611613
DescriptorGet(DescriptorGetCallContext<'db>),
614+
615+
/// An invalid fallback call, represented by its receiver and requested attribute name.
616+
///
617+
/// Retaining only these arguments avoids storing call bindings in cached lookup results.
618+
GetAttr {
619+
receiver: Type<'db>,
620+
name: Type<'db>,
621+
},
612622
}
613623

614624
/// A failed member lookup together with the member used to recover from the error.
@@ -652,7 +662,21 @@ impl<'db> MemberLookupError<'db> {
652662
target,
653663
);
654664
}
655-
MemberLookupErrorKind::DescriptorGet(_) => {}
665+
MemberLookupErrorKind::GetAttr { receiver, name }
666+
if assigned_type.is_none()
667+
&& let Err(CallDunderError::CallError(kind, bindings, _)) = receiver
668+
.try_call_dunder(
669+
db,
670+
env,
671+
"__getattr__",
672+
CallArguments::positional([name]),
673+
TypeContext::default(),
674+
) =>
675+
{
676+
let failure = CallError(kind, bindings);
677+
report_bad_dunder_getattr_call(context, &failure, object_type, target);
678+
}
679+
MemberLookupErrorKind::DescriptorGet(_) | MemberLookupErrorKind::GetAttr { .. } => {}
656680
}
657681
}
658682
}
@@ -6528,17 +6552,27 @@ impl<'db> Type<'db> {
65286552
return MemberLookupResult::from(Place::Undefined);
65296553
}
65306554

6531-
self.try_call_dunder(
6555+
let name_type = Type::string_literal(db, name);
6556+
match self.try_call_dunder(
65326557
db,
65336558
env,
65346559
"__getattr__",
6535-
CallArguments::positional([Type::string_literal(db, name)]),
6560+
CallArguments::positional([name_type]),
65366561
TypeContext::default(),
6537-
)
6538-
.map(|outcome| Place::bound(outcome.return_type(db, env)))
6539-
// TODO: Handle call errors here.
6540-
.unwrap_or_default()
6541-
.into()
6562+
) {
6563+
Ok(outcome) => Place::bound(outcome.return_type(db, env)).into(),
6564+
Err(CallDunderError::CallError(_, bindings, _)) => member_lookup_result(
6565+
db,
6566+
Place::bound(bindings.return_type(db, env)).into(),
6567+
Some(MemberLookupErrorKind::GetAttr {
6568+
receiver: self,
6569+
name: name_type,
6570+
}),
6571+
),
6572+
Err(
6573+
CallDunderError::PossiblyUnbound { .. } | CallDunderError::MethodNotAvailable,
6574+
) => Place::Undefined.into(),
6575+
}
65426576
};
65436577

65446578
let custom_getattribute = OnceCell::new();

‎crates/ty_python_semantic/src/types/diagnostic.rs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,41 @@ pub(super) fn report_bad_dunder_get_call<'db>(
17951795
}
17961796
}
17971797

1798+
/// Reports an invalid implicit `__getattr__` call at the original attribute access.
1799+
///
1800+
/// ```python
1801+
/// class C:
1802+
/// def __getattr__(self) -> int: ...
1803+
///
1804+
/// C().missing # Invalid: Python passes the attribute name to __getattr__.
1805+
/// ```
1806+
///
1807+
/// Preserves the underlying call diagnostic and explains why attribute access invoked the method.
1808+
pub(super) fn report_bad_dunder_getattr_call<'db>(
1809+
context: &InferContext<'db, '_>,
1810+
failure: &CallError<'db>,
1811+
object_type: Type<'db>,
1812+
target: &ast::ExprAttribute,
1813+
) {
1814+
let db = context.db();
1815+
let env = &context.program_environment();
1816+
let attribute = target.attr.as_str();
1817+
1818+
failure.report_diagnostics_with_override(
1819+
context,
1820+
target.into(),
1821+
&CallDiagnosticOverride {
1822+
lint: &INVALID_ATTRIBUTE_ACCESS,
1823+
message: format!(
1824+
"Invalid access to attribute `{attribute}` on type `{}`",
1825+
object_type.display(db, env),
1826+
),
1827+
info: "This access implicitly calls `__getattr__`",
1828+
argument_ranges: &[target.range()],
1829+
},
1830+
);
1831+
}
1832+
17981833
pub(super) fn report_bad_dunder_set_call<'db>(
17991834
context: &InferContext<'db, '_>,
18001835
dunder_set_failure: &CallError<'db>,

0 commit comments

Comments
 (0)