@@ -422,33 +422,83 @@ main: note: In member "f" of class "A":
422
422
-- ----------------------------
423
423
424
424
425
- [case testInvalidTypeApplicationTarget ]
425
+ [case testTypeApplicationArgs ]
426
426
from typing import TypeVar, Generic
427
427
T = TypeVar('T')
428
- class A(Generic[T]): pass
429
- A[A]() # E: Generic type is prohibited as a runtime expression (use a type alias or '# type:' comment)
430
- A[int, str]() # E: Generic type is prohibited as a runtime expression (use a type alias or '# type:' comment)
428
+ class Node(Generic[T]):
429
+ def __init__(self, x: T) -> None:
430
+ ...
431
+ Node[int]() # E: Too few arguments for "Node"
432
+ Node[int](1, 1, 1) # E: Too many arguments for "Node"
431
433
[out]
432
434
433
- [case testInvalidTypeApplicationTarget2 ]
435
+ [case testTypeApplicationTvars ]
434
436
from typing import TypeVar, Generic
435
437
T = TypeVar('T')
436
438
S = TypeVar('S')
437
439
class A(Generic[T, S]): pass
438
- A[int, str ]() # E: Generic type is prohibited as a runtime expression (use a type alias or '# type:' comment )
439
- A[int]() # E: Generic type is prohibited as a runtime expression (use a type alias or '# type:' comment )
440
+ A[int]() # E: Type application has too few types (2 expected )
441
+ A[int, str, int ]() # E: Type application has too many types (2 expected )
440
442
[out]
441
443
442
- [case testInvalidTypeApplicationTarget3]
443
-
444
+ [case testInvalidTypeApplicationType]
444
445
a = None # type: A
445
446
class A: pass
446
- a[A]() # Fail
447
- A[A]() # Fail
447
+ a[A]() # E: Value of type "A" is not indexable
448
+ A[A]() # E: Type application targets a non-generic function or class
448
449
[out]
449
- main:4: error: Value of type "A" is not indexable
450
- main:5: error: Generic type is prohibited as a runtime expression (use a type alias or '# type:' comment)
451
450
451
+ [case testTypeApplicationArgTypes]
452
+ from typing import TypeVar, Generic
453
+ T = TypeVar('T')
454
+ class Node(Generic[T]):
455
+ def __init__(self, x: T) -> None:
456
+ ...
457
+
458
+ Node[int](1)
459
+ Node[int]('a') # E: Argument 1 to "Node" has incompatible type "str"; expected "int"
460
+
461
+ class Dummy(Generic[T]):
462
+ def meth(self, x: T) -> None:
463
+ ...
464
+ def methout(self) -> T:
465
+ ...
466
+
467
+ Dummy[int]().meth(1)
468
+ Dummy[int]().meth('a') # E: Argument 1 to "meth" of "Dummy" has incompatible type "str"; expected "int"
469
+ reveal_type(Dummy[int]()) # E: Revealed type is '__main__.Dummy[builtins.int*]'
470
+ reveal_type(Dummy[int]().methout()) # E: Revealed type is 'builtins.int*'
471
+ [out]
472
+
473
+ [case testTypeApplicationArgTypesSubclasses]
474
+ from typing import TypeVar, Generic
475
+ T = TypeVar('T')
476
+ S = TypeVar('S')
477
+ class C(Generic[T, S]):
478
+ def __init__(self, x: T, y: S) -> None:
479
+ ...
480
+
481
+ class D(C[int, T], Generic[T]): ...
482
+
483
+ D[str](1, 'a')
484
+ D[str](1, 1) # E: Argument 2 to "D" has incompatible type "int"; expected "str"
485
+
486
+ class E(D[str]): ...
487
+ E(1, 'a')
488
+ E(1, 1) # E: Argument 2 to "E" has incompatible type "int"; expected "str"
489
+ [out]
490
+
491
+ [case testTypeApplicationAlias]
492
+ from typing import TypeVar, Generic
493
+ T = TypeVar('T')
494
+ class Node(Generic[T]):
495
+ def __init__(self, x: T) -> None:
496
+ ...
497
+
498
+ Alias = Node
499
+ Alias[int](1)
500
+ Alias[int]("a") # E: Argument 1 to "Node" has incompatible type "str"; expected "int"
501
+ [out]
452
502
453
503
-- Multiple assignment with lists
454
504
-- ------------------------------
0 commit comments