Open
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 #10152 (comment) 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
Metadata
Metadata
Assignees
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
alicederyn commentedon Dec 16, 2024
This may be the same issue as #14773 and #15408 ?