-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Checks module bodies to be reachable #11361
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
Conversation
@@ -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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a good idea! If a user asks for warnings about unreachable code, this seems like what would be expected.
@@ -768,13 +768,13 @@ while isinstance(b, int): | |||
else: | |||
reveal_type(b) # E: Statement is unreachable | |||
|
|||
def foo(c: int) -> None: | |||
def foo(c: int) -> None: # E: Statement is unreachable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rest of this test case should probably be moved to a new test case, since this is no longer testing what was the original intent, as everything is unreachable beyond this point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this resolved? Afaict the if False
check should be in its own test case right?
@JukkaL done! Now it is in sync with |
@@ -768,13 +768,13 @@ while isinstance(b, int): | |||
else: | |||
reveal_type(b) # E: Statement is unreachable | |||
|
|||
def foo(c: int) -> None: | |||
def foo(c: int) -> None: # E: Statement is unreachable | |||
reveal_type(c) # N: Revealed type is "builtins.int" | |||
assert not isinstance(c, int) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
@@ -768,13 +768,13 @@ while isinstance(b, int): | |||
else: | |||
reveal_type(b) # E: Statement is unreachable | |||
|
|||
def foo(c: int) -> None: | |||
def foo(c: int) -> None: # E: Statement is unreachable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this resolved? Afaict the if False
check should be in its own test case right?
@hauntsaninja I forgot to run
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of suspected that :-) Thanks, looks great
This is a very simple change, but now module bodies are also checked to be reachable.
Let's see what effect it will have.
Closes #11331