Poor inference of dict literal when dict type is unioned with certain types
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
To Reproduce
https://mypy-play.net/?mypy=master&python=3.11&gist=5cf1ab002a6dc63c5013fa0f6a00ba4d
Expected Behavior
I expect the dict literal to be inferred as dict[str, str |int] when passed to both foo and bar
Actual Behavior
It's inferred as dict[str, object] when passed to foo, which causes a false positive.
Note that the inference depends on the type you union the dict with. E.g. if you replace Iterable[tuple[str] with list[tuple[str]] you get the expected inference.
Your Environment
- Mypy version used: Tried 1.0.0, 1.0.1, 1.1.1, 1.2.0 and master in the playground, all have the same behavior
- Mypy command-line flags: See playground
- Mypy configuration options from
mypy.ini(and other config files): See playground - Python version used: 3.11
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked mypy playground example using Python 3.11 and the reported versions to reproduce the differing inference for foo and bar. Trace the dict-literal inference behavior for unions involving Iterable[tuple[str]] and list[tuple[str]]. Done means the dict is inferred as dict[str, str | int] in both calls and a regression test covers the case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 38/100