Skip to content

Commit b67dd0f

Browse files
authored
Improve the error message in a Y061 edge case (#445)
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.
1 parent c7d988e commit b67dd0f

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

pyi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,9 @@ def _visit_typing_Literal(self, node: ast.Subscript) -> None:
14321432
elts = node.slice.elts
14331433
for i, elt in enumerate(elts):
14341434
if _is_None(elt):
1435-
elts_without_none = elts[:i] + elts[i + 1 :]
1435+
elts_without_none = elts[:i] + [
1436+
elt for elt in elts[i + 1 :] if not _is_None(elt)
1437+
]
14361438
if len(elts_without_none) == 1:
14371439
new_literal_slice = unparse(elts_without_none[0])
14381440
else:

tests/literals.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ from typing import Literal
22

33
Literal[None] # Y061 None inside "Literal[]" expression. Replace with "None"
44
Literal[True, None] # Y061 None inside "Literal[]" expression. Replace with "Literal[True] | None"
5-
Literal[1, None, "foo", None] # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo', None] | None"
5+
Literal[1, None, "foo", None] # Y061 None inside "Literal[]" expression. Replace with "Literal[1, 'foo'] | None"

0 commit comments

Comments
 (0)