Skip to content

Backport tests of Union + Literal from CPython #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,13 @@ def test_basics(self):
Literal["x", "y", "z"]
Literal[None]

def test_enum(self):
import enum
class My(enum.Enum):
A = 'A'

self.assertEqual(Literal[My.A].__args__, (My.A,))

def test_illegal_parameters_do_not_raise_runtime_errors(self):
# Type checkers should reject these types, but we do not
# raise errors at runtime to maintain maximum flexibility
Expand Down Expand Up @@ -794,6 +801,64 @@ def test_args(self):
# Mutable arguments will not be deduplicated
self.assertEqual(Literal[[], []].__args__, ([], []))

def test_union_of_literals(self):
self.assertEqual(Union[Literal[1], Literal[2]].__args__,
(Literal[1], Literal[2]))
self.assertEqual(Union[Literal[1], Literal[1]],
Literal[1])

self.assertEqual(Union[Literal[False], Literal[0]].__args__,
(Literal[False], Literal[0]))
self.assertEqual(Union[Literal[True], Literal[1]].__args__,
(Literal[True], Literal[1]))

import enum
class Ints(enum.IntEnum):
A = 0
B = 1

self.assertEqual(Union[Literal[Ints.A], Literal[Ints.B]].__args__,
(Literal[Ints.A], Literal[Ints.B]))

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[0], Literal[Ints.A], Literal[False]].__args__,
(Literal[0], Literal[Ints.A], Literal[False]))
self.assertEqual(Union[Literal[1], Literal[Ints.B], Literal[True]].__args__,
(Literal[1], Literal[Ints.B], Literal[True]))

@skipUnless(TYPING_3_10_0, "Python 3.10+ required")
def test_or_type_operator_with_Literal(self):
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_flatten(self):
l1 = Literal[Literal[1], Literal[2], Literal[3]]
l2 = Literal[Literal[1, 2], 3]
Expand All @@ -802,6 +867,20 @@ def test_flatten(self):
self.assertEqual(lit, Literal[1, 2, 3])
self.assertEqual(lit.__args__, (1, 2, 3))

def test_does_not_flatten_enum(self):
import enum
class Ints(enum.IntEnum):
A = 1
B = 2

literal = Literal[
Literal[Ints.A],
Literal[Ints.B],
Literal[1],
Literal[2],
]
self.assertEqual(literal.__args__, (Ints.A, Ints.B, 1, 2))

def test_caching_of_Literal_respects_type(self):
self.assertIs(type(Literal[1].__args__[0]), int)
self.assertIs(type(Literal[True].__args__[0]), bool)
Expand Down