https://github.com/python/cpython/commit/a53b5b126749133e50692fe0c3729f5580ee8500
commit: a53b5b126749133e50692fe0c3729f5580ee8500
branch: main
author: Massimiliano Bruni <[email protected]>
committer: ZeroIntensity <[email protected]>
date: 2026-07-18T09:16:03-04:00
summary:
gh-153896: Deduplicate unhashable arguments in `typing.Literal` (GH-153914)
files:
A Misc/NEWS.d/next/Library/2026-07-18-10-52-10.gh-issue-153896.87oevp.rst
M Lib/test/test_typing.py
M Lib/typing.py
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 17574c7f03d8e1..c1f340230159db 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -2801,8 +2801,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]]
diff --git a/Lib/typing.py b/Lib/typing.py
index 933336ff4cf37e..054420865d7fb5 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -775,13 +775,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
diff --git
a/Misc/NEWS.d/next/Library/2026-07-18-10-52-10.gh-issue-153896.87oevp.rst
b/Misc/NEWS.d/next/Library/2026-07-18-10-52-10.gh-issue-153896.87oevp.rst
new file mode 100644
index 00000000000000..217a3d3d272ed0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-18-10-52-10.gh-issue-153896.87oevp.rst
@@ -0,0 +1 @@
+Deduplicate unhashable args in :data:`typing.Literal`.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]