@@ -6194,6 +6194,10 @@ def test_typing_extensions_defers_when_possible(self):
61946194 'AsyncGenerator' , 'ContextManager' , 'AsyncContextManager' ,
61956195 'ParamSpec' , 'TypeVar' , 'TypeVarTuple' , 'get_type_hints' ,
61966196 }
6197+ if sys .version_info < (3 , 14 ):
6198+ exclude |= {
6199+ 'TypeAliasType'
6200+ }
61976201 if not typing_extensions ._PEP_728_IMPLEMENTED :
61986202 exclude |= {'TypedDict' , 'is_typeddict' }
61996203 for item in typing_extensions .__all__ :
@@ -7404,6 +7408,80 @@ def test_no_instance_subclassing(self):
74047408 class MyAlias (TypeAliasType ):
74057409 pass
74067410
7411+ def test_type_var_compatibility (self ):
7412+ # Regression test to assure compatibility with typing variants
7413+ typingT = typing .TypeVar ('typingT' )
7414+ T1 = TypeAliasType ("TypingTypeVar" , ..., type_params = (typingT ,))
7415+ self .assertEqual (T1 .__type_params__ , (typingT ,))
7416+
7417+ # Test typing_extensions backports
7418+ textT = TypeVar ('textT' )
7419+ T2 = TypeAliasType ("TypingExtTypeVar" , ..., type_params = (textT ,))
7420+ self .assertEqual (T2 .__type_params__ , (textT ,))
7421+
7422+ textP = ParamSpec ("textP" )
7423+ T3 = TypeAliasType ("TypingExtParamSpec" , ..., type_params = (textP ,))
7424+ self .assertEqual (T3 .__type_params__ , (textP ,))
7425+
7426+ textTs = TypeVarTuple ("textTs" )
7427+ T4 = TypeAliasType ("TypingExtTypeVarTuple" , ..., type_params = (textTs ,))
7428+ self .assertEqual (T4 .__type_params__ , (textTs ,))
7429+
7430+ @skipUnless (TYPING_3_10_0 , "typing.ParamSpec is not available before 3.10" )
7431+ def test_param_spec_compatibility (self ):
7432+ # Regression test to assure compatibility with typing variant
7433+ typingP = typing .ParamSpec ("typingP" )
7434+ T5 = TypeAliasType ("TypingParamSpec" , ..., type_params = (typingP ,))
7435+ self .assertEqual (T5 .__type_params__ , (typingP ,))
7436+
7437+ @skipUnless (TYPING_3_12_0 , "typing.TypeVarTuple is not available before 3.12" )
7438+ def test_type_var_tuple_compatibility (self ):
7439+ # Regression test to assure compatibility with typing variant
7440+ typingTs = typing .TypeVarTuple ("typingTs" )
7441+ T6 = TypeAliasType ("TypingTypeVarTuple" , ..., type_params = (typingTs ,))
7442+ self .assertEqual (T6 .__type_params__ , (typingTs ,))
7443+
7444+ def test_type_params_possibilities (self ):
7445+ T = TypeVar ('T' )
7446+ # Test not a tuple
7447+ with self .assertRaisesRegex (TypeError , "type_params must be a tuple" ):
7448+ TypeAliasType ("InvalidTypeParams" , List [T ], type_params = [T ])
7449+
7450+ # Test default order and other invalid inputs
7451+ T_default = TypeVar ('T_default' , default = int )
7452+ Ts = TypeVarTuple ('Ts' )
7453+ Ts_default = TypeVarTuple ('Ts_default' , default = Unpack [Tuple [str , int ]])
7454+ P = ParamSpec ('P' )
7455+ P_default = ParamSpec ('P_default' , default = [str , int ])
7456+
7457+ # NOTE: PEP 696 states: "TypeVars with defaults cannot immediately follow TypeVarTuples"
7458+ # this is currently not enforced for the type statement and is not tested.
7459+ # PEP 695: Double usage of the same name is also not enforced and not tested.
7460+ valid_cases = [
7461+ (T , P , Ts ),
7462+ (T , Ts_default ),
7463+ (P_default , T_default ),
7464+ (P , T_default , Ts_default ),
7465+ (T_default , P_default , Ts_default ),
7466+ ]
7467+ invalid_cases = [
7468+ ((T_default , T ), f"non-default type parameter '{ T !r} ' follows default" ),
7469+ ((P_default , P ), f"non-default type parameter '{ P !r} ' follows default" ),
7470+ ((Ts_default , T ), f"non-default type parameter '{ T !r} ' follows default" ),
7471+ # Only type params are accepted
7472+ ((1 ,), "Expected a type param, got 1" ),
7473+ ((str ,), f"Expected a type param, got { str !r} " ),
7474+ # Unpack is not a TypeVar but isinstance(Unpack[Ts], TypeVar) is True in Python < 3.12
7475+ ((Unpack [Ts ],), f"Expected a type param, got { re .escape (repr (Unpack [Ts ]))} " ),
7476+ ]
7477+
7478+ for case in valid_cases :
7479+ with self .subTest (type_params = case ):
7480+ TypeAliasType ("OkCase" , List [T ], type_params = case )
7481+ for case , msg in invalid_cases :
7482+ with self .subTest (type_params = case ):
7483+ with self .assertRaisesRegex (TypeError , msg ):
7484+ TypeAliasType ("InvalidCase" , List [T ], type_params = case )
74077485
74087486class DocTests (BaseTestCase ):
74097487 def test_annotation (self ):
0 commit comments