@@ -360,10 +360,31 @@ def test_no_bivariant(self):
360360 with self .assertRaises (ValueError ):
361361 TypeVar ('T' , covariant = True , contravariant = True )
362362
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+
363376 def test_bad_var_substitution (self ):
364377 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 :
366385 with self .subTest (arg = arg ):
386+ with self .assertRaises (TypeError ):
387+ T .__typing_subst__ (arg )
367388 with self .assertRaises (TypeError ):
368389 List [T ][arg ]
369390 with self .assertRaises (TypeError ):
@@ -1110,8 +1131,7 @@ def test_var_substitution(self):
11101131 C2 = Callable [[KT , T ], VT ]
11111132 C3 = Callable [..., T ]
11121133 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 )])
11151135 self .assertEqual (C2 [int , float , str ], Callable [[int , float ], str ])
11161136 self .assertEqual (C3 [int ], Callable [..., int ])
11171137 self .assertEqual (C3 [NoReturn ], Callable [..., NoReturn ])
@@ -2696,7 +2716,10 @@ def test_all_repr_eq_any(self):
26962716 for obj in objs :
26972717 self .assertNotEqual (repr (obj ), '' )
26982718 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 ):
27002723 self .assertEqual (obj [Any ].__args__ , (Any ,))
27012724 if isinstance (obj , type ):
27022725 for base in obj .__mro__ :
@@ -5748,33 +5771,30 @@ class X(Generic[P, P2]):
57485771 self .assertEqual (G1 .__args__ , ((int , str ), (bytes ,)))
57495772 self .assertEqual (G2 .__args__ , ((int ,), (str , bytes )))
57505773
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+
57515785 def test_bad_var_substitution (self ):
57525786 T = TypeVar ('T' )
57535787 P = ParamSpec ('P' )
57545788 bad_args = (42 , int , None , T , int | str , Union [int , str ])
57555789 for arg in bad_args :
57565790 with self .subTest (arg = arg ):
5791+ with self .assertRaises (TypeError ):
5792+ P .__typing_subst__ (arg )
57575793 with self .assertRaises (TypeError ):
57585794 typing .Callable [P , T ][arg , str ]
57595795 with self .assertRaises (TypeError ):
57605796 collections .abc .Callable [P , T ][arg , str ]
57615797
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-
57785798 def test_paramspec_in_nested_generics (self ):
57795799 # Although ParamSpec should not be found in __parameters__ of most
57805800 # generics, they probably should be found when nested in
0 commit comments