We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 40dfcc1 + 6f198d2 commit d3daef1Copy full SHA for d3daef1
4 files changed
Lib/test/test_typing.py
@@ -2739,8 +2739,14 @@ def test_args(self):
2739
self.assertEqual(Literal[1, 2, 3].__args__, (1, 2, 3))
2740
self.assertEqual(Literal[1, 2, 3, 3].__args__, (1, 2, 3))
2741
self.assertEqual(Literal[1, Literal[2], Literal[3, 4]].__args__, (1, 2, 3, 4))
2742
- # Mutable arguments will not be deduplicated
2743
- self.assertEqual(Literal[[], []].__args__, ([], []))
+ # Unhashable arguments will be deduplicated too
+ self.assertEqual(Literal[[], []].__args__, ([],))
2744
+ self.assertEqual(Literal[{"a": 1}, {"a": 1}].__args__, ({"a": 1},))
2745
+ self.assertEqual(
2746
+ Literal[1, {'a': 'b'}, 2, {'a': 'b'}, 3].__args__,
2747
+ (1, {'a': 'b'}, 2, 3),
2748
+ )
2749
+ self.assertEqual(Literal[{1}, {1}, {2}, {2}].__args__, ({1}, {2}))
2750
2751
def test_flatten(self):
2752
l1 = Literal[Literal[1], Literal[2], Literal[3]]
Lib/typing.py
@@ -772,13 +772,16 @@ def open_helper(file: str, mode: MODE) -> str:
772
# There is no '_type_check' call because arguments to Literal[...] are
773
# values, not types.
774
parameters = _flatten_literal_params(parameters)
775
+ value_and_type_parameters = list(_value_and_type_iter(parameters))
776
+ deduplicated_parameters = tuple(
777
+ p
778
+ for p, _ in _deduplicate(
779
+ value_and_type_parameters,
780
+ unhashable_fallback=True,
781
782
783
- try:
- parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
- except TypeError: # unhashable parameters
- pass
-
- return _LiteralGenericAlias(self, parameters)
784
+ return _LiteralGenericAlias(self, deduplicated_parameters)
785
786
787
@_SpecialForm
Misc/NEWS.d/next/Library/2026-07-18-10-52-10.gh-issue-153896.87oevp.rst
@@ -0,0 +1 @@
1
+Deduplicate unhashable args in :data:`typing.Literal`.
Tools/check-c-api-docs/ignored_c_api.txt
@@ -102,3 +102,5 @@ PyUnicode_AsDecodedObject
102
PyUnicode_AsDecodedUnicode
103
PyUnicode_AsEncodedObject
104
PyUnicode_AsEncodedUnicode
105
+# Soft-deprecated in 3.15; will remain undocumented in 3.14 and 3.13
106
+PyException_HEAD
0 commit comments