Skip to content

Commit d2626c8

Browse files
[ty] Reject missing attributes on type[] aliases (#28267)
## Summary Previously, we allowed arbitrary attribute access on the alias objects `type[T]` and `typing.Type[T]`. We now resolve missing attributes against their known origin, `type`, instead of using the unrestricted `GenericAlias.__getattr__` fallback. ```python from typing import Any, Type, TypeAlias Modern: TypeAlias = type[Any] Legacy: TypeAlias = Type[Any] # Before: no errors. After: unresolved-attribute on each line. Modern.missing Legacy.missing def example(cls: type[Any]) -> None: cls.missing # Before and after: OK. ``` The alias object has a known origin, whereas `cls` can refer to any class with its own attributes. Attributes defined on the alias itself, such as `__args__` and `__origin__`, remain accessible. The origin is `type` regardless of the type argument, so attributes of `int`, such as `bit_length`, are not available on the alias object `type[int]`. Mypy 2.1.0, pyright 1.1.410, and pyrefly 1.3.0-dev.1 with the `default` preset agree with the behavior shown above: they reject both missing alias attributes and allow the access through `cls`. This reports the two missing errors in [`specialtypes_type`](https://github.com/python/typing/blob/cf943ccbea5596ef969eec6e2260097ff697b7ba/conformance/tests/specialtypes_type.py#L143-L146), reducing its discrepancies from 11 to 9. The file remains Partial because its remaining nine discrepancies reflect an intentional choice in ty: we interpret bare `type` as `type[object]`, whereas the conformance suite expects `type[Any]`. The other 144 conformance files are unchanged.
1 parent ee6a6d1 commit d2626c8

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

‎crates/ty_python_semantic/resources/mdtest/annotations/generic_alias.md‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# GenericAlias in type expressions
1+
# Generic aliases
2+
3+
## Type expressions
24

35
We recognize if a `types.GenericAlias` instance is created by specializing a generic class. We don't
46
explicitly mention it in our type display, but `list[int]` in the example below is a `GenericAlias`
@@ -38,3 +40,62 @@ However, using such a `GenericAlias` instance in a type expression is currently
3840
def _(strings: Strings) -> None:
3941
reveal_type(strings) # revealed: Unknown
4042
```
43+
44+
## Attributes of `type` aliases
45+
46+
The alias objects `type[Any]` and `typing.Type[Any]` delegate attribute access to their origin,
47+
`type`. They do not have arbitrary attributes, even though their type argument is dynamic.
48+
49+
```py
50+
from typing import Any, Type, TypeAlias
51+
52+
Modern: TypeAlias = type[Any]
53+
Legacy: TypeAlias = Type[Any]
54+
55+
Modern.missing # error: [unresolved-attribute]
56+
Legacy.missing # error: [unresolved-attribute]
57+
58+
reveal_type(Modern.__name__) # revealed: str
59+
reveal_type(Legacy.__name__) # revealed: str
60+
```
61+
62+
Attributes belonging to the alias itself remain accessible.
63+
64+
```py
65+
reveal_type(Modern.__args__) # revealed: tuple[Any, ...]
66+
reveal_type(Legacy.__args__) # revealed: tuple[Any, ...]
67+
Modern.__origin__
68+
Legacy.__origin__
69+
Modern.__mro_entries__((object,))
70+
```
71+
72+
The origin is `type` regardless of the type argument. Attributes of `int` are not available on the
73+
alias object `type[int]`.
74+
75+
```py
76+
Integers = type[int]
77+
LegacyIntegers = Type[int]
78+
79+
Integers.bit_length # error: [unresolved-attribute]
80+
LegacyIntegers.bit_length # error: [unresolved-attribute]
81+
```
82+
83+
When these aliases are used as annotations, their inhabitants can be arbitrary class objects.
84+
Accessing an unknown attribute through such a parameter remains valid.
85+
86+
```py
87+
def dynamic_class(modern: Modern, legacy: Legacy) -> None:
88+
reveal_type(modern.missing) # revealed: Any
89+
reveal_type(legacy.missing) # revealed: Any
90+
```
91+
92+
## Attributes of arbitrary `GenericAlias` instances
93+
94+
When the origin is unknown, we allow arbitrary attribute access through a `GenericAlias` instance.
95+
96+
```py
97+
from types import GenericAlias
98+
99+
def unknown_origin(alias: GenericAlias) -> None:
100+
reveal_type(alias.missing) # revealed: Any
101+
```

‎crates/ty_python_semantic/src/types.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7539,6 +7539,17 @@ impl<'db> Type<'db> {
75397539
return MemberLookupResult::from(Place::Undefined);
75407540
}
75417541

7542+
if matches!(
7543+
self,
7544+
Type::KnownInstance(KnownInstanceType::TypeGenericAlias(_))
7545+
) {
7546+
// `GenericAlias.__getattr__` delegates to `__origin__`. For `type[T]`, the
7547+
// origin is always `type`, not `T`, even when `T` is `Any`.
7548+
return KnownClass::Type
7549+
.to_class_literal(db, env)
7550+
.member_lookup_with_policy_and_receiver(db, env, name, policy, None);
7551+
}
7552+
75427553
let name_type = Type::string_literal(db, name);
75437554
match self.try_call_dunder(
75447555
db,

0 commit comments

Comments
 (0)