Skip to content

Commit

Permalink
Improve the error message in a Y061 edge case (#445)
Browse files Browse the repository at this point in the history
Currently we're recommending code that we'd immediately complain about
if the user actually used it. We shouldn't do that. Possibly we could
introduce another error code emitting an error for duplicate `Literal`
members, but for now I'd just like to fix the Y061 error message.
  • Loading branch information
AlexWaygood authored Nov 8, 2023
1 parent c7d988e commit b67dd0f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,9 @@ def _visit_typing_Literal(self, node: ast.Subscript) -> None:
elts = node.slice.elts
for i, elt in enumerate(elts):
if _is_None(elt):
elts_without_none = elts[:i] + elts[i + 1 :]
elts_without_none = elts[:i] + [
elt for elt in elts[i + 1 :] if not _is_None(elt)
]
if len(elts_without_none) == 1:
new_literal_slice = unparse(elts_without_none[0])
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/literals.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ from typing import Literal

Literal[None] # Y061 None inside "Literal[]" expression. Replace with "None"
Literal[True, None] # Y061 None inside "Literal[]" expression. Replace with "Literal[True] | None"
Literal[1, None, "foo", None] # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo', None] | None"
Literal[1, None, "foo", None] # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"

0 comments on commit b67dd0f

Please sign in to comment.