@@ -255,6 +255,15 @@ def test_no_bivariant(self):
255
255
with self .assertRaises (ValueError ):
256
256
TypeVar ('T' , covariant = True , contravariant = True )
257
257
258
+ def test_bad_var_substitution (self ):
259
+ T = TypeVar ('T' )
260
+ for arg in (), (int , str ):
261
+ with self .subTest (arg = arg ):
262
+ with self .assertRaises (TypeError ):
263
+ List [T ][arg ]
264
+ with self .assertRaises (TypeError ):
265
+ list [T ][arg ]
266
+
258
267
259
268
class UnionTests (BaseTestCase ):
260
269
@@ -568,8 +577,11 @@ def test_var_substitution(self):
568
577
C2 = Callable [[KT , T ], VT ]
569
578
C3 = Callable [..., T ]
570
579
self .assertEqual (C1 [str ], Callable [[int , str ], str ])
580
+ if Callable is typing .Callable :
581
+ self .assertEqual (C1 [None ], Callable [[int , type (None )], type (None )])
571
582
self .assertEqual (C2 [int , float , str ], Callable [[int , float ], str ])
572
583
self .assertEqual (C3 [int ], Callable [..., int ])
584
+ self .assertEqual (C3 [NoReturn ], Callable [..., NoReturn ])
573
585
574
586
# multi chaining
575
587
C4 = C2 [int , VT , str ]
@@ -4981,6 +4993,17 @@ class X(Generic[P, P2]):
4981
4993
self .assertEqual (G1 .__args__ , ((int , str ), (bytes ,)))
4982
4994
self .assertEqual (G2 .__args__ , ((int ,), (str , bytes )))
4983
4995
4996
+ def test_bad_var_substitution (self ):
4997
+ T = TypeVar ('T' )
4998
+ P = ParamSpec ('P' )
4999
+ bad_args = (42 , int , None , T , int | str , Union [int , str ])
5000
+ for arg in bad_args :
5001
+ with self .subTest (arg = arg ):
5002
+ with self .assertRaises (TypeError ):
5003
+ typing .Callable [P , T ][arg , str ]
5004
+ with self .assertRaises (TypeError ):
5005
+ collections .abc .Callable [P , T ][arg , str ]
5006
+
4984
5007
def test_no_paramspec_in__parameters__ (self ):
4985
5008
# ParamSpec should not be found in __parameters__
4986
5009
# of generics. Usages outside Callable, Concatenate
@@ -5010,6 +5033,10 @@ def test_paramspec_in_nested_generics(self):
5010
5033
self .assertEqual (G1 .__parameters__ , (P , T ))
5011
5034
self .assertEqual (G2 .__parameters__ , (P , T ))
5012
5035
self .assertEqual (G3 .__parameters__ , (P , T ))
5036
+ C = Callable [[int , str ], float ]
5037
+ self .assertEqual (G1 [[int , str ], float ], List [C ])
5038
+ self .assertEqual (G2 [[int , str ], float ], list [C ])
5039
+ self .assertEqual (G3 [[int , str ], float ], list [C ] | int )
5013
5040
5014
5041
5015
5042
class ConcatenateTests (BaseTestCase ):
0 commit comments