FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-153896: Deduplicate mutable args in `Literal` type by sobolevn · Pull Request #153913 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 7 additions & 2 deletions Lib/test/test_typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2801,8 +2801,13 @@ 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__, ([], []))
# Mutable arguments will also be deduplicated:
self.assertEqual(Literal[[], []].__args__, ([],))
self.assertEqual(
Literal[1, {'a': 'b'}, 2, {'a': 'b'}, 3].__args__,
(1, {'a': 'b'}, 2, 3),
)
self.assertEqual(Literal[{1}, {1}, {2}, {2}].__args__, ({1}, {2}))

def test_flatten(self):
l1 = Literal[Literal[1], Literal[2], Literal[3]]
Expand Down
11 changes: 7 additions & 4 deletions Lib/typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,13 @@ def open_helper(file: str, mode: MODE) -> str:
# values, not types.
parameters = _flatten_literal_params(parameters)

try:
parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
except TypeError: # unhashable parameters
pass
parameters = tuple(
p
for p, _ in _deduplicate(
list(_value_and_type_iter(parameters)),
unhashable_fallback=True,
)
)

return _LiteralGenericAlias(self, parameters)

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deduplicate mutable args in :data:`typing.Literal`.
Loading

Back | FazBrowse Home | New Git URL