-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Dictionary infering value type #7244
Comments
The revealed types on linen 10 and 18 are simplified union types. Mypy considers Do the error messages make sense assuming unions are simplified, or there another issue with the errors mypy is reporting? |
That makes sense too me. Its indeed a bit confusing. I see I made a typo though, I meant that the error shouldn't happen. At least thats what I thing. The error on line 11 and on line 19 are incorrect. |
Errors on lines 11 and 19 are as expected. You can avoid the errors by having a separate variable type declaration and assignment. This causes mypy to narrow the type down. This doesn't happen if you declare the type and assign a value in the same statement. Example: from typing import Dict, Union
D = Union[Dict[int, str], Dict[int, int], Dict[int, bool], Dict[int, float]]
d: D # Note separate declaration
d = {
0: 1,
1: 2
}
reveal_type(d[0])
d[0] = 123 # No error I think that this might not be properly documented, though. |
I've tried with the seperate declaration and it seems to work, thanks! |
I added an issue about documenting this #7252. |
Is it possible for mypy to infer the value of an dictionary?
I've got a problem with value types not being recognized correctly.
Example:
mypy output
The revealed type on line 10 and 18 also don't seem to be right.
The text was updated successfully, but these errors were encountered: