Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce consistent use of Literal and None #435

Merged
merged 7 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 29 additions & 1 deletion pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ def visit_Subscript(self, node: ast.Subscript) -> None:
self.visit(subscripted_object)
if subscripted_object_name == "Literal":
with self.string_literals_allowed.enabled():
self.visit(node.slice)
self._visit_typing_Literal(node)
return

if isinstance(node.slice, ast.Tuple):
Expand All @@ -1369,6 +1369,33 @@ def visit_Subscript(self, node: ast.Subscript) -> None:
if subscripted_object_name in {"tuple", "Tuple"}:
self._Y090_error(node)

def _visit_typing_Literal(self, node: ast.Subscript) -> None:
if isinstance(node.slice, ast.Constant) and _is_None(node.slice):
# Special case for `Literal[None]`
self.error(node.slice, Y061.format(suggestion="None"))
elif isinstance(node.slice, ast.Tuple):
elts = node.slice.elts
for i, elt in enumerate(elts):
if _is_None(elt):
elts_without_none = elts[:i] + elts[i + 1 :]
slice_without_none = (
elts_without_none[0]
if len(elts_without_none) == 1
else ast.Tuple(elts=elts_without_none)
)
suggestion = unparse(
ast.BinOp(
op=ast.BitOr(),
left=ast.Subscript(
value=node.value, slice=slice_without_none
),
right=ast.Constant(value=None),
)
)
tomasr8 marked this conversation as resolved.
Show resolved Hide resolved
self.error(elt, Y061.format(suggestion=suggestion))
break # Only report the first `None`
self.visit(node.slice)

def _visit_slice_tuple(self, node: ast.Tuple, parent: str | None) -> None:
if parent == "Union":
self._check_union_members(node.elts, is_pep_604_union=False)
Expand Down Expand Up @@ -2180,6 +2207,7 @@ def parse_options(options: argparse.Namespace) -> None:
'Y060 Redundant inheritance from "Generic[]"; '
"class would be inferred as generic anyway"
)
Y061 = "Y061 None inside Literal[...] expression. Replace with {suggestion}"
tomasr8 marked this conversation as resolved.
Show resolved Hide resolved
Y090 = (
'Y090 "{original}" means '
'"a tuple of length 1, in which the sole element is of type {typ!r}". '
Expand Down
6 changes: 6 additions & 0 deletions tests/literals.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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"] # Y061 None inside Literal[...] expression. Replace with Literal[1, 'foo'] | None
Literal[None, None] # Y061 None inside Literal[...] expression. Replace with Literal[None] | None
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved