-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Variable loses type inference after passing through dictionary of known type #9369
Comments
Please look up the definition of dict.get in typeshed. Maybe there’s an explanation there? |
I believe that's |
Perhaps this is because the type solver takes the target type (i.e. the full, declared type of The error goes away when I change the code to this:
A shorter, self-contained repro is:
It is the last line that is surprising. FYI pyright has the same results. Pyre says the inferred type is I'm labeling this as a bug, low priority (since there's an easy override). |
This comment has been minimized.
This comment has been minimized.
@adigitoleo
|
@JelleZijlstra Thanks for all the info! I'll hide my comment so it's not confused with this issue. |
Some updates: #6755 was fixed in mypy 0.930, but this was not, so probably not related.
Links to reproduce: |
Looks like your mypy-play link passes as of mypy 1.0.0. Fixed by #14151 |
🐛 Bug Report
(Possibly related to #6755?)
When a variable is declared to be of a type including
Optional
, mypy correctly infers that it is notNone
when passing through an assert indicating as such. However, if mapped through a dictionary with a known type (either by inference or declaration), mypy loses that inference and reverts to the original type definition.To Reproduce
Expected Behavior
The type of
x
should remain inferred asstr
incheck2
, because both possible return values fromx_map.get(x, x)
are known to bestr
.Actual Behavior
Your Environment
mypy --strict
The text was updated successfully, but these errors were encountered: