Skip to content

Commit

Permalink
Treat empty yield as no-op for reachability
Browse files Browse the repository at this point in the history
  • Loading branch information
ikonst committed Jul 18, 2023
1 parent b6b6624 commit 98c038d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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

0 comments on commit 98c038d

Please sign in to comment.