]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45679: Fix caching of multi-value typing.Literal (GH-29334)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 31 Oct 2021 08:22:16 +0000 (10:22 +0200)
committerGitHub <noreply@github.com>
Sun, 31 Oct 2021 08:22:16 +0000 (10:22 +0200)
Literal[True, 2] is no longer equal to Literal[1, 2].

Lib/test/test_typing.py
Lib/typing.py
Misc/NEWS.d/next/Library/2021-10-30-21-11-37.bpo-45679.Dq8Cpu.rst [new file with mode: 0644]

index b1414dc82bae0f0e5a373981419135df7d705f8e..90d6bea59f899ce84b87a1471a13d21027ebc110 100644 (file)
@@ -714,6 +714,8 @@ class LiteralTests(BaseTestCase):
         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 @@ class SpecialAttrsTests(BaseTestCase):
             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',
index 78d973d2bba056f5501cd7a2d008c64427cbc6c2..031aa24eaf44205c06350b0b7b8591f22f2fa8f1 100644 (file)
@@ -411,9 +411,10 @@ class _SpecialForm(_Final, _root=True):
 
 
 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):
+@_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 Literal(self, parameters):
     """
     # 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 (file)
index 0000000..a644492
--- /dev/null
@@ -0,0 +1,2 @@
+Fix caching of multi-value :data:`typing.Literal`. ``Literal[True, 2]`` is no
+longer equal to ``Literal[1, 2]``.