Debian Codegraph @

test-data/unit/check-typevar-unbound.test

raw
1 [case testUnboundTypeVar]
2 from typing import TypeVar
3
4 T = TypeVar('T')
5
6 def f() -> T: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar
7 ...
8 f()
9
10 U = TypeVar('U', bound=int)
11
12 def g() -> U: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar \
13 # N: Consider using the upper bound "int" instead
14 ...
15
16 V = TypeVar('V', int, str)
17
18 def h() -> V: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar
19 ...
20
21 [case testInnerFunctionTypeVar]
22
23 from typing import TypeVar
24
25 T = TypeVar('T')
26
27 def g(a: T) -> T:
28 def f() -> T:
29 ...
30 return f()
31
32 [case testUnboundIterableOfTypeVars]
33 from typing import Iterable, TypeVar
34
35 T = TypeVar('T')
36
37 def f() -> Iterable[T]:
38 ...
39 f()
40
41 [case testBoundTypeVar]
42 from typing import TypeVar
43
44 T = TypeVar('T')
45
46 def f(a: T, b: T, c: int) -> T:
47 ...
48
49 [case testNestedBoundTypeVar]
50 from typing import Callable, List, Union, Tuple, TypeVar
51
52 T = TypeVar('T')
53
54 def f(a: Union[int, T], b: str) -> T:
55 ...
56
57 def g(a: Callable[..., T], b: str) -> T:
58 ...
59
60 def h(a: List[Union[Callable[..., T]]]) -> T:
61 ...
62
63 def j(a: List[Union[Callable[..., Tuple[T, T]], int]]) -> T:
64 ...
65 [builtins fixtures/tuple.pyi]
66
67 [case testUnboundedTypevarUnpacking]
68 from typing import TypeVar
69 T = TypeVar("T")
70 def f(t: T) -> None:
71 a, *b = t # E: "object" object is not iterable
72
73 [case testTypeVarType]
74 from typing import Mapping, Type, TypeVar, Union
75 T = TypeVar("T")
76
77 class A: ...
78 class B: ...
79
80 lookup_table: Mapping[str, Type[Union[A,B]]]
81 def load(lookup_table: Mapping[str, Type[T]], lookup_key: str) -> T:
82 ...
83 reveal_type(load(lookup_table, "a")) # N: Revealed type is "__main__.A | __main__.B"
84
85 lookup_table_a: Mapping[str, Type[A]]
86 def load2(lookup_table: Mapping[str, Type[Union[T, int]]], lookup_key: str) -> T:
87 ...
88 reveal_type(load2(lookup_table_a, "a")) # N: Revealed type is "__main__.A"
89
90 [builtins fixtures/tuple.pyi]
91
92 [case testTypeVarTypeAssignment]
93 # Adapted from https://github.com/python/mypy/issues/12115
94 from typing import TypeVar, Type, Callable, Union, Any
95
96 t1: Type[bool] = bool
97 t2: Union[Type[bool], Type[str]] = bool
98
99 T1 = TypeVar("T1", bound=Union[bool, str])
100 def foo1(t: Type[T1]) -> None: ...
101 foo1(t1)
102 foo1(t2)
103
104 T2 = TypeVar("T2", bool, str)
105 def foo2(t: Type[T2]) -> None: ...
106 foo2(t1)
107 # Rejected correctly: T2 cannot be Union[bool, str]
108 foo2(t2) # E: Value of type variable "T2" of "foo2" cannot be "bool | str"
109
110 T3 = TypeVar("T3")
111 def foo3(t: Type[T3]) -> None: ...
112 foo3(t1)
113 foo3(t2)
114
115 def foo4(t: Type[Union[bool, str]]) -> None: ...
116 foo4(t1)
117 foo4(t2)
118 [builtins fixtures/tuple.pyi]