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

[possibly-undefined] Fix loop misconceptions #17720

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

NMertsch
Copy link

@NMertsch NMertsch commented Aug 29, 2024

This PR fixes two misconceptions in the current possibly-undefined handling of for and while loops.

for loop: index is only defined if the body is executed

Currently, the index is always considered as defined. Counter example:

for i in []:
    pass

i  # Name "i" is undefined

for and while loop: else is executed if the body is not executed

Currently, the else block of loops is only considered as executed if there was no break. Counter examples:

for _ in []:
    break
else:
    print("executed")

while False:
    break
else:
    print("executed")

The fix basically handles both cases like if {body is executed}: ...; else: ....
This is more accurate than the current implementation, but there is still room for improvement: #14209 (comment)

Refactorings

Following the Boy Scout Rule, I added a few minor code style improvements in separate commits. I'm fine with dropping them, please see them as suggestions.

@KotlinIsland
Copy link
Contributor

you would want to investigate mypy.partially_defined.PossiblyUndefinedVariableVisitor.visit_for_stmt

@NMertsch NMertsch force-pushed the fix-14209-for-break-else-possibly-undefined branch from 8a921a4 to 65935c6 Compare September 1, 2024 20:35
@NMertsch NMertsch force-pushed the fix-14209-for-break-else-possibly-undefined branch from 55bfee2 to 0a4d22c Compare September 9, 2024 16:18

This comment has been minimized.

@NMertsch NMertsch force-pushed the fix-14209-for-break-else-possibly-undefined branch 2 times, most recently from cdb4994 to 27b2c57 Compare September 9, 2024 17:22

This comment has been minimized.

@KotlinIsland
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

don't get worried that this primer is empty, it doesn't have any rules enabled. if you wanted, you could run a custom primer with the relevant rule enabled

@NMertsch NMertsch force-pushed the fix-14209-for-break-else-possibly-undefined branch from 27b2c57 to 730e65a Compare September 10, 2024 19:52

This comment has been minimized.

@NMertsch NMertsch changed the title Fix #14209: Handle else branch of for loops in possibly-undefined check possibly-undefined: fix loop misconceptions Sep 10, 2024
@NMertsch NMertsch changed the title possibly-undefined: fix loop misconceptions [possibly-undefined] Fix loop misconceptions Sep 10, 2024

This comment has been minimized.

@NMertsch NMertsch force-pushed the fix-14209-for-break-else-possibly-undefined branch from aa021aa to d8689ea Compare September 10, 2024 21:06

This comment has been minimized.

@NMertsch NMertsch force-pushed the fix-14209-for-break-else-possibly-undefined branch from d8689ea to 082ab1b Compare September 10, 2024 21:39
@NMertsch NMertsch marked this pull request as ready for review September 10, 2024 21:40
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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.

2 participants