@@ -6321,6 +6321,10 @@ def test_typing_extensions_defers_when_possible(self):
63216321 'AsyncGenerator' , 'ContextManager' , 'AsyncContextManager' ,
63226322 'ParamSpec' , 'TypeVar' , 'TypeVarTuple' , 'get_type_hints' ,
63236323 }
6324+ if sys .version_info < (3 , 14 ):
6325+ exclude |= {
6326+ 'TypeAliasType'
6327+ }
63246328 if not typing_extensions ._PEP_728_IMPLEMENTED :
63256329 exclude |= {'TypedDict' , 'is_typeddict' }
63266330 for item in typing_extensions .__all__ :
@@ -7531,6 +7535,80 @@ def test_no_instance_subclassing(self):
75317535 class MyAlias (TypeAliasType ):
75327536 pass
75337537
7538+ def test_type_var_compatibility (self ):
7539+ # Regression test to assure compatibility with typing variants
7540+ typingT = typing .TypeVar ('typingT' )
7541+ T1 = TypeAliasType ("TypingTypeVar" , ..., type_params = (typingT ,))
7542+ self .assertEqual (T1 .__type_params__ , (typingT ,))
7543+
7544+ # Test typing_extensions backports
7545+ textT = TypeVar ('textT' )
7546+ T2 = TypeAliasType ("TypingExtTypeVar" , ..., type_params = (textT ,))
7547+ self .assertEqual (T2 .__type_params__ , (textT ,))
7548+
7549+ textP = ParamSpec ("textP" )
7550+ T3 = TypeAliasType ("TypingExtParamSpec" , ..., type_params = (textP ,))
7551+ self .assertEqual (T3 .__type_params__ , (textP ,))
7552+
7553+ textTs = TypeVarTuple ("textTs" )
7554+ T4 = TypeAliasType ("TypingExtTypeVarTuple" , ..., type_params = (textTs ,))
7555+ self .assertEqual (T4 .__type_params__ , (textTs ,))
7556+
7557+ @skipUnless (TYPING_3_10_0 , "typing.ParamSpec is not available before 3.10" )
7558+ def test_param_spec_compatibility (self ):
7559+ # Regression test to assure compatibility with typing variant
7560+ typingP = typing .ParamSpec ("typingP" )
7561+ T5 = TypeAliasType ("TypingParamSpec" , ..., type_params = (typingP ,))
7562+ self .assertEqual (T5 .__type_params__ , (typingP ,))
7563+
7564+ @skipUnless (TYPING_3_12_0 , "typing.TypeVarTuple is not available before 3.12" )
7565+ def test_type_var_tuple_compatibility (self ):
7566+ # Regression test to assure compatibility with typing variant
7567+ typingTs = typing .TypeVarTuple ("typingTs" )
7568+ T6 = TypeAliasType ("TypingTypeVarTuple" , ..., type_params = (typingTs ,))
7569+ self .assertEqual (T6 .__type_params__ , (typingTs ,))
7570+
7571+ def test_type_params_possibilities (self ):
7572+ T = TypeVar ('T' )
7573+ # Test not a tuple
7574+ with self .assertRaisesRegex (TypeError , "type_params must be a tuple" ):
7575+ TypeAliasType ("InvalidTypeParams" , List [T ], type_params = [T ])
7576+
7577+ # Test default order and other invalid inputs
7578+ T_default = TypeVar ('T_default' , default = int )
7579+ Ts = TypeVarTuple ('Ts' )
7580+ Ts_default = TypeVarTuple ('Ts_default' , default = Unpack [Tuple [str , int ]])
7581+ P = ParamSpec ('P' )
7582+ P_default = ParamSpec ('P_default' , default = [str , int ])
7583+
7584+ # NOTE: PEP 696 states: "TypeVars with defaults cannot immediately follow TypeVarTuples"
7585+ # this is currently not enforced for the type statement and is not tested.
7586+ # PEP 695: Double usage of the same name is also not enforced and not tested.
7587+ valid_cases = [
7588+ (T , P , Ts ),
7589+ (T , Ts_default ),
7590+ (P_default , T_default ),
7591+ (P , T_default , Ts_default ),
7592+ (T_default , P_default , Ts_default ),
7593+ ]
7594+ invalid_cases = [
7595+ ((T_default , T ), f"non-default type parameter '{ T !r} ' follows default" ),
7596+ ((P_default , P ), f"non-default type parameter '{ P !r} ' follows default" ),
7597+ ((Ts_default , T ), f"non-default type parameter '{ T !r} ' follows default" ),
7598+ # Only type params are accepted
7599+ ((1 ,), "Expected a type param, got 1" ),
7600+ ((str ,), f"Expected a type param, got { str !r} " ),
7601+ # Unpack is not a TypeVar but isinstance(Unpack[Ts], TypeVar) is True in Python < 3.12
7602+ ((Unpack [Ts ],), f"Expected a type param, got { re .escape (repr (Unpack [Ts ]))} " ),
7603+ ]
7604+
7605+ for case in valid_cases :
7606+ with self .subTest (type_params = case ):
7607+ TypeAliasType ("OkCase" , List [T ], type_params = case )
7608+ for case , msg in invalid_cases :
7609+ with self .subTest (type_params = case ):
7610+ with self .assertRaisesRegex (TypeError , msg ):
7611+ TypeAliasType ("InvalidCase" , List [T ], type_params = case )
75347612
75357613class DocTests (BaseTestCase ):
75367614 def test_annotation (self ):
0 commit comments