Skip to content

Commit 1c8c009

Browse files
authored
Avoid treating pass and ... as no-op for reachability (#20488)
Fixes #16232 The inclusion of these statements dates back to the original `--warn-unreachable` PR (and doesn't provide justification) I've been advocating for more consistent heuristics for allowable unreachable code across type checkers and I can't come up with a good reason that this should be allowed
1 parent 5f90caf commit 1c8c009

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

mypy/checker.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,12 +3181,10 @@ def is_noop_for_reachability(self, s: Statement) -> bool:
31813181
return True
31823182
elif isinstance(s, ReturnStmt) and is_literal_not_implemented(s.expr):
31833183
return True
3184-
elif isinstance(s, (RaiseStmt, PassStmt)):
3184+
elif isinstance(s, RaiseStmt):
31853185
return True
31863186
elif isinstance(s, ExpressionStmt):
3187-
if isinstance(s.expr, EllipsisExpr):
3188-
return True
3189-
elif isinstance(s.expr, CallExpr):
3187+
if isinstance(s.expr, CallExpr):
31903188
with self.expr_checker.msg.filter_errors(filter_revealed_type=True):
31913189
typ = get_proper_type(
31923190
self.expr_checker.accept(

test-data/unit/check-unreachable-code.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,17 @@ class C:
16371637
return C()
16381638
else:
16391639
return NotImplemented
1640+
[builtins fixtures/isinstance.pyi]
1641+
1642+
[case testPassAndEllipsisUnreachable]
1643+
# flags: --warn-unreachable
1644+
def f() -> None:
1645+
return
1646+
pass # E: Statement is unreachable
16401647

1648+
def g() -> None:
1649+
return
1650+
... # E: Statement is unreachable
16411651
[builtins fixtures/isinstance.pyi]
16421652

16431653
[case testUnreachableStatementPrettyHighlighting]

0 commit comments

Comments
 (0)