2Tests the handling of builtins.type[T].
5# Specification: https://typing.readthedocs.io/en/latest/spec/special-types.html#type
8from typing import Any, Callable, Generic, Type, TypeAlias, TypeVar, assert_type
27def func1(user_class: type) -> User:
31def func2(user_class: type[User]) -> User:
35U = TypeVar("U", bound=User)
38def func3(user_class: type[U]) -> U:
42assert_type(func1(TeamUser), User)
43assert_type(func2(TeamUser), User)
44assert_type(func3(TeamUser), TeamUser)
47# > Note that it is legal to use a union of classes as the parameter for type[].
50def func4(user_class: type[BasicUser | ProUser]) -> User:
54assert_type(func4(BasicUser), User)
55assert_type(func4(ProUser), User)
[invalid-argument-type] Argument to function `func4` is incorrect: Expected `type[BasicUser | ProUser]`, found `<class 'TeamUser'>`
59# > Any other special constructs like Callable are not allowed as an argument to type.
64def func5(x: type[T]) -> None:
[invalid-argument-type] Argument to function `func5` is incorrect: Expected `type[Unknown]`, found `<special-form 'typing.Callable'>`
73# > When type is parameterized it requires exactly one parameter. Plain type
74# > without brackets, the root of Python’s metaclass hierarchy, is equivalent to type[Any].
76bad_type1: type[int, str] # E
[invalid-type-form] type[...] must have exactly one type argument
83def func6(a: type, b: type[Any]):
84 assert_type(a, type[Any])
Unexpected error
[type-assertion-failure] Type `type[Any]` does not match asserted type `type`
91# > Regarding the behavior of type[Any] (or type), accessing attributes of a
92# > variable with this type only provides attributes and methods defined by
93# > type (for example, __repr__() and __mro__). Such a variable can be called
94# > with arbitrary arguments, and the return type is Any.
97def func7(a: type, b: type[Any], c: Type, d: Type[Any]):
98 assert_type(a.__mro__, tuple[type, ...])
99 assert_type(a.unknown, Any)
Unexpected error
[unresolved-attribute] Object of type `type` has no attribute `unknown`
100 assert_type(a.unknown(), Any)
Unexpected error
[unresolved-attribute] Object of type `type` has no attribute `unknown`
102 assert_type(b.__mro__, tuple[type, ...])
Unexpected error
[type-assertion-failure] Type `tuple[type, ...]` does not match asserted type `Any`
103 assert_type(b.unknown, Any)
104 assert_type(b.unknown(), Any)
106 assert_type(c.__mro__, tuple[type, ...])
107 assert_type(c.unknown, Any)
Unexpected error
[unresolved-attribute] Object of type `type` has no attribute `unknown`
108 assert_type(c.unknown(), Any)
Unexpected error
[unresolved-attribute] Object of type `type` has no attribute `unknown`
110 assert_type(d.__mro__, tuple[type, ...])
Unexpected error
[type-assertion-failure] Type `tuple[type, ...]` does not match asserted type `Any`
111 assert_type(d.unknown, Any)
112 assert_type(d.unknown(), Any)
115def func8(a: type[object], b: Type[object]):
116 assert_type(a.__name__, str)
[unresolved-attribute] Object of type `type` has no attribute `unknown`
119 assert_type(b.__name__, str)
[unresolved-attribute] Object of type `type` has no attribute `unknown`
123# > type is covariant in its parameter, because type[Derived] is a subtype of type[Base]:
126def func9(pro_user_class: type[ProUser]):
127 assert_type(func3(pro_user_class), ProUser)
131TA2: TypeAlias = Type[Any]
133TA4: TypeAlias = type[Any]
136def func10(a: TA1, b: TA2, c: TA3, d: TA4):
137 assert_type(a, type[Any])
Unexpected error
[type-assertion-failure] Type `type[Any]` does not match asserted type `type`
138 assert_type(b, type[Any])
139 assert_type(c, type[Any])
Unexpected error
[type-assertion-failure] Type `type[Any]` does not match asserted type `type`
140 assert_type(d, type[Any])
[unresolved-attribute] Special form `typing.Type` has no attribute `unknown`
Expected a ty diagnostic for this line
[unresolved-attribute] Class `type` has no attribute `unknown`
Expected a ty diagnostic for this line
148TA7: TypeAlias = type[T]
149TA8: TypeAlias = Type[T]
152def func11(t1: TA7[T]) -> T:
156def func12(t1: TA8[T]) -> T:
160assert_type(func11(int), int)
161assert_type(func12(int), int)
165 x1: Callable[..., Any] = v # OK
166 x2: Callable[[int, int], int] = v # OK
169 x5: type[int] = v # OK
Unexpected error
[invalid-assignment] Object of type `type` is not assignable to `type[int]`
170 x6: type[Any] = v # OK
173class ClassA(Generic[T]):
174 def method1(self, v: type) -> type[T]:
Unexpected error
[invalid-return-type] Return type does not match returned value: expected `type[T@ClassA]`, found `type`