]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-45679: Fix caching of multi-value typing.Literal (GH-29334) (GH-29342)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 3 Nov 2021 09:28:55 +0000 (11:28 +0200)
committerGitHub <noreply@github.com>
Wed, 3 Nov 2021 09:28:55 +0000 (17:28 +0800)
Literal[True, 2] is no longer equal to Literal[1, 2]..
(cherry picked from commit 634984d7dbdd91e0a51a793eed4d870e139ae1e0)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
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 1163d6f70aef177de656d8f2d9d4abdc28302286..19c38ec833c676ba69389493aa99ab51596d7416 100644 (file)
@@ -562,6 +562,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])
index 747f6b25e5be89b158b2151e3f87bc726ece78f4..da70d4115fa2384d43dc51fe12d64a228b2e262c 100644 (file)
@@ -355,9 +355,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
@@ -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
@@ -501,9 +503,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]``.