[red-knot] Understand typing.Type (#14904)

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
This commit is contained in:
InSync
2024-12-11 18:01:38 +07:00
committed by GitHub
parent c8d505c8ea
commit f30227c436
4 changed files with 49 additions and 3 deletions

View File

@@ -0,0 +1,32 @@
# `typing.Type`
## Annotation
`typing.Type` can be used interchangeably with `type`:
```py
from typing import Type
class A: ...
def _(c: Type, d: Type[A], e: Type[A]):
reveal_type(c) # revealed: type
reveal_type(d) # revealed: type[A]
c = d # fine
d = c # fine
```
## Inheritance
Inheriting from `Type` results in a MRO with `builtins.type` and `typing.Generic`. `Type` itself is
not a class.
```py
from typing import Type
class C(Type): ...
# Runtime value: `(C, type, typing.Generic, object)`
# TODO: Add `Generic` to the MRO
reveal_type(C.__mro__) # revealed: tuple[Literal[C], Literal[type], Literal[object]]
```