summaryrefslogtreecommitdiffstats
path: root/Lib/typing.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2021-10-31 08:22:16 (GMT)
committerGitHub <noreply@github.com>2021-10-31 08:22:16 (GMT)
commit634984d7dbdd91e0a51a793eed4d870e139ae1e0 (patch)
tree18d88dc13135d6a8cae515519144525e91efabe7 /Lib/typing.py
parentaad48062ef8f983fbb95f9dc0c3c3cef9c89df02 (diff)
downloadcpython-634984d7dbdd91e0a51a793eed4d870e139ae1e0.zip
cpython-634984d7dbdd91e0a51a793eed4d870e139ae1e0.tar.gz
cpython-634984d7dbdd91e0a51a793eed4d870e139ae1e0.tar.bz2
bpo-45679: Fix caching of multi-value typing.Literal (GH-29334)
Literal[True, 2] is no longer equal to Literal[1, 2].
Diffstat (limited to 'Lib/typing.py')
-rw-r--r--Lib/typing.py11
1 files changed, 5 insertions, 6 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index 78d973d..031aa24 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -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: