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 3 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
4 changes: 4 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ 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
self.accept(d)

assert not self.current_node_deferred
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -1821,3 +1821,18 @@ def foo2():
bar2 = [] # type: List[str]
bar2
[builtins fixtures/list.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]