mirror of
https://github.com/python/cpython.git
synced 2025-11-02 11:08:57 +00:00
bpo-42345: Fix three issues with typing.Literal parameters (GH-23294)
Literal equality no longer depends on the order of arguments. Fix issue related to `typing.Literal` caching by adding `typed` parameter to `typing._tp_cache` function. Add deduplication of `typing.Literal` arguments.
This commit is contained in:
parent
b0aba1fcdc
commit
f03d318ca4
4 changed files with 104 additions and 23 deletions
|
|
@ -528,6 +528,7 @@ class LiteralTests(BaseTestCase):
|
|||
self.assertEqual(repr(Literal[int]), "typing.Literal[int]")
|
||||
self.assertEqual(repr(Literal), "typing.Literal")
|
||||
self.assertEqual(repr(Literal[None]), "typing.Literal[None]")
|
||||
self.assertEqual(repr(Literal[1, 2, 3, 3]), "typing.Literal[1, 2, 3]")
|
||||
|
||||
def test_cannot_init(self):
|
||||
with self.assertRaises(TypeError):
|
||||
|
|
@ -559,6 +560,30 @@ class LiteralTests(BaseTestCase):
|
|||
with self.assertRaises(TypeError):
|
||||
Literal[1][1]
|
||||
|
||||
def test_equal(self):
|
||||
self.assertNotEqual(Literal[0], Literal[False])
|
||||
self.assertNotEqual(Literal[True], Literal[1])
|
||||
self.assertNotEqual(Literal[1], Literal[2])
|
||||
self.assertNotEqual(Literal[1, True], Literal[1])
|
||||
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])
|
||||
|
||||
def test_args(self):
|
||||
self.assertEqual(Literal[1, 2, 3].__args__, (1, 2, 3))
|
||||
self.assertEqual(Literal[1, 2, 3, 3].__args__, (1, 2, 3))
|
||||
self.assertEqual(Literal[1, Literal[2], Literal[3, 4]].__args__, (1, 2, 3, 4))
|
||||
# Mutable arguments will not be deduplicated
|
||||
self.assertEqual(Literal[[], []].__args__, ([], []))
|
||||
|
||||
def test_flatten(self):
|
||||
l1 = Literal[Literal[1], Literal[2], Literal[3]]
|
||||
l2 = Literal[Literal[1, 2], 3]
|
||||
l3 = Literal[Literal[1, 2, 3]]
|
||||
for l in l1, l2, l3:
|
||||
self.assertEqual(l, Literal[1, 2, 3])
|
||||
self.assertEqual(l.__args__, (1, 2, 3))
|
||||
|
||||
|
||||
XK = TypeVar('XK', str, bytes)
|
||||
XV = TypeVar('XV')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue