Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-42345: Fix three issues with typing.Literal parameters #23294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update News. Add tests for Literal.__args__
  • Loading branch information
uriyyo committed Nov 17, 2020
commit f653d680e2fbf94e254f32a8a7e24cb0f95369a9
16 changes: 13 additions & 3 deletions 16 Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,20 @@ def test_equal(self):
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):
self.assertEqual(Literal[Literal[1], Literal[2], Literal[3]], Literal[1, 2, 3])
self.assertEqual(Literal[Literal[1, 2], 3], Literal[1, 2, 3])
self.assertEqual(Literal[Literal[1, 2, 3]], Literal[1, 2, 3])
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)
Expand Down
6 changes: 3 additions & 3 deletions 6 Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ def _tp_cache(func=None, /, *, typed=False):
"""Internal wrapper caching __getitem__ of generic types with a fallback to
original function for non-hashable arguments.
uriyyo marked this conversation as resolved.
Show resolved Hide resolved
"""
if func is not None:
return _tp_cache(typed=typed)(func)

def decorator(func):
cached = functools.lru_cache(typed=typed)(func)
_cleanups.append(cached.cache_clear)
Expand All @@ -267,6 +264,9 @@ def inner(*args, **kwds):
return func(*args, **kwds)
return inner

if func is not None:
return decorator(func)

return decorator

def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Fix ``typing.Literal`` equals method to ignore 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. Patch provided by Yurii Karabas.
Fix various issues with ``typing.Literal`` parameter handling (flatten,
deduplicate, use type to cache key). Patch provided by Yurii Karabas.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.