Confusing error from bidirectional inference?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
When the return type from dict.pop is incompatible with existing variable type, mypy gives an arg-type error when an assignment error seems more intuitive.
(Originally noted in #10152)
To Reproduce
values_by_name = {"one": 1, "two": 2}
value: int
value = values_by_name.pop("one", None)
https://gist.github.com/mypy-play/972f4514ebe5620ece42583192907f62
Expected Behavior
example.py:3: error: Incompatible types in assignment (expression has type "int | None", variable has type "int") [assignment]
Actual Behavior
example.py:3: error: Argument 2 to "pop" of "dict" has incompatible type "None"; expected "int" [arg-type]
Note: this is a lot more surprising if value has this type because it was unintentionally reused from some code further up, e.g. if you're using value in a loop or something; it can be very difficult to spot what the real error is. See https://github.com/python/mypy/issues/10152#issuecomment-2538685996 for example.
Your Environment
- Mypy version used: 1.13.0
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.12.2
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 by reproducing the diagnostic from the issue in example.py with mypy 1.13.0, then trace how bidirectional inference handles the dict.pop default and the existing variable type. Done means the example reports an incompatible assignment with [assignment] rather than an argument-type error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100