summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2023-04-24 16:55:16 (GMT)
committerGitHub <noreply@github.com>2023-04-24 16:55:16 (GMT)
commit3d29edaf0a5f2e12e7f72552f602c761ddaf0d13 (patch)
treee7745a0ea6aef935cd0e8acc3e40e9fa210252d4
parentd2745fe850d7b1959843c3fd3d284aa14956cd9e (diff)
downloadcpython-3d29edaf0a5f2e12e7f72552f602c761ddaf0d13.zip
cpython-3d29edaf0a5f2e12e7f72552f602c761ddaf0d13.tar.gz
cpython-3d29edaf0a5f2e12e7f72552f602c761ddaf0d13.tar.bz2
gh-103746: Test `types.UnionType` and `Literal` types together (#103747)
-rw-r--r--Lib/test/test_types.py29
-rw-r--r--Lib/test/test_typing.py5
2 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 9fe5812..8954810 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -925,6 +925,35 @@ class UnionTests(unittest.TestCase):
assert typing.Optional[int] | str == typing.Union[int, str, None]
assert typing.Union[int, bool] | str == typing.Union[int, bool, str]
+ def test_or_type_operator_with_Literal(self):
+ Literal = typing.Literal
+ self.assertEqual((Literal[1] | Literal[2]).__args__,
+ (Literal[1], Literal[2]))
+
+ self.assertEqual((Literal[0] | Literal[False]).__args__,
+ (Literal[0], Literal[False]))
+ self.assertEqual((Literal[1] | Literal[True]).__args__,
+ (Literal[1], Literal[True]))
+
+ self.assertEqual(Literal[1] | Literal[1], Literal[1])
+ self.assertEqual(Literal['a'] | Literal['a'], Literal['a'])
+
+ import enum
+ class Ints(enum.IntEnum):
+ A = 0
+ B = 1
+
+ self.assertEqual(Literal[Ints.A] | Literal[Ints.A], Literal[Ints.A])
+ self.assertEqual(Literal[Ints.B] | Literal[Ints.B], Literal[Ints.B])
+
+ self.assertEqual((Literal[Ints.B] | Literal[Ints.A]).__args__,
+ (Literal[Ints.B], Literal[Ints.A]))
+
+ self.assertEqual((Literal[0] | Literal[Ints.A]).__args__,
+ (Literal[0], Literal[Ints.A]))
+ self.assertEqual((Literal[1] | Literal[Ints.B]).__args__,
+ (Literal[1], Literal[Ints.B]))
+
def test_or_type_repr(self):
assert repr(int | str) == "int | str"
assert repr((int | str) | list) == "int | str | list"
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index b78ce1e..f36bb95 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -1805,6 +1805,11 @@ class UnionTests(BaseTestCase):
A = 0
B = 1
+ self.assertEqual(Union[Literal[Ints.A], Literal[Ints.A]],
+ Literal[Ints.A])
+ self.assertEqual(Union[Literal[Ints.B], Literal[Ints.B]],
+ Literal[Ints.B])
+
self.assertEqual(Union[Literal[Ints.A], Literal[Ints.B]].__args__,
(Literal[Ints.A], Literal[Ints.B]))