@@ -360,10 +360,31 @@ def test_no_bivariant(self):
360
360
with self .assertRaises (ValueError ):
361
361
TypeVar ('T' , covariant = True , contravariant = True )
362
362
363
+ def test_var_substitution (self ):
364
+ T = TypeVar ('T' )
365
+ subst = T .__typing_subst__
366
+ self .assertIs (subst (int ), int )
367
+ self .assertEqual (subst (list [int ]), list [int ])
368
+ self .assertEqual (subst (List [int ]), List [int ])
369
+ self .assertEqual (subst (List ), List )
370
+ self .assertIs (subst (Any ), Any )
371
+ self .assertIs (subst (None ), type (None ))
372
+ self .assertIs (subst (T ), T )
373
+ self .assertEqual (subst (int | str ), int | str )
374
+ self .assertEqual (subst (Union [int , str ]), Union [int , str ])
375
+
363
376
def test_bad_var_substitution (self ):
364
377
T = TypeVar ('T' )
365
- for arg in (), (int , str ):
378
+ P = ParamSpec ("P" )
379
+ bad_args = (
380
+ 42 , ..., [int ], (), (int , str ), Union ,
381
+ Generic , Generic [T ], Protocol , Protocol [T ],
382
+ Final , Final [int ], ClassVar , ClassVar [int ],
383
+ )
384
+ for arg in bad_args :
366
385
with self .subTest (arg = arg ):
386
+ with self .assertRaises (TypeError ):
387
+ T .__typing_subst__ (arg )
367
388
with self .assertRaises (TypeError ):
368
389
List [T ][arg ]
369
390
with self .assertRaises (TypeError ):
@@ -1110,8 +1131,7 @@ def test_var_substitution(self):
1110
1131
C2 = Callable [[KT , T ], VT ]
1111
1132
C3 = Callable [..., T ]
1112
1133
self .assertEqual (C1 [str ], Callable [[int , str ], str ])
1113
- if Callable is typing .Callable :
1114
- self .assertEqual (C1 [None ], Callable [[int , type (None )], type (None )])
1134
+ self .assertEqual (C1 [None ], Callable [[int , type (None )], type (None )])
1115
1135
self .assertEqual (C2 [int , float , str ], Callable [[int , float ], str ])
1116
1136
self .assertEqual (C3 [int ], Callable [..., int ])
1117
1137
self .assertEqual (C3 [NoReturn ], Callable [..., NoReturn ])
@@ -2696,7 +2716,10 @@ def test_all_repr_eq_any(self):
2696
2716
for obj in objs :
2697
2717
self .assertNotEqual (repr (obj ), '' )
2698
2718
self .assertEqual (obj , obj )
2699
- if getattr (obj , '__parameters__' , None ) and len (obj .__parameters__ ) == 1 :
2719
+ if (getattr (obj , '__parameters__' , None )
2720
+ and not isinstance (obj , typing .TypeVar )
2721
+ and isinstance (obj .__parameters__ , tuple )
2722
+ and len (obj .__parameters__ ) == 1 ):
2700
2723
self .assertEqual (obj [Any ].__args__ , (Any ,))
2701
2724
if isinstance (obj , type ):
2702
2725
for base in obj .__mro__ :
@@ -5748,33 +5771,30 @@ class X(Generic[P, P2]):
5748
5771
self .assertEqual (G1 .__args__ , ((int , str ), (bytes ,)))
5749
5772
self .assertEqual (G2 .__args__ , ((int ,), (str , bytes )))
5750
5773
5774
+ def test_var_substitution (self ):
5775
+ T = TypeVar ("T" )
5776
+ P = ParamSpec ("P" )
5777
+ subst = P .__typing_subst__
5778
+ self .assertEqual (subst ((int , str )), (int , str ))
5779
+ self .assertEqual (subst ([int , str ]), (int , str ))
5780
+ self .assertEqual (subst ([None ]), (type (None ),))
5781
+ self .assertIs (subst (...), ...)
5782
+ self .assertIs (subst (P ), P )
5783
+ self .assertEqual (subst (Concatenate [int , P ]), Concatenate [int , P ])
5784
+
5751
5785
def test_bad_var_substitution (self ):
5752
5786
T = TypeVar ('T' )
5753
5787
P = ParamSpec ('P' )
5754
5788
bad_args = (42 , int , None , T , int | str , Union [int , str ])
5755
5789
for arg in bad_args :
5756
5790
with self .subTest (arg = arg ):
5791
+ with self .assertRaises (TypeError ):
5792
+ P .__typing_subst__ (arg )
5757
5793
with self .assertRaises (TypeError ):
5758
5794
typing .Callable [P , T ][arg , str ]
5759
5795
with self .assertRaises (TypeError ):
5760
5796
collections .abc .Callable [P , T ][arg , str ]
5761
5797
5762
- def test_no_paramspec_in__parameters__ (self ):
5763
- # ParamSpec should not be found in __parameters__
5764
- # of generics. Usages outside Callable, Concatenate
5765
- # and Generic are invalid.
5766
- T = TypeVar ("T" )
5767
- P = ParamSpec ("P" )
5768
- self .assertNotIn (P , List [P ].__parameters__ )
5769
- self .assertIn (T , Tuple [T , P ].__parameters__ )
5770
-
5771
- # Test for consistency with builtin generics.
5772
- self .assertNotIn (P , list [P ].__parameters__ )
5773
- self .assertIn (T , tuple [T , P ].__parameters__ )
5774
-
5775
- self .assertNotIn (P , (list [P ] | int ).__parameters__ )
5776
- self .assertIn (T , (tuple [T , P ] | int ).__parameters__ )
5777
-
5778
5798
def test_paramspec_in_nested_generics (self ):
5779
5799
# Although ParamSpec should not be found in __parameters__ of most
5780
5800
# generics, they probably should be found when nested in
0 commit comments