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

Checks module bodies to be reachable #11361

Merged
merged 6 commits into from
Oct 31, 2021
Merged
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
5 changes: 5 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ def check_first_pass(self) -> None:
with self.tscope.module_scope(self.tree.fullname):
with self.enter_partial_types(), self.binder.top_frame_context():
for d in self.tree.defs:
if (self.binder.is_unreachable()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.accept(Block(self.tree.defs)) didn't work. Let's try explicit condition.

and self.should_report_unreachable_issues()
and not self.is_raising_or_empty(d)):
self.msg.unreachable_statement(d)
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
break
self.accept(d)

assert not self.current_node_deferred
Expand Down
30 changes: 28 additions & 2 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -754,35 +754,46 @@ if sys.version_info[0] >= 2:
reveal_type('') # N: Revealed type is "Literal['']?"
[builtins fixtures/ops.pyi]

[case testUnreachableFlagWithBadControlFlow]
[case testUnreachableFlagWithBadControlFlow1]
# flags: --warn-unreachable
a: int
if isinstance(a, int):
reveal_type(a) # N: Revealed type is "builtins.int"
else:
reveal_type(a) # E: Statement is unreachable
[builtins fixtures/isinstancelist.pyi]

[case testUnreachableFlagWithBadControlFlow2]
# flags: --warn-unreachable
b: int
while isinstance(b, int):
reveal_type(b) # N: Revealed type is "builtins.int"
else:
reveal_type(b) # E: Statement is unreachable
[builtins fixtures/isinstancelist.pyi]

[case testUnreachableFlagWithBadControlFlow3]
# flags: --warn-unreachable
def foo(c: int) -> None:
reveal_type(c) # N: Revealed type is "builtins.int"
assert not isinstance(c, int)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a clearer statement of intent to explicitly raise an exception here. This will not make the code below unreachable if run with assertions disabled, which you can do in Python.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, because we already have tests for explicit raise. Here we test assert False

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. I guess my point is that assert False isn't a safe way to block off code. But whether mypy recognizes that or not is a more high level design decision, and not a subject for this PR.

reveal_type(c) # E: Statement is unreachable
[builtins fixtures/isinstancelist.pyi]

[case testUnreachableFlagWithBadControlFlow4]
# flags: --warn-unreachable
d: int
if False:
reveal_type(d) # E: Statement is unreachable
[builtins fixtures/isinstancelist.pyi]

[case testUnreachableFlagWithBadControlFlow5]
# flags: --warn-unreachable
e: int
if True:
reveal_type(e) # N: Revealed type is "builtins.int"
else:
reveal_type(e) # E: Statement is unreachable

[builtins fixtures/isinstancelist.pyi]

[case testUnreachableFlagStatementAfterReturn]
Expand Down Expand Up @@ -1390,3 +1401,18 @@ def f() -> None:
if nope():
x = 1 # E: Statement is unreachable
[builtins fixtures/dict.pyi]

[case testUnreachableModuleBody1]
# flags: --warn-unreachable
from typing import NoReturn
def foo() -> NoReturn:
raise Exception("foo")
foo()
x = 1 # E: Statement is unreachable
[builtins fixtures/exception.pyi]

[case testUnreachableModuleBody2]
# flags: --warn-unreachable
raise Exception
x = 1 # E: Statement is unreachable
[builtins fixtures/exception.pyi]