-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support __bool__ with Literal in --warn-unreachable #15645
Conversation
Diff from mypy_primer, showing the effect of this PR on open source code: steam.py (https://github.com/Gobot1234/steam.py)
- steam/package.py:396: error: Return value expected [return-value]
- steam/state.py:2565: error: Item "None" of "Language | None" has no attribute "api_name" [union-attr]
|
MyPy Primer Analysis- steam/package.py:396: error: Return value expected [return-value]
See below for an explanation of - steam/state.py:2565: error: Item "None" of "Language | None" has no attribute "api_name" [union-attr]
The type of language is See below for an explanation of
|
I personally think we should make the type checker to go into "impossible" blocks in all but a few trivial cases (always-true constants, TYPE_CHECKING) which are pruned early on. But that's a separate discussion. |
I saw the comments in #15386 and I think there are also arguments for skipping. For example that someone might address issues in blocks that will be removed (e.g. On the other hand, I agree that if "--warn-unreachable" is not enabled skipping the blocks is completely confusing. Back to the code: I decided for
for handling the other cases at the top. So it feels more consistent for me, even though we are skipping now blocks that were not skipped before. For me both behaviors are fine and I can change it, if that is requested. |
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.
Thanks for fixing, this is clearer!
This adds support for
Literal
as return type of__bool__
in the reachability analysis.Example:
Addresses #7008
Fixes parts of #15523 (Handling
bool()
should probably be a separate PR)To be honest, I did not understand what the purpose of this code was / why "the branch was even taken or analzyed" before:
Now instead of the branch is taken with the updated type, it is marked as unreachable.
Edit: It is probably for the case that "--warn-unreachable" is not enabled. Should the type checker then continue in the unreachable block? But I guess it also never did for the other cases e.g.
if False
...Edit 2: It does not make sense to continue, because it might lead to incorrect preconditions of the blocks.