diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-04-23 18:15:24 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-23 18:15:24 (GMT) |
| commit | 4e9635e2b110b4d0d38d8e839e9604075b43a7f4 (patch) | |
| tree | 71f9e108086c548667d322f389dbc061b7316ff0 | |
| parent | ec29d0c0916af129eaafb0ad392449ca173309ed (diff) | |
| download | cpython-4e9635e2b110b4d0d38d8e839e9604075b43a7f4.zip cpython-4e9635e2b110b4d0d38d8e839e9604075b43a7f4.tar.gz cpython-4e9635e2b110b4d0d38d8e839e9604075b43a7f4.tar.bz2 | |
[3.11] gh-103592: Add tests of `Literal` with `Enum` and `Union` of `Literal`s (GH-103706) (#103720)
gh-103592: Add tests of `Literal` with `Enum` and `Union` of `Literal`s (GH-103706)
(cherry picked from commit 5041c2ba6e9c992d1c54834481c9be64581c0235)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
| -rw-r--r-- | Lib/test/test_typing.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index be804f1..5eb6d28 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1784,6 +1784,30 @@ class UnionTests(BaseTestCase): Union[Elem, str] # Nor should this + 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[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])) + class TupleTests(BaseTestCase): @@ -2151,6 +2175,13 @@ class LiteralTests(BaseTestCase): Literal[Literal[1, 2], Literal[4, 5]] Literal[b"foo", u"bar"] + 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. @@ -2240,6 +2271,20 @@ class LiteralTests(BaseTestCase): self.assertEqual(l, Literal[1, 2, 3]) self.assertEqual(l.__args__, (1, 2, 3)) + def test_does_not_flatten_enum(self): + import enum + class Ints(enum.IntEnum): + A = 1 + B = 2 + + l = Literal[ + Literal[Ints.A], + Literal[Ints.B], + Literal[1], + Literal[2], + ] + self.assertEqual(l.__args__, (Ints.A, Ints.B, 1, 2)) + XK = TypeVar('XK', str, bytes) XV = TypeVar('XV') |
