FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Checks module bodies to be reachable by sobolevn · Pull Request #11361 · python/mypy · GitHub

Repository navigation

Checks module bodies to be reachable - #11361

Merged
hauntsaninja merged 6 commits into
python:masterfrom
sobolevn:issue-11331
Oct 31, 2021
Merged

hauntsaninja merged 6 commits into
python:masterfrom
sobolevn:issue-11331

Conversation

sobolevn commented Oct 19, 2021 •
edited
Loading

Copy link
Copy Markdown
Member

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

Comment thread mypy/checker.py
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

sobolevn closed this Oct 19, 2021
sobolevn reopened this Oct 19, 2021
sobolevn changed the title Checks module bodies with to be reachable Checks module bodies to be reachable Oct 20, 2021

JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Sounds like a good idea! If a user asks for warnings about unreachable code, this seems like what would be expected.

reveal_type(b) # E: Statement is unreachable

def foo(c: int) -> None:
def foo(c: int) -> None: # E: Statement is unreachable

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is this resolved? Afaict the if False check should be in its own test case right?

Comment thread mypy/checker.py

Copy link
Copy Markdown
Member Author

@JukkaL done! Now it is in sync with pylance 👍

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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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(b) # E: Statement is unreachable

def foo(c: int) -> None:
def foo(c: int) -> None: # E: Statement is unreachable

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is this resolved? Afaict the if False check should be in its own test case right?

Copy link
Copy Markdown
Member Author

@hauntsaninja I forgot to run git push 🤦

» git log
commit 2d51fb85261a23242f79e9ffaa4dc95cb7c0c5d0 (HEAD -> issue-11331)
Author: sobolevn <mail@sobolevn.me>
Date:   Fri Oct 22 18:28:03 2021 +0300

    Addresses review

hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I kind of suspected that :-) Thanks, looks great

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The [unreachable] check fails for module scope code and allows unreachable raising of Exceptions

4 participants


Back | FazBrowse Home | New Git URL