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

[3.14] gh-153896: Deduplicate unhashable arguments in `typing.Literal` (GH-153914) by miss-islington · Pull Request #153956 · python/cpython · GitHub

/ cpython Public
Merged
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
10 changes: 8 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 @@ -2739,8 +2739,14 @@ 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__, ([], []))
# Unhashable arguments will be deduplicated too
self.assertEqual(Literal[[], []].__args__, ([],))
self.assertEqual(Literal[{"a": 1}, {"a": 1}].__args__, ({"a": 1},))
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
15 changes: 9 additions & 6 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 @@ -772,13 +772,16 @@ def open_helper(file: str, mode: MODE) -> str:
# There is no '_type_check' call because arguments to Literal[...] are
# values, not types.
parameters = _flatten_literal_params(parameters)
value_and_type_parameters = list(_value_and_type_iter(parameters))
deduplicated_parameters = tuple(
p
for p, _ in _deduplicate(
value_and_type_parameters,
unhashable_fallback=True,
)
)

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

return _LiteralGenericAlias(self, parameters)
return _LiteralGenericAlias(self, deduplicated_parameters)


@_SpecialForm
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 unhashable args in :data:`typing.Literal`.
Loading

Back | FazBrowse Home | New Git URL