For types like Type[User] or TypeForm[User | None]. Type[C] annotates variables that are class objects, constrained by the type argument. See PEP 484 for more details. TypeForm[T] annotates variables that hold the result of evaluating a type expression. See PEP 747 for more detai
| 3472 | |
| 3473 | |
| 3474 | class TypeType(ProperType): |
| 3475 | """For types like Type[User] or TypeForm[User | None]. |
| 3476 | |
| 3477 | Type[C] annotates variables that are class objects, constrained by |
| 3478 | the type argument. See PEP 484 for more details. |
| 3479 | |
| 3480 | TypeForm[T] annotates variables that hold the result of evaluating |
| 3481 | a type expression. See PEP 747 for more details. |
| 3482 | |
| 3483 | We may encounter expressions whose values are specific classes; |
| 3484 | those are represented as callables (possibly overloaded) |
| 3485 | corresponding to the class's constructor's signature and returning |
| 3486 | an instance of that class. The difference with Type[C] is that |
| 3487 | those callables always represent the exact class given as the |
| 3488 | return type; Type[C] represents any class that's a subclass of C, |
| 3489 | and C may also be a type variable or a union (or Any). |
| 3490 | |
| 3491 | Many questions around subtype relationships between Type[C1] and |
| 3492 | def(...) -> C2 are answered by looking at the subtype |
| 3493 | relationships between C1 and C2, since Type[] is considered |
| 3494 | covariant. |
| 3495 | |
| 3496 | There's an unsolved problem with constructor signatures (also |
| 3497 | unsolved in PEP 484): calling a variable whose type is Type[C] |
| 3498 | assumes the constructor signature for C, even though a subclass of |
| 3499 | C might completely change the constructor signature. For now we |
| 3500 | just assume that users of Type[C] are careful not to do that (in |
| 3501 | the future we might detect when they are violating that |
| 3502 | assumption). |
| 3503 | """ |
| 3504 | |
| 3505 | __slots__ = ("item", "is_type_form") |
| 3506 | |
| 3507 | # This can't be everything, but it can be a class reference, |
| 3508 | # a generic class instance, a union, Any, a type variable... |
| 3509 | item: ProperType |
| 3510 | |
| 3511 | # If True then this TypeType represents a TypeForm[T]. |
| 3512 | # If False then this TypeType represents a Type[C]. |
| 3513 | is_type_form: bool |
| 3514 | |
| 3515 | def __init__( |
| 3516 | self, |
| 3517 | item: Bogus[Instance | AnyType | TypeVarType | TupleType | NoneType | CallableType], |
| 3518 | *, |
| 3519 | line: int = -1, |
| 3520 | column: int = -1, |
| 3521 | is_type_form: bool = False, |
| 3522 | ) -> None: |
| 3523 | """To ensure Type[Union[A, B]] is always represented as Union[Type[A], Type[B]], item of |
| 3524 | type UnionType must be handled through make_normalized static method. |
| 3525 | """ |
| 3526 | super().__init__(line, column) |
| 3527 | self.item = item |
| 3528 | self.is_type_form = is_type_form |
| 3529 | |
| 3530 | @staticmethod |
| 3531 | def make_normalized( |
no outgoing calls
no test coverage detected
searching dependent graphs…