Debian Codegraph @

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

raw
1 [case testTypeVarDefaultsBasic]
2 from typing import Generic, TypeVar, ParamSpec, Callable, Tuple, List
3 from typing_extensions import TypeVarTuple, Unpack
4
5 T1 = TypeVar("T1", default=int)
6 P1 = ParamSpec("P1", default=[int, str])
7 Ts1 = TypeVarTuple("Ts1", default=Unpack[Tuple[int, str]])
8
9 def f1(a: T1) -> List[T1]: ...
10 reveal_type(f1) # N: Revealed type is "def [T1 = builtins.int] (a: T1`-1 = builtins.int) -> builtins.list[T1`-1 = builtins.int]"
11
12 def f2(a: Callable[P1, None]) -> Callable[P1, None]: ...
13 reveal_type(f2) # N: Revealed type is "def [P1 = [builtins.int, builtins.str]] (a: def (*P1.args, **P1.kwargs)) -> def (*P1.args, **P1.kwargs)"
14
15 def f3(a: Tuple[Unpack[Ts1]]) -> Tuple[Unpack[Ts1]]: ...
16 reveal_type(f3) # N: Revealed type is "def [Ts1 = Unpack[tuple[builtins.int, builtins.str]]] (a: tuple[Unpack[Ts1`-1 = Unpack[tuple[builtins.int, builtins.str]]]]) -> tuple[Unpack[Ts1`-1 = Unpack[tuple[builtins.int, builtins.str]]]]"
17
18
19 class ClassA1(Generic[T1]): ...
20 class ClassA2(Generic[P1]): ...
21 class ClassA3(Generic[Unpack[Ts1]]): ...
22
23 reveal_type(ClassA1) # N: Revealed type is "def [T1 = builtins.int] () -> __main__.ClassA1[T1`1 = builtins.int]"
24 reveal_type(ClassA2) # N: Revealed type is "def [P1 = [builtins.int, builtins.str]] () -> __main__.ClassA2[P1`1 = [builtins.int, builtins.str]]"
25 reveal_type(ClassA3) # N: Revealed type is "def [Ts1 = Unpack[tuple[builtins.int, builtins.str]]] () -> __main__.ClassA3[Unpack[Ts1`1 = Unpack[tuple[builtins.int, builtins.str]]]]"
26 [builtins fixtures/tuple.pyi]
27
28 [case testTypeVarDefaultsValid]
29 from typing import TypeVar, ParamSpec, Any, List, Tuple
30 from typing_extensions import TypeVarTuple, Unpack
31
32 S0 = TypeVar("S0")
33 S1 = TypeVar("S1", bound=int)
34
35 P0 = ParamSpec("P0")
36 Ts0 = TypeVarTuple("Ts0")
37
38 T1 = TypeVar("T1", default=int)
39 T2 = TypeVar("T2", bound=float, default=int)
40 T3 = TypeVar("T3", bound=List[Any], default=List[int])
41 T4 = TypeVar("T4", int, str, default=int)
42 T5 = TypeVar("T5", default=S0)
43 T6 = TypeVar("T6", bound=float, default=S1)
44 T7 = TypeVar("T7", bound=List[Any], default=List[S0])
45 T8 = TypeVar("T8", default=Tuple[int, Unpack[Tuple[int, str]]])
46
47 P1 = ParamSpec("P1", default=[])
48 P2 = ParamSpec("P2", default=...)
49 P3 = ParamSpec("P3", default=[int, str])
50 P4 = ParamSpec("P4", default=P0)
51
52 Ts1 = TypeVarTuple("Ts1", default=Unpack[Tuple[int]])
53 Ts2 = TypeVarTuple("Ts2", default=Unpack[Tuple[int, ...]])
54 Ts3 = TypeVarTuple("Ts3", default=Unpack[Ts0])
55 [builtins fixtures/tuple.pyi]
56
57 [case testTypeVarDefaultsInvalid]
58 from typing import TypeVar, ParamSpec, Tuple
59 from typing_extensions import TypeVarTuple, Unpack
60
61 T1 = TypeVar("T1", default=2) # E: TypeVar "default" must be a type
62 T2 = TypeVar("T2", default=[int]) # E: Bracketed expression "[...]" is not valid as a type \
63 # N: Did you mean "List[...]"? \
64 # E: TypeVar "default" must be a type
65
66 P1 = ParamSpec("P1", default=int) # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec
67 P2 = ParamSpec("P2", default=2) # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec
68 P3 = ParamSpec("P3", default=(2, int)) # E: The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec
69 P4 = ParamSpec("P4", default=[2, int]) # E: Argument 0 of ParamSpec default must be a type
70
71 Ts1 = TypeVarTuple("Ts1", default=2) # E: The default argument to TypeVarTuple must be an Unpacked tuple
72 Ts2 = TypeVarTuple("Ts2", default=int) # E: The default argument to TypeVarTuple must be an Unpacked tuple
73 Ts3 = TypeVarTuple("Ts3", default=Tuple[int]) # E: The default argument to TypeVarTuple must be an Unpacked tuple
74 [builtins fixtures/tuple.pyi]
75
76 [case testTypeVarDefaultsInvalid2]
77 from typing import TypeVar, List, Union
78
79 T1 = TypeVar("T1", bound=str, default=int) # E: TypeVar default must be a subtype of the bound type
80 T2 = TypeVar("T2", bound=List[str], default=List[int]) # E: TypeVar default must be a subtype of the bound type
81 T3 = TypeVar("T3", int, str, default=bytes) # E: TypeVar default must be one of the constraint types
82 T4 = TypeVar("T4", int, str, default=Union[int, str]) # E: TypeVar default must be one of the constraint types
83 T5 = TypeVar("T5", float, str, default=int) # E: TypeVar default must be one of the constraint types
84
85 [case testTypeVarDefaultsInvalid3]
86 from typing import Dict, Generic, TypeVar
87
88 T1 = TypeVar("T1")
89 T2 = TypeVar("T2", default=T3) # E: Name "T3" is used before definition
90 T3 = TypeVar("T3", default=str)
91 T4 = TypeVar("T4", default=T3)
92
93 class ClassError1(Generic[T3, T1]): ... # E: "T1" cannot appear after "T3" in type parameter list because it has no default type
94
95 def func_error1(
96 a: ClassError1,
97 b: ClassError1[int],
98 c: ClassError1[int, float],
99 ) -> None:
100 reveal_type(a) # N: Revealed type is "__main__.ClassError1[builtins.str, Any]"
101 reveal_type(b) # N: Revealed type is "__main__.ClassError1[builtins.int, Any]"
102 reveal_type(c) # N: Revealed type is "__main__.ClassError1[builtins.int, builtins.float]"
103
104 k = ClassError1()
105 reveal_type(k) # N: Revealed type is "__main__.ClassError1[builtins.str, Any]"
106 l = ClassError1[int]()
107 reveal_type(l) # N: Revealed type is "__main__.ClassError1[builtins.int, Any]"
108 m = ClassError1[int, float]()
109 reveal_type(m) # N: Revealed type is "__main__.ClassError1[builtins.int, builtins.float]"
110
111 class ClassError2(Generic[T4, T3]): ... # E: Type parameter "T4" has a default type that refers to one or more type variables that are out of scope
112
113 def func_error2(
114 a: ClassError2,
115 b: ClassError2[int],
116 c: ClassError2[int, float],
117 ) -> None:
118 reveal_type(a) # N: Revealed type is "__main__.ClassError2[Any, builtins.str]"
119 reveal_type(b) # N: Revealed type is "__main__.ClassError2[builtins.int, builtins.str]"
120 reveal_type(c) # N: Revealed type is "__main__.ClassError2[builtins.int, builtins.float]"
121
122 k = ClassError2()
123 reveal_type(k) # N: Revealed type is "__main__.ClassError2[Any, builtins.str]"
124 l = ClassError2[int]()
125 reveal_type(l) # N: Revealed type is "__main__.ClassError2[builtins.int, builtins.str]"
126 m = ClassError2[int, float]()
127 reveal_type(m) # N: Revealed type is "__main__.ClassError2[builtins.int, builtins.float]"
128
129 TERR1 = Dict[T3, T1] # E: "T1" cannot appear after "T3" in type parameter list because it has no default type
130
131 def func_error_alias1(
132 a: TERR1,
133 b: TERR1[int],
134 c: TERR1[int, float],
135 ) -> None:
136 reveal_type(a) # N: Revealed type is "builtins.dict[builtins.str, Any]"
137 reveal_type(b) # N: Revealed type is "builtins.dict[builtins.int, Any]"
138 reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.float]"
139
140 TERR2 = Dict[T4, T3] # E: Type parameter "T4" has a default type that refers to one or more type variables that are out of scope
141
142 def func_error_alias2(
143 a: TERR2,
144 b: TERR2[int],
145 c: TERR2[int, float],
146 ) -> None:
147 reveal_type(a) # N: Revealed type is "builtins.dict[Any, builtins.str]"
148 reveal_type(b) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"
149 reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.float]"
150 [builtins fixtures/dict.pyi]
151
152 [case testTypeVarDefaultsExplicitNever]
153 from typing import Generic, Never, TypeVar
154
155 D = TypeVar("D", default=int)
156 class WithDefault(Generic[D]): pass
157
158 never_with_default: WithDefault[Never] = WithDefault()
159 explicit_with_default: WithDefault[Never] = WithDefault[Never]()
160
161 [case testTypeVarDefaultsFunctions]
162 from typing import TypeVar, ParamSpec, List, Union, Callable, Tuple
163 from typing_extensions import TypeVarTuple, Unpack
164
165 T1 = TypeVar("T1", default=str)
166 T2 = TypeVar("T2", bound=str, default=str)
167 T3 = TypeVar("T3", bytes, str, default=str)
168 P1 = ParamSpec("P1", default=[int, str])
169 Ts1 = TypeVarTuple("Ts1", default=Unpack[Tuple[int, str]])
170
171 def callback1(x: str) -> None: ...
172
173 def func_a1(x: Union[int, T1]) -> T1: ...
174 reveal_type(func_a1(2)) # N: Revealed type is "builtins.str"
175 reveal_type(func_a1(2.1)) # N: Revealed type is "builtins.float"
176
177 def func_a2(x: Union[int, T1]) -> List[T1]: ...
178 reveal_type(func_a2(2)) # N: Revealed type is "builtins.list[builtins.str]"
179 reveal_type(func_a2(2.1)) # N: Revealed type is "builtins.list[builtins.float]"
180
181 def func_a3(x: Union[int, T2]) -> T2: ...
182 reveal_type(func_a3(2)) # N: Revealed type is "builtins.str"
183
184 def func_a4(x: Union[int, T3]) -> T3: ...
185 reveal_type(func_a4(2)) # N: Revealed type is "builtins.str"
186
187 def func_b1(x: Union[int, Callable[P1, None]]) -> Callable[P1, None]: ...
188 reveal_type(func_b1(callback1)) # N: Revealed type is "def (x: builtins.str)"
189 reveal_type(func_b1(2)) # N: Revealed type is "def (builtins.int, builtins.str)"
190
191 def func_c1(x: Union[int, Callable[[Unpack[Ts1]], None]]) -> Tuple[Unpack[Ts1]]: ...
192 reveal_type(func_c1(callback1)) # N: Revealed type is "tuple[builtins.str]"
193 reveal_type(func_c1(2)) # N: Revealed type is "tuple[builtins.int, builtins.str]"
194 [builtins fixtures/tuple.pyi]
195
196 [case testTypeVarDefaultsClass1]
197 # flags: --disallow-any-generics
198 from typing import Generic, TypeVar, Union, overload
199
200 T1 = TypeVar("T1")
201 T2 = TypeVar("T2", default=int)
202 T3 = TypeVar("T3", default=str)
203 T4 = TypeVar("T4", default=Union[int, None])
204
205 class ClassA1(Generic[T2, T3]): ...
206
207 def func_a1(
208 a: ClassA1,
209 b: ClassA1[float],
210 c: ClassA1[float, float],
211 d: ClassA1[float, float, float], # E: "ClassA1" expects between 0 and 2 type arguments, but 3 given
212 ) -> None:
213 reveal_type(a) # N: Revealed type is "__main__.ClassA1[builtins.int, builtins.str]"
214 reveal_type(b) # N: Revealed type is "__main__.ClassA1[builtins.float, builtins.str]"
215 reveal_type(c) # N: Revealed type is "__main__.ClassA1[builtins.float, builtins.float]"
216 reveal_type(d) # N: Revealed type is "__main__.ClassA1[builtins.int, builtins.str]"
217
218 k = ClassA1()
219 reveal_type(k) # N: Revealed type is "__main__.ClassA1[builtins.int, builtins.str]"
220 l = ClassA1[float]()
221 reveal_type(l) # N: Revealed type is "__main__.ClassA1[builtins.float, builtins.str]"
222 m = ClassA1[float, float]()
223 reveal_type(m) # N: Revealed type is "__main__.ClassA1[builtins.float, builtins.float]"
224 n = ClassA1[float, float, float]() # E: Type application has too many types (expected between 0 and 2)
225 reveal_type(n) # N: Revealed type is "Any"
226
227 class ClassA2(Generic[T1, T2, T3]): ...
228
229 def func_a2(
230 a: ClassA2, # E: Missing type arguments for generic type "ClassA2"
231 b: ClassA2[float],
232 c: ClassA2[float, float],
233 d: ClassA2[float, float, float],
234 e: ClassA2[float, float, float, float], # E: "ClassA2" expects between 1 and 3 type arguments, but 4 given
235 ) -> None:
236 reveal_type(a) # N: Revealed type is "__main__.ClassA2[Any, builtins.int, builtins.str]"
237 reveal_type(b) # N: Revealed type is "__main__.ClassA2[builtins.float, builtins.int, builtins.str]"
238 reveal_type(c) # N: Revealed type is "__main__.ClassA2[builtins.float, builtins.float, builtins.str]"
239 reveal_type(d) # N: Revealed type is "__main__.ClassA2[builtins.float, builtins.float, builtins.float]"
240 reveal_type(e) # N: Revealed type is "__main__.ClassA2[Any, builtins.int, builtins.str]"
241
242 k = ClassA2() # E: Need type annotation for "k"
243 reveal_type(k) # N: Revealed type is "__main__.ClassA2[Any, builtins.int, builtins.str]"
244 l = ClassA2[float]()
245 reveal_type(l) # N: Revealed type is "__main__.ClassA2[builtins.float, builtins.int, builtins.str]"
246 m = ClassA2[float, float]()
247 reveal_type(m) # N: Revealed type is "__main__.ClassA2[builtins.float, builtins.float, builtins.str]"
248 n = ClassA2[float, float, float]()
249 reveal_type(n) # N: Revealed type is "__main__.ClassA2[builtins.float, builtins.float, builtins.float]"
250 o = ClassA2[float, float, float, float]() # E: Type application has too many types (expected between 1 and 3)
251 reveal_type(o) # N: Revealed type is "Any"
252
253 class ClassA3(Generic[T1, T2]):
254 @overload
255 def __init__(self) -> None: ...
256 @overload
257 def __init__(self, var: int) -> None: ...
258 def __init__(self, var: Union[int, None] = None) -> None: ...
259
260 def func_a3(
261 a: ClassA3, # E: Missing type arguments for generic type "ClassA3"
262 b: ClassA3[float],
263 c: ClassA3[float, float],
264 d: ClassA3[float, float, float], # E: "ClassA3" expects between 1 and 2 type arguments, but 3 given
265 ) -> None:
266 reveal_type(a) # N: Revealed type is "__main__.ClassA3[Any, builtins.int]"
267 reveal_type(b) # N: Revealed type is "__main__.ClassA3[builtins.float, builtins.int]"
268 reveal_type(c) # N: Revealed type is "__main__.ClassA3[builtins.float, builtins.float]"
269 reveal_type(d) # N: Revealed type is "__main__.ClassA3[Any, builtins.int]"
270
271 k = ClassA3() # E: Need type annotation for "k"
272 reveal_type(k) # N: Revealed type is "__main__.ClassA3[Any, builtins.int]"
273 l = ClassA3[float]()
274 reveal_type(l) # N: Revealed type is "__main__.ClassA3[builtins.float, builtins.int]"
275 m = ClassA3[float, float]()
276 reveal_type(m) # N: Revealed type is "__main__.ClassA3[builtins.float, builtins.float]"
277 n = ClassA3[float, float, float]() # E: Type application has too many types (expected between 1 and 2)
278 reveal_type(n) # N: Revealed type is "Any"
279
280 class ClassA4(Generic[T4]): ...
281
282 def func_a4(
283 a: ClassA4,
284 b: ClassA4[float],
285 ) -> None:
286 reveal_type(a) # N: Revealed type is "__main__.ClassA4[builtins.int | None]"
287 reveal_type(b) # N: Revealed type is "__main__.ClassA4[builtins.float]"
288
289 k = ClassA4()
290 reveal_type(k) # N: Revealed type is "__main__.ClassA4[builtins.int | None]"
291 l = ClassA4[float]()
292 reveal_type(l) # N: Revealed type is "__main__.ClassA4[builtins.float]"
293
294 [case testTypeVarDefaultsClass2]
295 # flags: --disallow-any-generics
296 from typing import Generic, ParamSpec
297
298 P1 = ParamSpec("P1")
299 P2 = ParamSpec("P2", default=[int, str])
300 P3 = ParamSpec("P3", default=...)
301
302 class ClassB1(Generic[P2, P3]): ...
303
304 def func_b1(
305 a: ClassB1,
306 b: ClassB1[[float]],
307 c: ClassB1[[float], [float]],
308 d: ClassB1[[float], [float], [float]], # E: "ClassB1" expects between 0 and 2 type arguments, but 3 given
309 ) -> None:
310 reveal_type(a) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], ...]"
311 reveal_type(b) # N: Revealed type is "__main__.ClassB1[[builtins.float], ...]"
312 reveal_type(c) # N: Revealed type is "__main__.ClassB1[[builtins.float], [builtins.float]]"
313 reveal_type(d) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], ...]"
314
315 k = ClassB1()
316 reveal_type(k) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], [*Any, **Any]]"
317 l = ClassB1[[float]]()
318 reveal_type(l) # N: Revealed type is "__main__.ClassB1[[builtins.float], [*Any, **Any]]"
319 m = ClassB1[[float], [float]]()
320 reveal_type(m) # N: Revealed type is "__main__.ClassB1[[builtins.float], [builtins.float]]"
321 n = ClassB1[[float], [float], [float]]() # E: Type application has too many types (expected between 0 and 2)
322 reveal_type(n) # N: Revealed type is "Any"
323
324 class ClassB2(Generic[P1, P2]): ...
325
326 def func_b2(
327 a: ClassB2, # E: Missing type arguments for generic type "ClassB2"
328 b: ClassB2[[float]],
329 c: ClassB2[[float], [float]],
330 d: ClassB2[[float], [float], [float]], # E: "ClassB2" expects between 1 and 2 type arguments, but 3 given
331 ) -> None:
332 reveal_type(a) # N: Revealed type is "__main__.ClassB2[Any, [builtins.int, builtins.str]]"
333 reveal_type(b) # N: Revealed type is "__main__.ClassB2[[builtins.float], [builtins.int, builtins.str]]"
334 reveal_type(c) # N: Revealed type is "__main__.ClassB2[[builtins.float], [builtins.float]]"
335 reveal_type(d) # N: Revealed type is "__main__.ClassB2[Any, [builtins.int, builtins.str]]"
336
337 k = ClassB2() # E: Need type annotation for "k"
338 reveal_type(k) # N: Revealed type is "__main__.ClassB2[Any, [builtins.int, builtins.str]]"
339 l = ClassB2[[float]]()
340 reveal_type(l) # N: Revealed type is "__main__.ClassB2[[builtins.float], [builtins.int, builtins.str]]"
341 m = ClassB2[[float], [float]]()
342 reveal_type(m) # N: Revealed type is "__main__.ClassB2[[builtins.float], [builtins.float]]"
343 n = ClassB2[[float], [float], [float]]() # E: Type application has too many types (expected between 1 and 2)
344 reveal_type(n) # N: Revealed type is "Any"
345
346 [case testTypeVarDefaultsClass3]
347 # flags: --disallow-any-generics
348 from typing import Generic, Tuple, TypeVar
349 from typing_extensions import TypeVarTuple, Unpack
350
351 T1 = TypeVar("T1")
352 T3 = TypeVar("T3", default=str)
353
354 Ts1 = TypeVarTuple("Ts1")
355 Ts2 = TypeVarTuple("Ts2", default=Unpack[Tuple[int, str]])
356 Ts3 = TypeVarTuple("Ts3", default=Unpack[Tuple[float, ...]])
357 Ts4 = TypeVarTuple("Ts4", default=Unpack[Tuple[()]])
358
359 class ClassC1(Generic[Unpack[Ts2]]): ...
360
361 def func_c1(
362 a: ClassC1,
363 b: ClassC1[float],
364 ) -> None:
365 reveal_type(a) # N: Revealed type is "__main__.ClassC1[builtins.int, builtins.str]"
366 reveal_type(b) # N: Revealed type is "__main__.ClassC1[builtins.float]"
367
368 k = ClassC1()
369 reveal_type(k) # N: Revealed type is "__main__.ClassC1[builtins.int, builtins.str]"
370 l = ClassC1[float]()
371 reveal_type(l) # N: Revealed type is "__main__.ClassC1[builtins.float]"
372
373 class ClassC2(Generic[T3, Unpack[Ts3]]): ...
374
375 def func_c2(
376 a: ClassC2,
377 b: ClassC2[int],
378 ) -> None:
379 reveal_type(a) # N: Revealed type is "__main__.ClassC2[builtins.str, Unpack[builtins.tuple[builtins.float, ...]]]"
380 reveal_type(b) # N: Revealed type is "__main__.ClassC2[builtins.int]"
381
382 k = ClassC2()
383 reveal_type(k) # N: Revealed type is "__main__.ClassC2[builtins.str, Unpack[builtins.tuple[builtins.float, ...]]]"
384 l = ClassC2[int]()
385 reveal_type(l) # N: Revealed type is "__main__.ClassC2[builtins.int]"
386
387 class ClassC3(Generic[T3, Unpack[Ts4]]): ...
388
389 def func_c3(
390 a: ClassC3,
391 b: ClassC3[int],
392 c: ClassC3[int, Unpack[Tuple[float]]]
393 ) -> None:
394 reveal_type(a) # N: Revealed type is "__main__.ClassC3[builtins.str]"
395 reveal_type(b) # N: Revealed type is "__main__.ClassC3[builtins.int]"
396 reveal_type(c) # N: Revealed type is "__main__.ClassC3[builtins.int, builtins.float]"
397
398 k = ClassC3()
399 reveal_type(k) # N: Revealed type is "__main__.ClassC3[builtins.str]"
400 l = ClassC3[int]()
401 reveal_type(l) # N: Revealed type is "__main__.ClassC3[builtins.int]"
402 m = ClassC3[int, Unpack[Tuple[float]]]()
403 reveal_type(m) # N: Revealed type is "__main__.ClassC3[builtins.int, builtins.float]"
404
405 class ClassC4(Generic[T1, Unpack[Ts1], T3]): # E: A type variable with default cannot follow TypeVarTuple
406 x: T3
407
408 reveal_type(ClassC4().x) # N: Revealed type is "Any"
409
410 def func_c4(
411 a: ClassC4, # E: Missing type arguments for generic type "ClassC4"
412 b: ClassC4[int],
413 c: ClassC4[int, float],
414 ) -> None:
415 reveal_type(a) # N: Revealed type is "__main__.ClassC4[Any, Unpack[builtins.tuple[Any, ...]]]"
416 reveal_type(b) # N: Revealed type is "__main__.ClassC4[builtins.int]"
417 reveal_type(c) # N: Revealed type is "__main__.ClassC4[builtins.int, builtins.float]"
418
419 k = ClassC4() # E: Need type annotation for "k"
420 reveal_type(k) # N: Revealed type is "__main__.ClassC4[Any, Unpack[builtins.tuple[Any, ...]]]"
421 l = ClassC4[int]()
422 reveal_type(l) # N: Revealed type is "__main__.ClassC4[builtins.int]"
423 m = ClassC4[int, float]()
424 reveal_type(m) # N: Revealed type is "__main__.ClassC4[builtins.int, builtins.float]"
425 [builtins fixtures/tuple.pyi]
426
427 [case testTypeVarDefaultsSwap]
428 from typing import TypeVar, Generic
429
430 T = TypeVar("T")
431 X = TypeVar("X", default=object)
432 Y = TypeVar("Y", default=object)
433
434
435 class Foo(Generic[T, Y]):
436 def test(self) -> None:
437 reveal_type( Foo[Y, T]() ) # N: Revealed type is "__main__.Foo[Y`2 = builtins.object, T`1]"
438
439
440 class Bar(Generic[X, Y]):
441 def test(self) -> None:
442 reveal_type( Bar[Y, X]() ) # N: Revealed type is "__main__.Bar[Y`2 = builtins.object, X`1 = builtins.object]"
443
444
445 [case testTypeVarDefaultsSwap2]
446 from typing import TypeVar, Generic
447
448 X = TypeVar("X", default=object)
449 Y = TypeVar("Y", default=object)
450 U = TypeVar("U", default=object)
451 V = TypeVar("V", default=object)
452
453 class Transform(Generic[X, Y]):
454 def invert(self) -> "Transform[Y, X]": ...
455
456 class Foo(Transform[U, V], Generic[U, V]):
457 def invert(self) -> "Foo[V, U]": ...
458
459 [case testTypeVarDefaultsClassRecursive1]
460 # flags: --disallow-any-generics
461 from typing import Generic, TypeVar, List
462
463 T1 = TypeVar("T1", default=str)
464 T2 = TypeVar("T2", default=T1)
465 T3 = TypeVar("T3", default=T2)
466 T4 = TypeVar("T4", default=List[T1])
467
468 class ClassD1(Generic[T1, T2]): ...
469
470 def func_d1(
471 a: ClassD1,
472 b: ClassD1[int],
473 c: ClassD1[int, float]
474 ) -> None:
475 reveal_type(a) # N: Revealed type is "__main__.ClassD1[builtins.str, builtins.str]"
476 reveal_type(b) # N: Revealed type is "__main__.ClassD1[builtins.int, builtins.int]"
477 reveal_type(c) # N: Revealed type is "__main__.ClassD1[builtins.int, builtins.float]"
478
479 k = ClassD1()
480 reveal_type(k) # N: Revealed type is "__main__.ClassD1[builtins.str, builtins.str]"
481 l = ClassD1[int]()
482 reveal_type(l) # N: Revealed type is "__main__.ClassD1[builtins.int, builtins.int]"
483 m = ClassD1[int, float]()
484 reveal_type(m) # N: Revealed type is "__main__.ClassD1[builtins.int, builtins.float]"
485
486 class ClassD2(Generic[T1, T2, T3]): ...
487
488 def func_d2(
489 a: ClassD2,
490 b: ClassD2[int],
491 c: ClassD2[int, float],
492 d: ClassD2[int, float, str],
493 ) -> None:
494 reveal_type(a) # N: Revealed type is "__main__.ClassD2[builtins.str, builtins.str, builtins.str]"
495 reveal_type(b) # N: Revealed type is "__main__.ClassD2[builtins.int, builtins.int, builtins.int]"
496 reveal_type(c) # N: Revealed type is "__main__.ClassD2[builtins.int, builtins.float, builtins.float]"
497 reveal_type(d) # N: Revealed type is "__main__.ClassD2[builtins.int, builtins.float, builtins.str]"
498
499 k = ClassD2()
500 reveal_type(k) # N: Revealed type is "__main__.ClassD2[builtins.str, builtins.str, builtins.str]"
501 l = ClassD2[int]()
502 reveal_type(l) # N: Revealed type is "__main__.ClassD2[builtins.int, builtins.int, builtins.int]"
503 m = ClassD2[int, float]()
504 reveal_type(m) # N: Revealed type is "__main__.ClassD2[builtins.int, builtins.float, builtins.float]"
505 n = ClassD2[int, float, str]()
506 reveal_type(n) # N: Revealed type is "__main__.ClassD2[builtins.int, builtins.float, builtins.str]"
507
508 class ClassD3(Generic[T1, T4]): ...
509
510 def func_d3(
511 a: ClassD3,
512 b: ClassD3[int],
513 c: ClassD3[int, float],
514 ) -> None:
515 reveal_type(a) # N: Revealed type is "__main__.ClassD3[builtins.str, builtins.list[builtins.str]]"
516 reveal_type(b) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.list[builtins.int]]"
517 reveal_type(c) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.float]"
518
519 k = ClassD3()
520 reveal_type(k) # N: Revealed type is "__main__.ClassD3[builtins.str, builtins.list[builtins.str]]"
521 l = ClassD3[int]()
522 reveal_type(l) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.list[builtins.int]]"
523 m = ClassD3[int, float]()
524 reveal_type(m) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.float]"
525
526 [case testTypeVarDefaultsClassRecursiveMultipleFiles]
527 # flags: --disallow-any-generics
528 from typing import Generic, TypeVar
529 from file2 import T as T2
530
531 T = TypeVar("T", default=T2)
532
533 class ClassG1(Generic[T2, T]):
534 pass
535
536 def func(
537 a: ClassG1,
538 b: ClassG1[str],
539 c: ClassG1[str, float],
540 ) -> None:
541 reveal_type(a) # N: Revealed type is "__main__.ClassG1[builtins.int, builtins.int]"
542 reveal_type(b) # N: Revealed type is "__main__.ClassG1[builtins.str, builtins.str]"
543 reveal_type(c) # N: Revealed type is "__main__.ClassG1[builtins.str, builtins.float]"
544
545 k = ClassG1()
546 reveal_type(k) # N: Revealed type is "__main__.ClassG1[builtins.int, builtins.int]"
547 l = ClassG1[str]()
548 reveal_type(l) # N: Revealed type is "__main__.ClassG1[builtins.str, builtins.str]"
549 m = ClassG1[str, float]()
550 reveal_type(m) # N: Revealed type is "__main__.ClassG1[builtins.str, builtins.float]"
551
552 [file file2.py]
553 from typing import TypeVar
554 T = TypeVar('T', default=int)
555
556 [case testTypeVarDefaultsTypeAlias1]
557 # flags: --disallow-any-generics
558 from typing import Any, Dict, List, Tuple, TypeVar, Union
559
560 T1 = TypeVar("T1")
561 T2 = TypeVar("T2", default=int)
562 T3 = TypeVar("T3", default=str)
563 T4 = TypeVar("T4")
564
565 TA1 = Dict[T2, T3]
566
567 def func_a1(
568 a: TA1,
569 b: TA1[float],
570 c: TA1[float, float],
571 d: TA1[float, float, float], # E: Bad number of arguments for type alias, expected between 0 and 2, given 3
572 ) -> None:
573 reveal_type(a) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"
574 reveal_type(b) # N: Revealed type is "builtins.dict[builtins.float, builtins.str]"
575 reveal_type(c) # N: Revealed type is "builtins.dict[builtins.float, builtins.float]"
576 reveal_type(d) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"
577
578 TA2 = Tuple[T1, T2, T3]
579
580 def func_a2(
581 a: TA2, # E: Missing type arguments for generic type "TA2"
582 b: TA2[float],
583 c: TA2[float, float],
584 d: TA2[float, float, float],
585 e: TA2[float, float, float, float], # E: Bad number of arguments for type alias, expected between 1 and 3, given 4
586 ) -> None:
587 reveal_type(a) # N: Revealed type is "tuple[Any, builtins.int, builtins.str]"
588 reveal_type(b) # N: Revealed type is "tuple[builtins.float, builtins.int, builtins.str]"
589 reveal_type(c) # N: Revealed type is "tuple[builtins.float, builtins.float, builtins.str]"
590 reveal_type(d) # N: Revealed type is "tuple[builtins.float, builtins.float, builtins.float]"
591 reveal_type(e) # N: Revealed type is "tuple[Any, builtins.int, builtins.str]"
592
593 TA3 = Union[Dict[T1, T2], List[T3]]
594
595 def func_a3(
596 a: TA3, # E: Missing type arguments for generic type "TA3"
597 b: TA3[float],
598 c: TA3[float, float],
599 d: TA3[float, float, float],
600 e: TA3[float, float, float, float], # E: Bad number of arguments for type alias, expected between 1 and 3, given 4
601 ) -> None:
602 reveal_type(a) # N: Revealed type is "builtins.dict[Any, builtins.int] | builtins.list[builtins.str]"
603 reveal_type(b) # N: Revealed type is "builtins.dict[builtins.float, builtins.int] | builtins.list[builtins.str]"
604 reveal_type(c) # N: Revealed type is "builtins.dict[builtins.float, builtins.float] | builtins.list[builtins.str]"
605 reveal_type(d) # N: Revealed type is "builtins.dict[builtins.float, builtins.float] | builtins.list[builtins.float]"
606 reveal_type(e) # N: Revealed type is "builtins.dict[Any, builtins.int] | builtins.list[builtins.str]"
607
608 TA4 = Tuple[T1, T4, T2]
609
610 def func_a4(
611 a: TA4, # E: Missing type arguments for generic type "TA4"
612 b: TA4[float], # E: Bad number of arguments for type alias, expected between 2 and 3, given 1
613 c: TA4[float, float],
614 d: TA4[float, float, float],
615 e: TA4[float, float, float, float], # E: Bad number of arguments for type alias, expected between 2 and 3, given 4
616 ) -> None:
617 reveal_type(a) # N: Revealed type is "tuple[Any, Any, builtins.int]"
618 reveal_type(b) # N: Revealed type is "tuple[Any, Any, builtins.int]"
619 reveal_type(c) # N: Revealed type is "tuple[builtins.float, builtins.float, builtins.int]"
620 reveal_type(d) # N: Revealed type is "tuple[builtins.float, builtins.float, builtins.float]"
621 reveal_type(e) # N: Revealed type is "tuple[Any, Any, builtins.int]"
622 [builtins fixtures/dict.pyi]
623
624 [case testTypeVarDefaultsTypeAlias2]
625 # flags: --disallow-any-generics
626 from typing import Any, Generic, ParamSpec
627
628 P1 = ParamSpec("P1")
629 P2 = ParamSpec("P2", default=[int, str])
630 P3 = ParamSpec("P3", default=...)
631
632 class ClassB1(Generic[P2, P3]): ...
633 TB1 = ClassB1[P2, P3]
634
635 def func_b1(
636 a: TB1,
637 b: TB1[[float]],
638 c: TB1[[float], [float]],
639 d: TB1[[float], [float], [float]], # E: Bad number of arguments for type alias, expected between 0 and 2, given 3
640 ) -> None:
641 reveal_type(a) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], [*Any, **Any]]"
642 reveal_type(b) # N: Revealed type is "__main__.ClassB1[[builtins.float], [*Any, **Any]]"
643 reveal_type(c) # N: Revealed type is "__main__.ClassB1[[builtins.float], [builtins.float]]"
644 reveal_type(d) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], [*Any, **Any]]"
645
646 class ClassB2(Generic[P1, P2]): ...
647 TB2 = ClassB2[P1, P2]
648
649 def func_b2(
650 a: TB2, # E: Missing type arguments for generic type "TB2"
651 b: TB2[[float]],
652 c: TB2[[float], [float]],
653 d: TB2[[float], [float], [float]], # E: Bad number of arguments for type alias, expected between 1 and 2, given 3
654 ) -> None:
655 reveal_type(a) # N: Revealed type is "__main__.ClassB2[Any, [builtins.int, builtins.str]]"
656 reveal_type(b) # N: Revealed type is "__main__.ClassB2[[builtins.float], [builtins.int, builtins.str]]"
657 reveal_type(c) # N: Revealed type is "__main__.ClassB2[[builtins.float], [builtins.float]]"
658 reveal_type(d) # N: Revealed type is "__main__.ClassB2[Any, [builtins.int, builtins.str]]"
659 [builtins fixtures/tuple.pyi]
660
661 [case testTypeVarDefaultsTypeAlias3]
662 # flags: --disallow-any-generics
663 from typing import Tuple, TypeVar
664 from typing_extensions import TypeVarTuple, Unpack
665
666 T1 = TypeVar("T1")
667 T3 = TypeVar("T3", default=str)
668
669 Ts1 = TypeVarTuple("Ts1")
670 Ts2 = TypeVarTuple("Ts2", default=Unpack[Tuple[int, str]])
671 Ts3 = TypeVarTuple("Ts3", default=Unpack[Tuple[float, ...]])
672 Ts4 = TypeVarTuple("Ts4", default=Unpack[Tuple[()]])
673
674 TC1 = Tuple[Unpack[Ts2]]
675
676 def func_c1(
677 a: TC1,
678 b: TC1[float],
679 ) -> None:
680 reveal_type(a) # N: Revealed type is "tuple[builtins.int, builtins.str]"
681 reveal_type(b) # N: Revealed type is "tuple[builtins.float]"
682
683 TC2 = Tuple[T3, Unpack[Ts3]]
684
685 def func_c2(
686 a: TC2,
687 b: TC2[int],
688 ) -> None:
689 reveal_type(a) # N: Revealed type is "tuple[builtins.str, Unpack[builtins.tuple[builtins.float, ...]]]"
690 reveal_type(b) # N: Revealed type is "tuple[builtins.int]"
691
692 TC3 = Tuple[T3, Unpack[Ts4]]
693
694 def func_c3(
695 a: TC3,
696 b: TC3[int],
697 c: TC3[int, Unpack[Tuple[float]]],
698 ) -> None:
699 reveal_type(a) # N: Revealed type is "tuple[builtins.str]"
700 reveal_type(b) # N: Revealed type is "tuple[builtins.int]"
701 reveal_type(c) # N: Revealed type is "tuple[builtins.int, builtins.float]"
702
703 TC4 = Tuple[T1, Unpack[Ts1], T3] # E: A type variable with default cannot follow TypeVarTuple
704
705 def func_c4(
706 a: TC4, # E: Missing type arguments for generic type "TC4"
707 b: TC4[int],
708 c: TC4[int, float],
709 ) -> None:
710 reveal_type(a) # N: Revealed type is "tuple[Any, Unpack[builtins.tuple[Any, ...]], Any]"
711 reveal_type(b) # N: Revealed type is "tuple[builtins.int, Any]"
712 reveal_type(c) # N: Revealed type is "tuple[builtins.int, builtins.float, Any]"
713 [builtins fixtures/tuple.pyi]
714
715 [case testTypeVarDefaultsTypeAliasRecursive1]
716 # flags: --disallow-any-generics
717 from typing import Dict, List, TypeVar
718
719 T1 = TypeVar("T1")
720 T2 = TypeVar("T2", default=T1)
721
722 TD1 = Dict[T1, T2]
723
724 def func_d1(
725 a: TD1, # E: Missing type arguments for generic type "TD1"
726 b: TD1[int],
727 c: TD1[int, float],
728 ) -> None:
729 reveal_type(a) # N: Revealed type is "builtins.dict[Any, Any]"
730 reveal_type(b) # N: Revealed type is "builtins.dict[builtins.int, builtins.int]"
731 reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.float]"
732 [builtins fixtures/dict.pyi]
733
734 [case testTypeVarDefaultsTypeAliasRecursive2]
735 from typing import Any, Dict, Generic, TypeVar
736
737 T1 = TypeVar("T1", default=str)
738 T2 = TypeVar("T2", default=T1)
739 Alias1 = Dict[T1, T2]
740 T3 = TypeVar("T3")
741 class A(Generic[T3]): ...
742
743 T4 = TypeVar("T4", default=A[Alias1])
744 class B(Generic[T4]): ...
745
746 def func_d3(
747 a: B,
748 b: B[A[Alias1[int]]],
749 c: B[A[Alias1[int, float]]],
750 d: B[int],
751 ) -> None:
752 reveal_type(a) # N: Revealed type is "__main__.B[__main__.A[builtins.dict[builtins.str, builtins.str]]]"
753 reveal_type(b) # N: Revealed type is "__main__.B[__main__.A[builtins.dict[builtins.int, builtins.int]]]"
754 reveal_type(c) # N: Revealed type is "__main__.B[__main__.A[builtins.dict[builtins.int, builtins.float]]]"
755 reveal_type(d) # N: Revealed type is "__main__.B[builtins.int]"
756 [builtins fixtures/dict.pyi]
757
758 [case testTypeVarDefaultsAndTypeObjectTypeInUnion]
759 from __future__ import annotations
760 from typing import Generic
761 from typing_extensions import TypeVar
762
763 _I = TypeVar("_I", default=int)
764
765 class C(Generic[_I]): pass
766
767 t: type[C] | int = C
768 [builtins fixtures/tuple.pyi]
769
770 [case testGenericTypeAliasWithDefaultTypeVarPreservesNoneInDefault]
771 from typing_extensions import TypeVar
772 from typing import Generic, Union
773
774 T1 = TypeVar("T1", default=Union[int, None])
775 T2 = TypeVar("T2", default=Union[int, None])
776
777
778 class A(Generic[T1, T2]):
779 def __init__(self, a: T1, b: T2) -> None:
780 self.a = a
781 self.b = b
782
783
784 MyA = A[T1, int]
785 a: MyA = A(None, 10)
786 reveal_type(a.a) # N: Revealed type is "builtins.int | None"
787 [builtins fixtures/tuple.pyi]
788
789 [case testTypeVarConstraintsDefaultAliasesTypeAliasType]
790 from typing import Generic
791 from typing_extensions import TypeAliasType, TypeVar
792
793 K = TypeAliasType("K", int)
794 V = TypeAliasType("V", int)
795 L = TypeAliasType("L", list[int])
796 T1 = TypeVar("T1", str, K, default=K)
797 T2 = TypeVar("T2", str, K, default=V)
798 T3 = TypeVar("T3", str, L, default=L)
799
800 class A1(Generic[T1]):
801 x: T1
802 class A2(Generic[T2]):
803 x: T2
804 class A3(Generic[T3]):
805 x: T3
806
807 reveal_type(A1().x) # N: Revealed type is "builtins.int"
808 reveal_type(A2().x) # N: Revealed type is "builtins.int"
809 reveal_type(A3().x) # N: Revealed type is "builtins.list[builtins.int]"
810 [builtins fixtures/tuple.pyi]
811
812 [case testTypeVarConstraintsDefaultAliasesImplicitAlias]
813 from typing_extensions import TypeVar
814
815 K = int
816 V = int
817 L = list[int]
818 T1 = TypeVar("T1", str, K, default=K)
819 T2 = TypeVar("T2", str, K, default=V)
820 T3 = TypeVar("T3", str, L, default=L)
821 [builtins fixtures/tuple.pyi]
822
823 [case testTypeVarConstraintsDefaultAliasesExplicitAlias]
824 from typing_extensions import TypeAlias, TypeVar
825
826 K: TypeAlias = int
827 V: TypeAlias = int
828 L: TypeAlias = list[int]
829 T1 = TypeVar("T1", str, K, default=K)
830 T2 = TypeVar("T2", str, K, default=V)
831 T3 = TypeVar("T3", str, L, default=L)
832 [builtins fixtures/tuple.pyi]
833
834 [case testTypeVarConstraintsDefaultSpecialTypes]
835 from typing import Generic, NamedTuple
836 from typing_extensions import TypedDict, TypeVar
837
838 class TD(TypedDict):
839 foo: str
840
841 class NT(NamedTuple):
842 foo: str
843
844 T1 = TypeVar("T1", str, TD, default=TD)
845 T2 = TypeVar("T2", str, NT, default=NT)
846
847 class A1(Generic[T1]):
848 x: T1
849 class A2(Generic[T2]):
850 x: T2
851
852 reveal_type(A1().x) # N: Revealed type is "TypedDict('__main__.TD', {'foo': builtins.str})"
853 reveal_type(A2().x) # N: Revealed type is "tuple[builtins.str, fallback=__main__.NT]"
854 [builtins fixtures/tuple.pyi]
855
856 [case testTypeVarConstraintsDefaultSpecialTypesGeneric]
857 from typing import Generic, NamedTuple
858 from typing_extensions import TypedDict, TypeVar
859
860 T = TypeVar("T")
861
862 class TD(TypedDict, Generic[T]):
863 foo: T
864 class TD2(TD[int]): pass
865 class TD3(TD[int]):
866 bar: str
867
868 class NT(NamedTuple, Generic[T]):
869 foo: T
870 class NT2(NT[int]): pass
871
872 T1 = TypeVar("T1", str, TD[int], default=TD[int])
873 T2 = TypeVar("T2", str, NT[int], default=NT[int])
874 T3 = TypeVar("T3", str, TD2, default=TD[int])
875 T4 = TypeVar("T4", str, TD3, default=TD[int]) # E: TypeVar default must be one of the constraint types
876 T5 = TypeVar("T5", str, NT2, default=NT[int]) # E: TypeVar default must be one of the constraint types
877
878 class A1(Generic[T1]):
879 x: T1
880 class A2(Generic[T2]):
881 x: T2
882 class A3(Generic[T3]):
883 x: T3
884
885 reveal_type(A1().x) # N: Revealed type is "TypedDict('__main__.TD', {'foo': builtins.int})"
886 reveal_type(A2().x) # N: Revealed type is "tuple[builtins.int, fallback=__main__.NT[builtins.int]]"
887 reveal_type(A3().x) # N: Revealed type is "TypedDict('__main__.TD', {'foo': builtins.int})"
888 [builtins fixtures/tuple.pyi]
889
890 [case testDefaultsApplicationInAliasNoCrash]
891 # https://github.com/python/mypy/issues/19186
892 from typing import Generic, TypeVar
893 from typing_extensions import TypeAlias
894
895 T1 = TypeVar("T1")
896 T2 = TypeVar("T2", default=T1)
897
898 Alias: TypeAlias = "MyClass[T1, T2]"
899
900 class MyClass(Generic["T1", "T2"]): ...
901 [builtins fixtures/tuple.pyi]
902
903 [case testTypeVariableDefaultNotSticky]
904 from typing import Generic, TypeVar
905
906 T1 = TypeVar("T1")
907 T2 = TypeVar("T2", default=T1)
908
909 class MyClass(Generic[T1, T2]): ...
910
911 reveal_type(MyClass[int]) # N: Revealed type is "def () -> __main__.MyClass[builtins.int, builtins.int]"
912 reveal_type(MyClass[str]) # N: Revealed type is "def () -> __main__.MyClass[builtins.str, builtins.str]"
913 reveal_type(MyClass[bytes]) # N: Revealed type is "def () -> __main__.MyClass[builtins.bytes, builtins.bytes]"
914 [builtins fixtures/tuple.pyi]
915
916 [case testDefaultsApplicationInAliasNoCrashNested]
917 from typing import Generic, TypeVar
918 from typing_extensions import TypeAlias
919
920 T1 = TypeVar("T1")
921 T2 = TypeVar("T2", default=list[T1])
922
923 Alias: TypeAlias = "MyClass[T1, T2]"
924
925 class MyClass(Generic["T1", "T2"]): ...
926 x: Alias[int]
927 reveal_type(x) # N: Revealed type is "__main__.MyClass[builtins.int, builtins.list[builtins.int]]"
928 [builtins fixtures/tuple.pyi]
929
930 [case testDefaultsMustBeInScope]
931 from typing import Generic, TypeVar
932
933 T1 = TypeVar("T1")
934 T2 = TypeVar("T2", default=T1)
935 T3 = TypeVar("T3", default=T2)
936
937 class A(Generic[T1, T2, T3]): ...
938 reveal_type(A) # N: Revealed type is "def [T1, T2 = T1`1, T3 = T2`2 = T1`1] () -> __main__.A[T1`1, T2`2 = T1`1, T3`3 = T2`2 = T1`1]"
939 a: A[int]
940 reveal_type(a) # N: Revealed type is "__main__.A[builtins.int, builtins.int, builtins.int]"
941
942 AA = tuple[T1, T2, T3]
943 aa: AA[str]
944 reveal_type(aa) # N: Revealed type is "tuple[builtins.str, builtins.str, builtins.str]"
945
946 class B(Generic[T1, T3]): ... # E: Type parameter "T3" has a default type that refers to one or more type variables that are out of scope
947 reveal_type(B) # N: Revealed type is "def [T1, T3 = Any] () -> __main__.B[T1`1, T3`2 = Any]"
948 b: B[int]
949 reveal_type(b) # N: Revealed type is "__main__.B[builtins.int, Any]"
950
951 class C(Generic[T2]): ... # E: Type parameter "T2" has a default type that refers to one or more type variables that are out of scope
952 reveal_type(C) # N: Revealed type is "def [T2 = Any] () -> __main__.C[T2`1 = Any]"
953 c: C
954 reveal_type(c) # N: Revealed type is "__main__.C[Any]"
955
956 class D(Generic[T2, T1]): ... # E: Type parameter "T2" has a default type that refers to one or more type variables that are out of scope \
957 # E: "T1" cannot appear after "T2" in type parameter list because it has no default type
958 reveal_type(D) # N: Revealed type is "def [T2 = Any, T1 = Any] () -> __main__.D[T2`1 = Any, T1`2 = Any]"
959 d: D
960 reveal_type(d) # N: Revealed type is "__main__.D[Any, Any]"
961 [builtins fixtures/tuple.pyi]
962
963 [case testRecursiveTypeVarDefaultBasic]
964 from typing import TypeVar, Generic
965
966 class C(Generic["T"]):
967 pass
968
969 T = TypeVar("T", bound=C, default=C)
970
971 c: C
972 reveal_type(c) # N: Revealed type is "__main__.C[__main__.C[Any]]"
973
974 [case testRecursiveTypeVarDefaultMutual]
975 from typing import TypeVar, Generic
976
977 class C(Generic["T"]):
978 pass
979
980 class D(Generic["S"]):
981 pass
982
983 T = TypeVar("T", default=D)
984 S = TypeVar("S", default=C)
985
986 c: C
987 d: D
988 reveal_type(c) # N: Revealed type is "__main__.C[__main__.D[Any]]"
989 reveal_type(d) # N: Revealed type is "__main__.D[__main__.C[Any]]"
990
991 [case testNonRecursiveSimpleTypeVarDefault]
992 from typing import TypeVar, Generic
993
994 S = TypeVar("S", default="Parent")
995 class Child(Generic[S]): ...
996
997 T = TypeVar("T", default=int)
998 class Parent(Generic[T]): ...
999
1000 reveal_type(Child()) # N: Revealed type is "__main__.Child[__main__.Parent[builtins.int]]"
1001
1002 [case testNonRecursiveTypeVarDefaultImportCycleClass]
1003 import exp
1004 [file exp.pyi]
1005 from typing import Generic, TypeVar
1006 import ind
1007
1008 T = TypeVar("T", bound=D, default=D)
1009 class F(Generic[T]):
1010 x: T
1011
1012 class D(E): ...
1013 class E: ...
1014
1015 [file ind.pyi]
1016 from exp import F
1017
1018 class Ind(F): ...
1019 x: Ind
1020 reveal_type(x.x) # N: Revealed type is "exp.D"
1021
1022 [case testNonRecursiveTypeVarDefaultImportCycleAlias]
1023 import exp
1024 [file exp.pyi]
1025 from typing import TypeVar
1026 import ind
1027
1028 T = TypeVar("T", bound=D, default=D)
1029 F = list[T]
1030
1031 class D(E): ...
1032 class E: ...
1033
1034 [file ind.pyi]
1035 from exp import F
1036
1037 Ind = list[F]
1038 x: Ind
1039 reveal_type(x) # N: Revealed type is "builtins.list[builtins.list[exp.D]]"
1040
1041 [case testRecursiveTypeVarDefaultClassAndAlias]
1042 from typing import Generic, TypeVar, Union
1043
1044 T = TypeVar("T", default="Pattern")
1045
1046 class Trait(Generic[T]):
1047 pass
1048
1049 class Pattern1(Trait):
1050 pass
1051
1052 Pattern = Union[Pattern1, None]
1053
1054 reveal_type(Trait()) # N: Revealed type is "__main__.Trait[__main__.Pattern1 | None]"
1055 [builtins fixtures/tuple.pyi]
1056
1057 [case testRecursiveTypeVarDefaultOnlyAlias]
1058 from typing import TypeVar
1059
1060 T = TypeVar("T", default="A")
1061
1062 A = list[T]
1063 a: A
1064 reveal_type(a) # N: Revealed type is "builtins.list[builtins.list[Any]]"
1065 [builtins fixtures/tuple.pyi]