-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Bad type inference with a partial None type and a for loop. #8637
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
Comments
I narrowed this down to this example: c = None
a = [1]
for c in a:
pass
reveal_type(c) # int (should be Optional[int]) I believe that this is not about tracking whether iterables are empty, but an issue with type inference with a partial Marking as high priority since this can lead to false negatives and cause issues with mypyc. |
Any hints on how to fix this? I'd like to take a shot. Updates: I did some tracing and summarize below:
I am still going through the details but I feel that this is caused by the incorrect conditional binder+name lookup behavior as a modified version of @JukkaL's example presented:
|
I believe there's a bug in
mypy
(frommaster
) which makes it not detect that a variabe used in a loop may be uninitialised (or use the value assigned before the loop) unless you are iterating over a literal[]
/()
.This code:
Prints:
But mypy says:
This is with mypy just updated from git and Python 3.7.4 but also happens with mypy 0.720 and Python 2.7:
If you replace
""
with a[]
or()
I get:It's also interesting that, if the loop iterates over a variable, then nothing potentially wrong is detected:
The text was updated successfully, but these errors were encountered: