Skip to content

Commit f2aa35c

Browse files
committed
Added general and comprehensive test
1 parent af0a133 commit f2aa35c

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/test_typing_extensions.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7214,6 +7214,76 @@ def test_attributes_from_origin_3_12_plus(self):
72147214
self.assertEqual(fully_subscripted.__value__, Union[List[T], Set[T]],)
72157215
self.assertEqual(fully_subscripted.__type_params__, (T, ))
72167216

7217+
def test_alias_types_and_substitutions(self):
7218+
T = TypeVar('T')
7219+
T2 = TypeVar('T2')
7220+
T_default = TypeVar("T_default", default=int)
7221+
Ts = TypeVarTuple("Ts")
7222+
P = ParamSpec('P')
7223+
7224+
test_argument_cases = {
7225+
# arguments : expected parameters
7226+
int : (),
7227+
... : (),
7228+
T2 : (T2,),
7229+
Union[int, List[T2]] : (T2,),
7230+
Ts : (Ts,),
7231+
Tuple[int, str] : (),
7232+
Tuple[T, T_default, T2] : (T, T_default, T2),
7233+
Tuple[Unpack[Ts]] : (Ts,),
7234+
Callable[[Unpack[Ts]], T2] : (Ts, T2),
7235+
Callable[P, T2] : (P, T2),
7236+
Callable[Concatenate[T2, P], T_default] : (T2, P, T_default),
7237+
TypeAliasType("NestedAlias", List[T], type_params=(T,))[T2] : (T2,),
7238+
}
7239+
# currently a limitation, these args are no longer unpacked in 3.11
7240+
test_argument_cases_311_plus = {
7241+
Unpack[Ts] : (Ts,),
7242+
Unpack[Tuple[int, T2]] : (T2,),
7243+
Concatenate[int, P] : (P,),
7244+
}
7245+
test_argument_cases.update(test_argument_cases_311_plus)
7246+
7247+
test_alias_cases = [
7248+
# Simple cases
7249+
TypeAliasType("ListT", List[T], type_params=(T,)),
7250+
TypeAliasType("UnionT", Union[int, List[T]], type_params=(T,)),
7251+
# Either value or type_params contain generic
7252+
TypeAliasType("ValueWithoutT", int, type_params=(T,)),
7253+
TypeAliasType("ValueTNoParams", List[T], type_params=()),
7254+
# Callable
7255+
TypeAliasType("CallableP", Callable[P, Any], type_params=(P, )),
7256+
TypeAliasType("CallableT", Callable[..., T], type_params=(T, )),
7257+
TypeAliasType("CallableTs", Callable[[Unpack[Ts]], Any], type_params=(Ts, )),
7258+
# TypeVarTuple
7259+
TypeAliasType("Variadic", Tuple[int, Unpack[Ts]], type_params=(Ts,)),
7260+
# TypeVar with default
7261+
TypeAliasType("TupleT_default", Tuple[T_default, T], type_params=(T, T_default)),
7262+
TypeAliasType("CallableT_default", Callable[[T], T_default], type_params=(T, T_default)),
7263+
# default order reversed
7264+
TypeAliasType("TupleT_default_reversed", Tuple[T_default, T], type_params=(T_default, T)),
7265+
TypeAliasType("CallableT_default_reversed", Callable[[T], T_default], type_params=(T_default, T)),
7266+
]
7267+
7268+
for alias in test_alias_cases:
7269+
with self.subTest(alias=alias, args=[]):
7270+
subscripted = alias[[]]
7271+
self.assertEqual(get_args(subscripted), ([],))
7272+
self.assertEqual(subscripted.__parameters__, ())
7273+
with self.subTest(alias=alias, args=()):
7274+
subscripted = alias[()]
7275+
self.assertEqual(get_args(subscripted), ())
7276+
self.assertEqual(subscripted.__parameters__, ())
7277+
with self.subTest(alias=alias, args=(int, float)):
7278+
subscripted = alias[int, float]
7279+
self.assertEqual(get_args(subscripted), (int, float))
7280+
self.assertEqual(subscripted.__parameters__, ())
7281+
for expected_args, expected_parameters in test_argument_cases.items():
7282+
with self.subTest(alias=alias, args=expected_args):
7283+
if expected_args in test_argument_cases_311_plus and sys.version_info < (3, 11):
7284+
self.skipTest("args are unpacked before 3.11")
7285+
self.assertEqual(get_args(alias[expected_args]), (expected_args,))
7286+
self.assertEqual(alias[expected_args].__parameters__, expected_parameters)
72177287

72187288
def test_cannot_set_attributes(self):
72197289
Simple = TypeAliasType("Simple", int)

0 commit comments

Comments
 (0)