Skip to content

gh-103746: Test types.UnionType and Literal types together #103747

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 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
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
gh-103746: Test types.UnionType and Literal types together
  • Loading branch information
sobolevn committed Apr 24, 2023
commit bd2f61c0ee3801dfe7a9497b94ea3daee98f90b5
29 changes: 29 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,35 @@ def test_or_type_operator_with_SpecialForm(self):
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"
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,11 @@ class Ints(enum.IntEnum):
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]))

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy