Skip to content

[possibly-undefined] Fix loop misconceptions #17720

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

Closed
wants to merge 6 commits into from
Closed

[possibly-undefined] Fix loop misconceptions #17720

wants to merge 6 commits into 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

This comment has been minimized.

1 similar comment

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

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.

1 similar comment

This comment has been minimized.

@NMertsch NMertsch marked this pull request as ready for review September 10, 2024 21:40

This comment has been minimized.

@NMertsch NMertsch closed this Apr 6, 2025
Copy link
Contributor

github-actions bot commented Apr 6, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

CPython (cases_generator) (https://github.com/python/cpython)
+ Tools/cases_generator/generators_common.py:89: error: Name "tkn" may be undefined  [possibly-undefined]

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