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

[3.9] bpo-45679: Fix caching of multi-value typing.Literal (GH-29334) #29342

Merged
merged 1 commit into from Nov 3, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/test/test_typing.py
Expand Up @@ -562,6 +562,8 @@ def test_equal(self):
self.assertNotEqual(Literal[True], Literal[1])
self.assertNotEqual(Literal[1], Literal[2])
self.assertNotEqual(Literal[1, True], Literal[1])
self.assertNotEqual(Literal[1, True], Literal[1, 1])
self.assertNotEqual(Literal[1, 2], Literal[True, 2])
self.assertEqual(Literal[1], Literal[1])
self.assertEqual(Literal[1, 2], Literal[2, 1])
self.assertEqual(Literal[1, 2, 3], Literal[1, 2, 3, 3])
Expand Down
11 changes: 5 additions & 6 deletions Lib/typing.py
Expand Up @@ -355,9 +355,10 @@ def __getitem__(self, parameters):


class _LiteralSpecialForm(_SpecialForm, _root=True):
@_tp_cache(typed=True)
def __getitem__(self, parameters):
return self._getitem(self, parameters)
if not isinstance(parameters, tuple):
parameters = (parameters,)
return self._getitem(self, *parameters)


@_SpecialForm
Expand Down Expand Up @@ -478,7 +479,8 @@ def Optional(self, parameters):
return Union[arg, type(None)]

@_LiteralSpecialForm
def Literal(self, parameters):
@_tp_cache(typed=True)
def Literal(self, *parameters):
"""Special typing form to define literal types (a.k.a. value types).

This form can be used to indicate to type checkers that the corresponding
Expand All @@ -501,9 +503,6 @@ def open_helper(file: str, mode: MODE) -> str:
"""
# There is no '_type_check' call because arguments to Literal[...] are
# values, not types.
if not isinstance(parameters, tuple):
parameters = (parameters,)

parameters = _flatten_literal_params(parameters)

try:
Expand Down
@@ -0,0 +1,2 @@
Fix caching of multi-value :data:`typing.Literal`. ``Literal[True, 2]`` is no
longer equal to ``Literal[1, 2]``.