We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent aad4806 commit 634984dCopy full SHA for 634984d
3 files changed
Lib/test/test_typing.py
@@ -714,6 +714,8 @@ def test_equal(self):
714
self.assertNotEqual(Literal[True], Literal[1])
715
self.assertNotEqual(Literal[1], Literal[2])
716
self.assertNotEqual(Literal[1, True], Literal[1])
717
+ self.assertNotEqual(Literal[1, True], Literal[1, 1])
718
+ self.assertNotEqual(Literal[1, 2], Literal[True, 2])
719
self.assertEqual(Literal[1], Literal[1])
720
self.assertEqual(Literal[1, 2], Literal[2, 1])
721
self.assertEqual(Literal[1, 2, 3], Literal[1, 2, 3, 3])
@@ -4963,6 +4965,8 @@ def test_special_attrs(self):
4963
4965
typing.Concatenate[Any, SpecialAttrsP]: 'Concatenate',
4964
4966
typing.Final[Any]: 'Final',
4967
typing.Literal[Any]: 'Literal',
4968
+ typing.Literal[1, 2]: 'Literal',
4969
+ typing.Literal[True, 2]: 'Literal',
4970
typing.Optional[Any]: 'Optional',
4971
typing.TypeGuard[Any]: 'TypeGuard',
4972
typing.Union[Any]: 'Any',
Lib/typing.py
@@ -411,9 +411,10 @@ def __getitem__(self, parameters):
411
412
413
class _LiteralSpecialForm(_SpecialForm, _root=True):
414
- @_tp_cache(typed=True)
415
def __getitem__(self, parameters):
416
- return self._getitem(self, parameters)
+ if not isinstance(parameters, tuple):
+ parameters = (parameters,)
417
+ return self._getitem(self, *parameters)
418
419
420
@_SpecialForm
@@ -536,7 +537,8 @@ def Optional(self, parameters):
536
537
return Union[arg, type(None)]
538
539
@_LiteralSpecialForm
-def Literal(self, parameters):
540
+@_tp_cache(typed=True)
541
+def Literal(self, *parameters):
542
"""Special typing form to define literal types (a.k.a. value types).
543
544
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:
559
561
"""
560
562
# There is no '_type_check' call because arguments to Literal[...] are
563
# values, not types.
- if not isinstance(parameters, tuple):
- parameters = (parameters,)
564
-
565
parameters = _flatten_literal_params(parameters)
566
567
try:
Misc/NEWS.d/next/Library/2021-10-30-21-11-37.bpo-45679.Dq8Cpu.rst
@@ -0,0 +1,2 @@
1
+Fix caching of multi-value :data:`typing.Literal`. ``Literal[True, 2]`` is no
2
+longer equal to ``Literal[1, 2]``.
0 commit comments