|
21 | 21 | import typing
|
22 | 22 | from typing import TypeVar, Optional, Union, AnyStr
|
23 | 23 | from typing import T, KT, VT # Not in __all__.
|
24 |
| -from typing import Tuple, List, Dict, Iterable, Iterator, Callable |
| 24 | +from typing import Tuple, List, Set, Dict, Iterable, Iterator, Callable |
25 | 25 | from typing import Generic
|
26 | 26 | from typing import no_type_check
|
27 | 27 | import warnings
|
|
35 | 35 | from typing_extensions import assert_type, get_type_hints, get_origin, get_args, get_original_bases
|
36 | 36 | from typing_extensions import clear_overloads, get_overloads, overload
|
37 | 37 | from typing_extensions import NamedTuple
|
38 |
| -from typing_extensions import override, deprecated, Buffer |
| 38 | +from typing_extensions import override, deprecated, Buffer, TypeAliasType |
39 | 39 | from _typed_dict_test_helper import Foo, FooGeneric
|
40 | 40 |
|
41 | 41 | # Flags used to mark tests that only apply after a specific
|
@@ -4553,5 +4553,82 @@ class GenericTypedDict(TypedDict, Generic[T]):
|
4553 | 4553 | )
|
4554 | 4554 |
|
4555 | 4555 |
|
| 4556 | +class TypeAliasTypeTests(BaseTestCase): |
| 4557 | + def test_attributes(self): |
| 4558 | + Simple = TypeAliasType("Simple", int) |
| 4559 | + self.assertEqual(Simple.__name__, "Simple") |
| 4560 | + self.assertIs(Simple.__value__, int) |
| 4561 | + self.assertEqual(Simple.__type_params__, ()) |
| 4562 | + self.assertEqual(Simple.__parameters__, ()) |
| 4563 | + |
| 4564 | + T = TypeVar("T") |
| 4565 | + ListOrSetT = TypeAliasType("ListOrSetT", Union[List[T], Set[T]], type_params=(T,)) |
| 4566 | + self.assertEqual(ListOrSetT.__name__, "ListOrSetT") |
| 4567 | + self.assertEqual(ListOrSetT.__value__, Union[List[T], Set[T]]) |
| 4568 | + self.assertEqual(ListOrSetT.__type_params__, (T,)) |
| 4569 | + self.assertEqual(ListOrSetT.__parameters__, (T,)) |
| 4570 | + |
| 4571 | + Ts = TypeVarTuple("Ts") |
| 4572 | + Variadic = TypeAliasType("Variadic", Tuple[int, Unpack[Ts]], type_params=(Ts,)) |
| 4573 | + self.assertEqual(Variadic.__name__, "Variadic") |
| 4574 | + self.assertEqual(Variadic.__value__, Tuple[int, Unpack[Ts]]) |
| 4575 | + self.assertEqual(Variadic.__type_params__, (Ts,)) |
| 4576 | + self.assertEqual(Variadic.__parameters__, tuple(iter(Ts))) |
| 4577 | + |
| 4578 | + def test_immutable(self): |
| 4579 | + Simple = TypeAliasType("Simple", int) |
| 4580 | + with self.assertRaisesRegex(AttributeError, "Can't set attribute"): |
| 4581 | + Simple.__name__ = "NewName" |
| 4582 | + with self.assertRaisesRegex(AttributeError, "Can't set attribute"): |
| 4583 | + Simple.__value__ = str |
| 4584 | + with self.assertRaisesRegex(AttributeError, "Can't set attribute"): |
| 4585 | + Simple.__type_params__ = (T,) |
| 4586 | + with self.assertRaisesRegex(AttributeError, "Can't set attribute"): |
| 4587 | + Simple.__parameters__ = (T,) |
| 4588 | + with self.assertRaisesRegex(AttributeError, "Can't set attribute"): |
| 4589 | + Simple.some_attribute = "not allowed" |
| 4590 | + with self.assertRaisesRegex(AttributeError, "Can't delete attribute"): |
| 4591 | + del Simple.__name__ |
| 4592 | + with self.assertRaisesRegex(AttributeError, "Can't delete attribute"): |
| 4593 | + del Simple.nonexistent_attribute |
| 4594 | + |
| 4595 | + def test_or(self): |
| 4596 | + Alias = TypeAliasType("Alias", int) |
| 4597 | + if sys.version_info >= (3, 10): |
| 4598 | + self.assertEqual(Alias | "Ref", Union[Alias, typing.ForwardRef("Ref")]) |
| 4599 | + else: |
| 4600 | + with self.assertRaises(TypeError): |
| 4601 | + Alias | "Ref" |
| 4602 | + |
| 4603 | + def test_getitem(self): |
| 4604 | + ListOrSetT = TypeAliasType("ListOrSetT", Union[List[T], Set[T]], type_params=(T,)) |
| 4605 | + subscripted = ListOrSetT[int] |
| 4606 | + self.assertEqual(get_args(subscripted), (int,)) |
| 4607 | + self.assertIs(get_origin(subscripted), ListOrSetT) |
| 4608 | + with self.assertRaises(TypeError): |
| 4609 | + subscripted[str] |
| 4610 | + |
| 4611 | + still_generic = ListOrSetT[Iterable[T]] |
| 4612 | + self.assertEqual(get_args(still_generic), (Iterable[T],)) |
| 4613 | + self.assertIs(get_origin(still_generic), ListOrSetT) |
| 4614 | + fully_subscripted = still_generic[float] |
| 4615 | + self.assertEqual(get_args(fully_subscripted), (Iterable[float],)) |
| 4616 | + self.assertIs(get_origin(fully_subscripted), ListOrSetT) |
| 4617 | + |
| 4618 | + def test_pickle(self): |
| 4619 | + global Alias |
| 4620 | + Alias = TypeAliasType("Alias", int) |
| 4621 | + for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 4622 | + with self.subTest(proto=proto): |
| 4623 | + pickled = pickle.dumps(Alias, proto) |
| 4624 | + unpickled = pickle.loads(pickled) |
| 4625 | + self.assertIs(unpickled, Alias) |
| 4626 | + |
| 4627 | + def test_no_instance_subclassing(self): |
| 4628 | + with self.assertRaises(TypeError): |
| 4629 | + class MyAlias(TypeAliasType): |
| 4630 | + pass |
| 4631 | + |
| 4632 | + |
4556 | 4633 | if __name__ == '__main__':
|
4557 | 4634 | main()
|
0 commit comments