[Python-checkins] bpo-45679: Fix caching of multi-value typing.Literal (GH-29334)

serhiy-storchaka webhook-mailer at python.org
Sun Oct 31 04:22:21 EDT 2021


https://github.com/python/cpython/commit/634984d7dbdd91e0a51a793eed4d870e139ae1e0
commit: 634984d7dbdd91e0a51a793eed4d870e139ae1e0
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-10-31T10:22:16+02:00
summary:

bpo-45679: Fix caching of multi-value typing.Literal (GH-29334)

Literal[True, 2] is no longer equal to Literal[1, 2].

files:
A Misc/NEWS.d/next/Library/2021-10-30-21-11-37.bpo-45679.Dq8Cpu.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index b1414dc82bae0..90d6bea59f899 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -714,6 +714,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])
@@ -4963,6 +4965,8 @@ def test_special_attrs(self):
             typing.Concatenate[Any, SpecialAttrsP]: 'Concatenate',
             typing.Final[Any]: 'Final',
             typing.Literal[Any]: 'Literal',
+            typing.Literal[1, 2]: 'Literal',
+            typing.Literal[True, 2]: 'Literal',
             typing.Optional[Any]: 'Optional',
             typing.TypeGuard[Any]: 'TypeGuard',
             typing.Union[Any]: 'Any',
diff --git a/Lib/typing.py b/Lib/typing.py
index 78d973d2bba05..031aa24eaf442 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -411,9 +411,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
@@ -536,7 +537,8 @@ def Optional(self, parameters):
     return Union[arg, type(None)]
 
 @_LiteralSpecialForm
-def Literal(self, parameters):
+ at _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
@@ -559,9 +561,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:
diff --git a/Misc/NEWS.d/next/Library/2021-10-30-21-11-37.bpo-45679.Dq8Cpu.rst b/Misc/NEWS.d/next/Library/2021-10-30-21-11-37.bpo-45679.Dq8Cpu.rst
new file mode 100644
index 0000000000000..a644492a12d17
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-10-30-21-11-37.bpo-45679.Dq8Cpu.rst
@@ -0,0 +1,2 @@
+Fix caching of multi-value :data:`typing.Literal`. ``Literal[True, 2]`` is no
+longer equal to ``Literal[1, 2]``.



More information about the Python-checkins mailing list