← Back to index

specialtypes_type.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 7
FP: 11
FN: 2
Optional: 0 / 0
1"""
2Tests the handling of builtins.type[T].
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/special-types.html#type
6
7
8from typing import Any, Callable, Generic, Type, TypeAlias, TypeVar, assert_type
9
11class User:
12 ...
15class BasicUser(User):
16 ...
19class ProUser(User):
20 ...
23class TeamUser(User):
24 ...
27def func1(user_class: type) -> User:
28 return user_class()
31def func2(user_class: type[User]) -> User:
32 return user_class()
35U = TypeVar("U", bound=User)
38def func3(user_class: type[U]) -> U:
39 return user_class()
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:
51 return user_class()
54assert_type(func4(BasicUser), User)
55assert_type(func4(ProUser), User)
56func4(TeamUser) # E
[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.
61T = TypeVar("T")
64def func5(x: type[T]) -> None:
65 pass
68func5(User) # OK
69func5(tuple) # OK
70func5(Callable) # E
[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
79class MyMetaA(type):
80 ...
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`
85 a = b # OK
86 b = a # OK
87 a = type # OK
88 b = type # OK
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)
117 a.unknown # E
[unresolved-attribute] Object of type `type` has no attribute `unknown`
119 assert_type(b.__name__, str)
120 b.unknown # E
[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)
130TA1: TypeAlias = Type
131TA2: TypeAlias = Type[Any]
132TA3: TypeAlias = type
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])
143TA1.unknown # E
[unresolved-attribute] Special form `typing.Type` has no attribute `unknown`
144TA2.unknown # E
Expected a ty diagnostic for this line
145TA3.unknown # E
[unresolved-attribute] Class `type` has no attribute `unknown`
146TA4.unknown # E
Expected a ty diagnostic for this line
148TA7: TypeAlias = type[T]
149TA8: TypeAlias = Type[T]
152def func11(t1: TA7[T]) -> T:
153 return t1()
156def func12(t1: TA8[T]) -> T:
157 return t1()
160assert_type(func11(int), int)
161assert_type(func12(int), int)
164def func13(v: type):
165 x1: Callable[..., Any] = v # OK
166 x2: Callable[[int, int], int] = v # OK
167 x3: object = v # OK
168 x4: type = 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]:
175 return v # OK
Unexpected error [invalid-return-type] Return type does not match returned value: expected `type[T@ClassA]`, found `type`