@@ -6294,6 +6294,10 @@ def test_typing_extensions_defers_when_possible(self):
62946294 'AsyncGenerator' , 'ContextManager' , 'AsyncContextManager' ,
62956295 'ParamSpec' , 'TypeVar' , 'TypeVarTuple' , 'get_type_hints' ,
62966296 }
6297+ if sys .version_info < (3 , 14 ):
6298+ exclude |= {
6299+ 'TypeAliasType'
6300+ }
62976301 if not typing_extensions ._PEP_728_IMPLEMENTED :
62986302 exclude |= {'TypedDict' , 'is_typeddict' }
62996303 for item in typing_extensions .__all__ :
@@ -7504,6 +7508,80 @@ def test_no_instance_subclassing(self):
75047508 class MyAlias (TypeAliasType ):
75057509 pass
75067510
7511+ def test_type_var_compatibility (self ):
7512+ # Regression test to assure compatibility with typing variants
7513+ typingT = typing .TypeVar ('typingT' )
7514+ T1 = TypeAliasType ("TypingTypeVar" , ..., type_params = (typingT ,))
7515+ self .assertEqual (T1 .__type_params__ , (typingT ,))
7516+
7517+ # Test typing_extensions backports
7518+ textT = TypeVar ('textT' )
7519+ T2 = TypeAliasType ("TypingExtTypeVar" , ..., type_params = (textT ,))
7520+ self .assertEqual (T2 .__type_params__ , (textT ,))
7521+
7522+ textP = ParamSpec ("textP" )
7523+ T3 = TypeAliasType ("TypingExtParamSpec" , ..., type_params = (textP ,))
7524+ self .assertEqual (T3 .__type_params__ , (textP ,))
7525+
7526+ textTs = TypeVarTuple ("textTs" )
7527+ T4 = TypeAliasType ("TypingExtTypeVarTuple" , ..., type_params = (textTs ,))
7528+ self .assertEqual (T4 .__type_params__ , (textTs ,))
7529+
7530+ @skipUnless (TYPING_3_10_0 , "typing.ParamSpec is not available before 3.10" )
7531+ def test_param_spec_compatibility (self ):
7532+ # Regression test to assure compatibility with typing variant
7533+ typingP = typing .ParamSpec ("typingP" )
7534+ T5 = TypeAliasType ("TypingParamSpec" , ..., type_params = (typingP ,))
7535+ self .assertEqual (T5 .__type_params__ , (typingP ,))
7536+
7537+ @skipUnless (TYPING_3_12_0 , "typing.TypeVarTuple is not available before 3.12" )
7538+ def test_type_var_tuple_compatibility (self ):
7539+ # Regression test to assure compatibility with typing variant
7540+ typingTs = typing .TypeVarTuple ("typingTs" )
7541+ T6 = TypeAliasType ("TypingTypeVarTuple" , ..., type_params = (typingTs ,))
7542+ self .assertEqual (T6 .__type_params__ , (typingTs ,))
7543+
7544+ def test_type_params_possibilities (self ):
7545+ T = TypeVar ('T' )
7546+ # Test not a tuple
7547+ with self .assertRaisesRegex (TypeError , "type_params must be a tuple" ):
7548+ TypeAliasType ("InvalidTypeParams" , List [T ], type_params = [T ])
7549+
7550+ # Test default order and other invalid inputs
7551+ T_default = TypeVar ('T_default' , default = int )
7552+ Ts = TypeVarTuple ('Ts' )
7553+ Ts_default = TypeVarTuple ('Ts_default' , default = Unpack [Tuple [str , int ]])
7554+ P = ParamSpec ('P' )
7555+ P_default = ParamSpec ('P_default' , default = [str , int ])
7556+
7557+ # NOTE: PEP 696 states: "TypeVars with defaults cannot immediately follow TypeVarTuples"
7558+ # this is currently not enforced for the type statement and is not tested.
7559+ # PEP 695: Double usage of the same name is also not enforced and not tested.
7560+ valid_cases = [
7561+ (T , P , Ts ),
7562+ (T , Ts_default ),
7563+ (P_default , T_default ),
7564+ (P , T_default , Ts_default ),
7565+ (T_default , P_default , Ts_default ),
7566+ ]
7567+ invalid_cases = [
7568+ ((T_default , T ), f"non-default type parameter '{ T !r} ' follows default" ),
7569+ ((P_default , P ), f"non-default type parameter '{ P !r} ' follows default" ),
7570+ ((Ts_default , T ), f"non-default type parameter '{ T !r} ' follows default" ),
7571+ # Only type params are accepted
7572+ ((1 ,), "Expected a type param, got 1" ),
7573+ ((str ,), f"Expected a type param, got { str !r} " ),
7574+ # Unpack is not a TypeVar but isinstance(Unpack[Ts], TypeVar) is True in Python < 3.12
7575+ ((Unpack [Ts ],), f"Expected a type param, got { re .escape (repr (Unpack [Ts ]))} " ),
7576+ ]
7577+
7578+ for case in valid_cases :
7579+ with self .subTest (type_params = case ):
7580+ TypeAliasType ("OkCase" , List [T ], type_params = case )
7581+ for case , msg in invalid_cases :
7582+ with self .subTest (type_params = case ):
7583+ with self .assertRaisesRegex (TypeError , msg ):
7584+ TypeAliasType ("InvalidCase" , List [T ], type_params = case )
75077585
75087586class DocTests (BaseTestCase ):
75097587 def test_annotation (self ):
0 commit comments