Repository navigation
Commit d2626c8
authored
[ty] Reject missing attributes on
## 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.type[] aliases (#28267)1 parent ee6a6d1 commit d2626c8
2 files changed
Lines changed: 73 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| |||
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7539 | 7539 | | |
7540 | 7540 | | |
7541 | 7541 | | |
| 7542 | + | |
| 7543 | + | |
| 7544 | + | |
| 7545 | + | |
| 7546 | + | |
| 7547 | + | |
| 7548 | + | |
| 7549 | + | |
| 7550 | + | |
| 7551 | + | |
| 7552 | + | |
7542 | 7553 | | |
7543 | 7554 | | |
7544 | 7555 | | |
| |||
0 commit comments