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
9 changes: 9 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,15 @@ def test_no_multiple_subscripts(self):
with self.assertRaises(TypeError):
Literal[1][1]

def test_equal(self):
self.assertEqual(Literal[1, 2], Literal[2, 1])
self.assertNotEqual(Literal[1, True], Literal[1])

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


XK = TypeVar('XK', str, bytes)
XV = TypeVar('XV')
Expand Down
25 changes: 24 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ def _remove_dups_flatten(parameters):
return tuple(params)


def _flatten_literal_params(parameters):
"""An internal helper for Literal creation: flatten Literals among parameters"""
params = []
for p in parameters:
if isinstance(p, _LiteralGenericAlias):
params.extend(p.__args__)
else:
uriyyo marked this conversation as resolved.
Show resolved Hide resolved
params.append(p)
return tuple(params)


_cleanups = []


Expand Down Expand Up @@ -460,7 +471,11 @@ def open_helper(file: str, mode: MODE) -> str:
"""
# There is no '_type_check' call because arguments to Literal[...] are
# values, not types.
return _GenericAlias(self, parameters)
if not isinstance(parameters, tuple):
parameters = (parameters,)

parameters = _flatten_literal_params(parameters)
return _LiteralGenericAlias(self, parameters)


@_SpecialForm
Expand Down Expand Up @@ -930,6 +945,14 @@ def __subclasscheck__(self, cls):
return True


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__)
uriyyo marked this conversation as resolved.
Show resolved Hide resolved


class Generic:
"""Abstract base class for generic types.
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ Jan Kanis
Rafe Kaplan
Jacob Kaplan-Moss
Allison Kaptur
Yurii Karabas
Janne Karila
Per Øyvind Karlsen
Anton Kasyanov
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Literal equality no longer depends on order of arguments. Patch provided by
Yurii Karabas.