Skip to content
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

bpo-42345: Fix three issues with typing.Literal parameters #23294

Merged
merged 8 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,9 @@ def test_no_multiple_subscripts(self):
Literal[1][1]

def test_equal(self):
self.assertEqual(Literal[1], Literal[1])
self.assertEqual(Literal[1, 2], Literal[2, 1])
self.assertNotEqual(Literal[1, True], Literal[1])
uriyyo marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(Literal[1, 2, 3], Literal[1, 2, 3, 3])

def test_flatten(self):
self.assertEqual(Literal[Literal[1], Literal[2], Literal[3]], Literal[1, 2, 3])
Expand Down
9 changes: 8 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,13 +945,20 @@ def __subclasscheck__(self, cls):
return True


def _value_and_type_iter(parameters):
return ((p, type(p)) for p in parameters)


class _LiteralGenericAlias(_GenericAlias, _root=True):

def __eq__(self, other):
if not isinstance(other, _LiteralGenericAlias):
return NotImplemented

return len(self.__args__) == len(other.__args__) and set(self.__args__) == set(other.__args__)
return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))

def __hash__(self):
return hash(tuple(_value_and_type_iter(self.__args__)))


class Generic:
Expand Down