Skip to content

Treat empty yield as no-op for reachability #15699

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
Var,
WhileStmt,
WithStmt,
YieldExpr,
is_final_node,
)
from mypy.options import Options
Expand Down Expand Up @@ -2739,6 +2740,8 @@ def is_noop_for_reachability(self, s: Statement) -> bool:
elif isinstance(s, ExpressionStmt):
if isinstance(s.expr, EllipsisExpr):
return True
elif isinstance(s.expr, YieldExpr) and s.expr.expr is None:
return True
elif isinstance(s.expr, CallExpr):
with self.expr_checker.msg.filter_errors():
typ = get_proper_type(
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -1446,3 +1446,21 @@ def f() -> None:
Foo()['a'] = 'a'
x = 0 # This should not be reported as unreachable
[builtins fixtures/exception.pyi]

[case testIntentionallyEmptyGenerator]
# flags: --warn-unreachable
from typing import Generator

def f() -> Generator[None, None, None]:
return
yield

[case testYieldNoneIgnored]
# flags: --warn-unreachable
from typing import Generator

def f() -> Generator[None, None, None]:
return
yield
yield
x = 42 # E: Statement is unreachable