Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e0e1bee

Browse filesBrowse files
[3.11] gh-103746: Test types.UnionType and Literal types together (GH-103747) (#103772)
gh-103746: Test `types.UnionType` and `Literal` types together (GH-103747) (cherry picked from commit 3d29eda) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent 25406e5 commit e0e1bee
Copy full SHA for e0e1bee

File tree

2 files changed

+34
-0
lines changed
Filter options

2 files changed

+34
-0
lines changed

‎Lib/test/test_types.py

Copy file name to clipboardExpand all lines: Lib/test/test_types.py
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,35 @@ def test_or_type_operator_with_SpecialForm(self):
925925
assert typing.Optional[int] | str == typing.Union[int, str, None]
926926
assert typing.Union[int, bool] | str == typing.Union[int, bool, str]
927927

928+
def test_or_type_operator_with_Literal(self):
929+
Literal = typing.Literal
930+
self.assertEqual((Literal[1] | Literal[2]).__args__,
931+
(Literal[1], Literal[2]))
932+
933+
self.assertEqual((Literal[0] | Literal[False]).__args__,
934+
(Literal[0], Literal[False]))
935+
self.assertEqual((Literal[1] | Literal[True]).__args__,
936+
(Literal[1], Literal[True]))
937+
938+
self.assertEqual(Literal[1] | Literal[1], Literal[1])
939+
self.assertEqual(Literal['a'] | Literal['a'], Literal['a'])
940+
941+
import enum
942+
class Ints(enum.IntEnum):
943+
A = 0
944+
B = 1
945+
946+
self.assertEqual(Literal[Ints.A] | Literal[Ints.A], Literal[Ints.A])
947+
self.assertEqual(Literal[Ints.B] | Literal[Ints.B], Literal[Ints.B])
948+
949+
self.assertEqual((Literal[Ints.B] | Literal[Ints.A]).__args__,
950+
(Literal[Ints.B], Literal[Ints.A]))
951+
952+
self.assertEqual((Literal[0] | Literal[Ints.A]).__args__,
953+
(Literal[0], Literal[Ints.A]))
954+
self.assertEqual((Literal[1] | Literal[Ints.B]).__args__,
955+
(Literal[1], Literal[Ints.B]))
956+
928957
def test_or_type_repr(self):
929958
assert repr(int | str) == "int | str"
930959
assert repr((int | str) | list) == "int | str | list"

‎Lib/test/test_typing.py

Copy file name to clipboardExpand all lines: Lib/test/test_typing.py
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,11 @@ class Ints(enum.IntEnum):
18001800
A = 0
18011801
B = 1
18021802

1803+
self.assertEqual(Union[Literal[Ints.A], Literal[Ints.A]],
1804+
Literal[Ints.A])
1805+
self.assertEqual(Union[Literal[Ints.B], Literal[Ints.B]],
1806+
Literal[Ints.B])
1807+
18031808
self.assertEqual(Union[Literal[Ints.A], Literal[Ints.B]].__args__,
18041809
(Literal[Ints.A], Literal[Ints.B]))
18051810

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.