|
2 | 2 |
|
3 | 3 | from test.support import run_with_locale |
4 | 4 | import collections.abc |
| 5 | +from collections import namedtuple |
5 | 6 | import inspect |
6 | 7 | import pickle |
7 | 8 | import locale |
8 | 9 | import sys |
9 | 10 | import types |
10 | 11 | import unittest.mock |
11 | 12 | import weakref |
| 13 | +import typing |
| 14 | + |
| 15 | +class Example: |
| 16 | + pass |
| 17 | + |
| 18 | +class Forward: ... |
12 | 19 |
|
13 | 20 | class TypesTests(unittest.TestCase): |
14 | 21 |
|
@@ -598,6 +605,113 @@ def test_method_descriptor_types(self): |
598 | 605 | self.assertIsInstance(int.from_bytes, types.BuiltinMethodType) |
599 | 606 | self.assertIsInstance(int.__new__, types.BuiltinMethodType) |
600 | 607 |
|
| 608 | + def test_or_types_operator(self): |
| 609 | + self.assertEqual(int | str, typing.Union[int, str]) |
| 610 | + self.assertNotEqual(int | list, typing.Union[int, str]) |
| 611 | + self.assertEqual(str | int, typing.Union[int, str]) |
| 612 | + self.assertEqual(int | None, typing.Union[int, None]) |
| 613 | + self.assertEqual(None | int, typing.Union[int, None]) |
| 614 | + self.assertEqual(int | str | list, typing.Union[int, str, list]) |
| 615 | + self.assertEqual(int | (str | list), typing.Union[int, str, list]) |
| 616 | + self.assertEqual(str | (int | list), typing.Union[int, str, list]) |
| 617 | + self.assertEqual(typing.List | typing.Tuple, typing.Union[typing.List, typing.Tuple]) |
| 618 | + self.assertEqual(typing.List[int] | typing.Tuple[int], typing.Union[typing.List[int], typing.Tuple[int]]) |
| 619 | + self.assertEqual(typing.List[int] | None, typing.Union[typing.List[int], None]) |
| 620 | + self.assertEqual(None | typing.List[int], typing.Union[None, typing.List[int]]) |
| 621 | + self.assertEqual(str | float | int | complex | int, (int | str) | (float | complex)) |
| 622 | + self.assertEqual(typing.Union[str, int, typing.List[int]], str | int | typing.List[int]) |
| 623 | + self.assertEqual(int | int, int) |
| 624 | + self.assertEqual( |
| 625 | + BaseException | |
| 626 | + bool | |
| 627 | + bytes | |
| 628 | + complex | |
| 629 | + float | |
| 630 | + int | |
| 631 | + list | |
| 632 | + map | |
| 633 | + set, |
| 634 | + typing.Union[ |
| 635 | + BaseException, |
| 636 | + bool, |
| 637 | + bytes, |
| 638 | + complex, |
| 639 | + float, |
| 640 | + int, |
| 641 | + list, |
| 642 | + map, |
| 643 | + set, |
| 644 | + ]) |
| 645 | + with self.assertRaises(TypeError): |
| 646 | + int | 3 |
| 647 | + with self.assertRaises(TypeError): |
| 648 | + 3 | int |
| 649 | + with self.assertRaises(TypeError): |
| 650 | + Example() | int |
| 651 | + with self.assertRaises(TypeError): |
| 652 | + (int | str) < typing.Union[str, int] |
| 653 | + with self.assertRaises(TypeError): |
| 654 | + (int | str) < (int | bool) |
| 655 | + with self.assertRaises(TypeError): |
| 656 | + (int | str) <= (int | str) |
| 657 | + with self.assertRaises(TypeError): |
| 658 | + # Check that we don't crash if typing.Union does not have a tuple in __args__ |
| 659 | + x = typing.Union[str, int] |
| 660 | + x.__args__ = [str, int] |
| 661 | + (int | str ) == x |
| 662 | + |
| 663 | + def test_or_type_operator_with_TypeVar(self): |
| 664 | + TV = typing.TypeVar('T') |
| 665 | + assert TV | str == typing.Union[TV, str] |
| 666 | + assert str | TV == typing.Union[str, TV] |
| 667 | + |
| 668 | + def test_or_type_operator_with_forward(self): |
| 669 | + T = typing.TypeVar('T') |
| 670 | + ForwardAfter = T | 'Forward' |
| 671 | + ForwardBefore = 'Forward' | T |
| 672 | + def forward_after(x: ForwardAfter[int]) -> None: ... |
| 673 | + def forward_before(x: ForwardBefore[int]) -> None: ... |
| 674 | + assert typing.get_args(typing.get_type_hints(forward_after)['x']) == (int, Forward) |
| 675 | + assert typing.get_args(typing.get_type_hints(forward_before)['x']) == (int, Forward) |
| 676 | + |
| 677 | + def test_or_type_operator_with_Protocol(self): |
| 678 | + class Proto(typing.Protocol): |
| 679 | + def meth(self) -> int: |
| 680 | + ... |
| 681 | + assert Proto | str == typing.Union[Proto, str] |
| 682 | + |
| 683 | + def test_or_type_operator_with_Alias(self): |
| 684 | + assert list | str == typing.Union[list, str] |
| 685 | + assert typing.List | str == typing.Union[typing.List, str] |
| 686 | + |
| 687 | + def test_or_type_operator_with_NamedTuple(self): |
| 688 | + NT=namedtuple('A', ['B', 'C', 'D']) |
| 689 | + assert NT | str == typing.Union[NT,str] |
| 690 | + |
| 691 | + def test_or_type_operator_with_TypedDict(self): |
| 692 | + class Point2D(typing.TypedDict): |
| 693 | + x: int |
| 694 | + y: int |
| 695 | + label: str |
| 696 | + assert Point2D | str == typing.Union[Point2D, str] |
| 697 | + |
| 698 | + def test_or_type_operator_with_NewType(self): |
| 699 | + UserId = typing.NewType('UserId', int) |
| 700 | + assert UserId | str == typing.Union[UserId, str] |
| 701 | + |
| 702 | + def test_or_type_operator_with_IO(self): |
| 703 | + assert typing.IO | str == typing.Union[typing.IO, str] |
| 704 | + |
| 705 | + def test_or_type_operator_with_SpecialForm(self): |
| 706 | + assert typing.Any | str == typing.Union[typing.Any, str] |
| 707 | + assert typing.NoReturn | str == typing.Union[typing.NoReturn, str] |
| 708 | + assert typing.Optional[int] | str == typing.Union[typing.Optional[int], str] |
| 709 | + assert typing.Optional[int] | str == typing.Union[int, str, None] |
| 710 | + assert typing.Union[int, bool] | str == typing.Union[int, bool, str] |
| 711 | + |
| 712 | + def test_or_type_repr(self): |
| 713 | + assert repr(int | None) == "int | None" |
| 714 | + assert repr(int | typing.GenericAlias(list, int)) == "int | list[int]" |
601 | 715 |
|
602 | 716 | class MappingProxyTests(unittest.TestCase): |
603 | 717 | mappingproxy = types.MappingProxyType |
|
0 commit comments