mirror of
https://github.com/python/cpython.git
synced 2025-11-08 09:32:01 +00:00
[3.9] bpo-45679: Fix caching of multi-value typing.Literal (GH-29334) (GH-29342)
Literal[True, 2] is no longer equal to Literal[1, 2]..
(cherry picked from commit 634984d7db)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
5f527caf15
commit
bbcf06bf95
3 changed files with 9 additions and 6 deletions
|
|
@ -562,6 +562,8 @@ def test_equal(self):
|
||||||
self.assertNotEqual(Literal[True], Literal[1])
|
self.assertNotEqual(Literal[True], Literal[1])
|
||||||
self.assertNotEqual(Literal[1], Literal[2])
|
self.assertNotEqual(Literal[1], Literal[2])
|
||||||
self.assertNotEqual(Literal[1, True], Literal[1])
|
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], Literal[1])
|
||||||
self.assertEqual(Literal[1, 2], Literal[2, 1])
|
self.assertEqual(Literal[1, 2], Literal[2, 1])
|
||||||
self.assertEqual(Literal[1, 2, 3], Literal[1, 2, 3, 3])
|
self.assertEqual(Literal[1, 2, 3], Literal[1, 2, 3, 3])
|
||||||
|
|
|
||||||
|
|
@ -355,9 +355,10 @@ def __getitem__(self, parameters):
|
||||||
|
|
||||||
|
|
||||||
class _LiteralSpecialForm(_SpecialForm, _root=True):
|
class _LiteralSpecialForm(_SpecialForm, _root=True):
|
||||||
@_tp_cache(typed=True)
|
|
||||||
def __getitem__(self, parameters):
|
def __getitem__(self, parameters):
|
||||||
return self._getitem(self, parameters)
|
if not isinstance(parameters, tuple):
|
||||||
|
parameters = (parameters,)
|
||||||
|
return self._getitem(self, *parameters)
|
||||||
|
|
||||||
|
|
||||||
@_SpecialForm
|
@_SpecialForm
|
||||||
|
|
@ -478,7 +479,8 @@ def Optional(self, parameters):
|
||||||
return Union[arg, type(None)]
|
return Union[arg, type(None)]
|
||||||
|
|
||||||
@_LiteralSpecialForm
|
@_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).
|
"""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
|
This form can be used to indicate to type checkers that the corresponding
|
||||||
|
|
@ -501,9 +503,6 @@ def open_helper(file: str, mode: MODE) -> str:
|
||||||
"""
|
"""
|
||||||
# There is no '_type_check' call because arguments to Literal[...] are
|
# There is no '_type_check' call because arguments to Literal[...] are
|
||||||
# values, not types.
|
# values, not types.
|
||||||
if not isinstance(parameters, tuple):
|
|
||||||
parameters = (parameters,)
|
|
||||||
|
|
||||||
parameters = _flatten_literal_params(parameters)
|
parameters = _flatten_literal_params(parameters)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix caching of multi-value :data:`typing.Literal`. ``Literal[True, 2]`` is no
|
||||||
|
longer equal to ``Literal[1, 2]``.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue