From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Sun, 31 Oct 2021 08:43:40 +0000 (-0700) Subject: bpo-45679: Fix caching of multi-value typing.Literal (GH-29334) X-Git-Tag: v3.10.1~115 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3997f3ce8ab15269fc800062f75411865dbc0d55;p=thirdparty%2FPython%2Fcpython.git bpo-45679: Fix caching of multi-value typing.Literal (GH-29334) Literal[True, 2] is no longer equal to Literal[1, 2]. (cherry picked from commit 634984d7dbdd91e0a51a793eed4d870e139ae1e0) Co-authored-by: Serhiy Storchaka --- diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 762c0cc8f6be..fdec29ea5877 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -708,6 +708,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]) @@ -4917,6 +4919,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', diff --git a/Lib/typing.py b/Lib/typing.py index f842fc23da6c..21caabb5d3e6 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -403,9 +403,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 @@ -528,7 +529,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 @@ -551,9 +553,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 index 000000000000..a644492a12d1 --- /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]``.