Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
15 changes: 9 additions & 6 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)

Comment on lines +778 to 786

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ususally want lines under 80 charactesr, so I suggest that you rename value_and_type_parameters to a shorter name or you use the same format as Nikita:

    parameters = tuple(
        p
        for p, _ in _deduplicate(
            list(_value_and_type_iter(parameters)),
            unhashable_fallback=True,
        )
    )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the future: how do I enforce the correct formatting? The pre-commit hook passed for some reason. I see it uses ruff, but if I run it one the codebase it changes all the files

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is a manual task =/ we do have some parts of the stdlib with ruff configs, but for historical reasons, we don't enforce style on old files (for maintenance and diff purposes) and only try to enforce them for new code (so code you add, or code you change around). See PEP-7/PEP-8 guidelines (while they are guidelines, sometimes we need to break them because it's not consistent).

What we aim for:

  • Minimal diff in general.
  • If possible PEP-7/PEP-8 style if this doesn't introduce too many unrelated changes.
  • If not psosible, keep consistent style.

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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deduplicate unhashable args in :data:`typing.Literal`.
Loading