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

(🎁) Support for/else statement in partially defined check #14097

Closed
KotlinIsland opened this issue Nov 15, 2022 · 4 comments · Fixed by #14191
Closed

(🎁) Support for/else statement in partially defined check #14097

KotlinIsland opened this issue Nov 15, 2022 · 4 comments · Fixed by #14191
Labels
feature topic-possibly-undefined possibly-undefined error code

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Nov 15, 2022

l: list[int]
for _ in l:
    pass
else:
    result = False
print(result)  # error: Name "result" may be undefined  [partially-defined]

This is incorrect, result will always be defined in this case.

@ilinum ilinum added the topic-possibly-undefined possibly-undefined error code label Nov 16, 2022
@ilinum
Copy link
Collaborator

ilinum commented Nov 21, 2022

Thanks for the bug report!

mypy isn't smart enough to detect that the list l is always empty. Note that technically the body of the loop is unreachable but it's not flagged with --warn-unreachable (mypy-play).

Do you have a real-world example where this would matter? In the toy example here it seems like it's better to just simplify everything to result = False and remove the loop.

@KotlinIsland
Copy link
Contributor Author

mypy isn't smart enough to detect that the list l is always empty.

This issue isn't regarding an empty list, the else branch will always be executed here regardless of if l has items or not, and result will always be set to False

Note that technically the body of the loop is unreachable

Again, this isn't regarding a list that is empty, so the body is potentially reachable.

Do you have a real-world example where this would matter?

Yes, some projects I have lit up with false positives when I updated mypy. Basically any scenario where a for/else is used to set a fallback value.

@ilinum
Copy link
Collaborator

ilinum commented Nov 25, 2022

Yep! You're totally right. Thanks for pointing this out; sent a PR with the fix.

ilinum added a commit that referenced this issue Nov 27, 2022
`else` in loops is executed if the loop didn't exit via a raise, return,
or a break. Therefore, we should treat it execution as unconditional if
there is not a break statement.

This PR also improves error messages in `else`, reporting them as "may
be undefined" instead of "used before definition"

Fixes #14097

Co-authored-by: Ivan Levkivskyi <[email protected]>
@KotlinIsland
Copy link
Contributor Author

follow up: #14209

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-possibly-undefined possibly-undefined error code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants