@@ -239,3 +239,164 @@ def func_c4(
239
239
# reveal_type(b) # Revealed type is "__main__.ClassC4[builtins.int, builtins.str]" # TODO
240
240
reveal_type(c) # N: Revealed type is "__main__.ClassC4[builtins.int, builtins.float]"
241
241
[builtins fixtures/tuple.pyi]
242
+
243
+ [case testTypeVarDefaultsTypeAlias1]
244
+ # flags: --disallow-any-generics
245
+ from typing import Any, Dict, List, Tuple, TypeVar, Union
246
+
247
+ T1 = TypeVar("T1")
248
+ T2 = TypeVar("T2", default=int)
249
+ T3 = TypeVar("T3", default=str)
250
+ T4 = TypeVar("T4")
251
+
252
+ TA1 = Dict[T2, T3]
253
+
254
+ def func_a1(
255
+ a: TA1,
256
+ b: TA1[float],
257
+ c: TA1[float, float],
258
+ d: TA1[float, float, float], # E: Bad number of arguments for type alias, expected between 0 and 2, given: 3
259
+ ) -> None:
260
+ reveal_type(a) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"
261
+ reveal_type(b) # N: Revealed type is "builtins.dict[builtins.float, builtins.str]"
262
+ reveal_type(c) # N: Revealed type is "builtins.dict[builtins.float, builtins.float]"
263
+ reveal_type(d) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"
264
+
265
+ TA2 = Tuple[T1, T2, T3]
266
+
267
+ def func_a2(
268
+ a: TA2, # E: Missing type parameters for generic type "TA2"
269
+ b: TA2[float],
270
+ c: TA2[float, float],
271
+ d: TA2[float, float, float],
272
+ e: TA2[float, float, float, float], # E: Bad number of arguments for type alias, expected between 1 and 3, given: 4
273
+ ) -> None:
274
+ reveal_type(a) # N: Revealed type is "Tuple[Any, builtins.int, builtins.str]"
275
+ reveal_type(b) # N: Revealed type is "Tuple[builtins.float, builtins.int, builtins.str]"
276
+ reveal_type(c) # N: Revealed type is "Tuple[builtins.float, builtins.float, builtins.str]"
277
+ reveal_type(d) # N: Revealed type is "Tuple[builtins.float, builtins.float, builtins.float]"
278
+ reveal_type(e) # N: Revealed type is "Tuple[Any, builtins.int, builtins.str]"
279
+
280
+ TA3 = Union[Dict[T1, T2], List[T3]]
281
+
282
+ def func_a3(
283
+ a: TA3, # E: Missing type parameters for generic type "TA3"
284
+ b: TA3[float],
285
+ c: TA3[float, float],
286
+ d: TA3[float, float, float],
287
+ e: TA3[float, float, float, float], # E: Bad number of arguments for type alias, expected between 1 and 3, given: 4
288
+ ) -> None:
289
+ reveal_type(a) # N: Revealed type is "Union[builtins.dict[Any, builtins.int], builtins.list[builtins.str]]"
290
+ reveal_type(b) # N: Revealed type is "Union[builtins.dict[builtins.float, builtins.int], builtins.list[builtins.str]]"
291
+ reveal_type(c) # N: Revealed type is "Union[builtins.dict[builtins.float, builtins.float], builtins.list[builtins.str]]"
292
+ reveal_type(d) # N: Revealed type is "Union[builtins.dict[builtins.float, builtins.float], builtins.list[builtins.float]]"
293
+ reveal_type(e) # N: Revealed type is "Union[builtins.dict[Any, builtins.int], builtins.list[builtins.str]]"
294
+
295
+ TA4 = Tuple[T1, T4, T2]
296
+
297
+ def func_a4(
298
+ a: TA4, # E: Missing type parameters for generic type "TA4"
299
+ b: TA4[float], # E: Bad number of arguments for type alias, expected between 2 and 3, given: 1
300
+ c: TA4[float, float],
301
+ d: TA4[float, float, float],
302
+ e: TA4[float, float, float, float], # E: Bad number of arguments for type alias, expected between 2 and 3, given: 4
303
+ ) -> None:
304
+ reveal_type(a) # N: Revealed type is "Tuple[Any, Any, builtins.int]"
305
+ reveal_type(b) # N: Revealed type is "Tuple[Any, Any, builtins.int]"
306
+ reveal_type(c) # N: Revealed type is "Tuple[builtins.float, builtins.float, builtins.int]"
307
+ reveal_type(d) # N: Revealed type is "Tuple[builtins.float, builtins.float, builtins.float]"
308
+ reveal_type(e) # N: Revealed type is "Tuple[Any, Any, builtins.int]"
309
+ [builtins fixtures/dict.pyi]
310
+
311
+ [case testTypeVarDefaultsTypeAlias2]
312
+ # flags: --disallow-any-generics
313
+ from typing import Any, Generic, ParamSpec
314
+
315
+ P1 = ParamSpec("P1")
316
+ P2 = ParamSpec("P2", default=[int, str])
317
+ P3 = ParamSpec("P3", default=...)
318
+
319
+ class ClassB1(Generic[P2, P3]): ...
320
+ TB1 = ClassB1[P2, P3]
321
+
322
+ def func_b1(
323
+ a: TB1,
324
+ b: TB1[[float]],
325
+ c: TB1[[float], [float]],
326
+ d: TB1[[float], [float], [float]], # E: Bad number of arguments for type alias, expected between 0 and 2, given: 3
327
+ ) -> None:
328
+ reveal_type(a) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], [*Any, **Any]]"
329
+ reveal_type(b) # N: Revealed type is "__main__.ClassB1[[builtins.float], [*Any, **Any]]"
330
+ reveal_type(c) # N: Revealed type is "__main__.ClassB1[[builtins.float], [builtins.float]]"
331
+ reveal_type(d) # N: Revealed type is "__main__.ClassB1[[builtins.int, builtins.str], [*Any, **Any]]"
332
+
333
+ class ClassB2(Generic[P1, P2]): ...
334
+ TB2 = ClassB2[P1, P2]
335
+
336
+ def func_b2(
337
+ a: TB2, # E: Missing type parameters for generic type "TB2"
338
+ b: TB2[[float]],
339
+ c: TB2[[float], [float]],
340
+ d: TB2[[float], [float], [float]], # E: Bad number of arguments for type alias, expected between 1 and 2, given: 3
341
+ ) -> None:
342
+ reveal_type(a) # N: Revealed type is "__main__.ClassB2[Any, [builtins.int, builtins.str]]"
343
+ reveal_type(b) # N: Revealed type is "__main__.ClassB2[[builtins.float], [builtins.int, builtins.str]]"
344
+ reveal_type(c) # N: Revealed type is "__main__.ClassB2[[builtins.float], [builtins.float]]"
345
+ reveal_type(d) # N: Revealed type is "__main__.ClassB2[Any, [builtins.int, builtins.str]]"
346
+ [builtins fixtures/tuple.pyi]
347
+
348
+ [case testTypeVarDefaultsTypeAlias3]
349
+ # flags: --disallow-any-generics
350
+ from typing import Tuple, TypeVar
351
+ from typing_extensions import TypeVarTuple, Unpack
352
+
353
+ T1 = TypeVar("T1")
354
+ T3 = TypeVar("T3", default=str)
355
+
356
+ Ts1 = TypeVarTuple("Ts1")
357
+ Ts2 = TypeVarTuple("Ts2", default=Unpack[Tuple[int, str]])
358
+ Ts3 = TypeVarTuple("Ts3", default=Unpack[Tuple[float, ...]])
359
+ Ts4 = TypeVarTuple("Ts4", default=Unpack[Tuple[()]])
360
+
361
+ TC1 = Tuple[Unpack[Ts2]]
362
+
363
+ def func_c1(
364
+ a: TC1,
365
+ b: TC1[float],
366
+ ) -> None:
367
+ # reveal_type(a) # Revealed type is "Tuple[builtins.int, builtins.str]" # TODO
368
+ reveal_type(b) # N: Revealed type is "Tuple[builtins.float]"
369
+
370
+ TC2 = Tuple[T3, Unpack[Ts3]]
371
+
372
+ def func_c2(
373
+ a: TC2,
374
+ b: TC2[int],
375
+ c: TC2[int, Unpack[Tuple[()]]],
376
+ ) -> None:
377
+ # reveal_type(a) # Revealed type is "Tuple[builtins.str, Unpack[builtins.tuple[builtins.float, ...]]]" # TODO
378
+ # reveal_type(b) # Revealed type is "Tuple[builtins.int, Unpack[builtins.tuple[builtins.float, ...]]]" # TODO
379
+ reveal_type(c) # N: Revealed type is "Tuple[builtins.int]"
380
+
381
+ TC3 = Tuple[T3, Unpack[Ts4]]
382
+
383
+ def func_c3(
384
+ a: TC3,
385
+ b: TC3[int],
386
+ c: TC3[int, Unpack[Tuple[float]]],
387
+ ) -> None:
388
+ # reveal_type(a) # Revealed type is "Tuple[builtins.str]" # TODO
389
+ reveal_type(b) # N: Revealed type is "Tuple[builtins.int]"
390
+ reveal_type(c) # N: Revealed type is "Tuple[builtins.int, builtins.float]"
391
+
392
+ TC4 = Tuple[T1, Unpack[Ts1], T3]
393
+
394
+ def func_c4(
395
+ a: TC4, # E: Missing type parameters for generic type "TC4"
396
+ b: TC4[int],
397
+ c: TC4[int, float],
398
+ ) -> None:
399
+ reveal_type(a) # N: Revealed type is "Tuple[Any, Unpack[builtins.tuple[Any, ...]], builtins.str]"
400
+ # reveal_type(b) # Revealed type is "Tuple[builtins.int, builtins.str]" # TODO
401
+ reveal_type(c) # N: Revealed type is "Tuple[builtins.int, builtins.float]"
402
+ [builtins fixtures/tuple.pyi]
0 commit comments