-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Avoid false possibly-undefined errors due to omitted unrequired else statements.
#20149
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
base: master
Are you sure you want to change the base?
Avoid false possibly-undefined errors due to omitted unrequired else statements.
#20149
Conversation
This comment has been minimized.
This comment has been minimized.
mypy/checker.py
Outdated
|
|
||
| if_map, else_map = self.find_isinstance_check(e) | ||
|
|
||
| s.else_irrelevant_for_possibly_undefined = else_map is None |
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.
So else_irrelevant_for_possibly_undefined is essentially equivalent to else_unreachable or is_exhaustive? I'm a bit confused by this naming
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.
Oh yes, I should have changed this. When I started working on this PR, I thought that the assert_never cases and similar ones would also need similar treatment. So else_irrelevant_for_possibly_undefined meant "unreachable or does not return" to me. I will change it later. Thanks!
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.
done
|
That's a nice idea overall. Can I suggest setting the same flag in Now that was one path I found that checks for "unreachable else". There may be more, so maybe introducing a dummy empty I tried grepping for |
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
I am not sure about this. Semantic analysis already adds fake Lines 69 to 75 in d31d526
|
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.
OK, makes sense! I'd maybe consider a separate PR refactoring reachability.py to no longer add fake else blocks, but that might also be too much of effort for little gain. Could you please add a backlink comment to reachability.py to mention that these two strategies coexist now? It might be surprising later to see that one place adds fake else blocks and another place only uses a flag to do the same
| expr: list[Expression] | ||
| body: list[Block] | ||
| else_body: Block | None | ||
| unreachable_else: bool |
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 you add a brief comment explaining the attribute here? It would be nice to immediately see a notice with this attr's meaning and "volatile" nature
Completes #14771
The mentioned issue's
except: assert_neverexamples are already fixed by #15995. This PR only targets the "no unreachable else" examples.Introducing the special-purpose attribute
IfStmt.else_irrelevant_for_possibly_undefinedis not super nice, but it appears more straightforward and less risky than starting to introduce empty else blocks withis_unreachable = True(as discussed here).