We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Bug Report
Strange inference with dict.
dict
To Reproduce
The repro is found in this Mypy playground, but here it is for convenience:
import random value: str|None = None if random.random() > 0.5: my_dict = {'a': False, 'b': None} else: my_dict = {'a': True, 'b': value}
Expected Behavior
No error.
Actual Behavior
Error: main.py:8: error: Dict entry 1 has incompatible type "str": "Optional[str]"; expected "str": "Optional[bool]" [dict-item
main.py:8: error: Dict entry 1 has incompatible type "str": "Optional[str]"; expected "str": "Optional[bool]" [dict-item
Your Environment
The text was updated successfully, but these errors were encountered:
I believe this is the same issue as #2008.
Sorry, something went wrong.
This isn't related to #2008, which talks about how to narrow types of symbols that have a declared type.
There is no declared type here, adding one would remove the error, e.g.
my_dict: dict[str, bool | str | None] if random.random() > 0.5: my_dict = {'a': False, 'b': None} else: my_dict = {'a': True, 'b': value}
You may also want to use a TypedDict
No branches or pull requests
Bug Report
Strange inference with
dict
.To Reproduce
The repro is found in this Mypy playground, but here it is for convenience:
Expected Behavior
No error.
Actual Behavior
Error:
main.py:8: error: Dict entry 1 has incompatible type "str": "Optional[str]"; expected "str": "Optional[bool]" [dict-item
Your Environment
The text was updated successfully, but these errors were encountered: