Signatures of *in-place binary operation* and *binary operation* are incompatible
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
If I define custom binary operations (both regular & in-place versions) using __dunder__ methods like __add__ & __iadd__, I'm having odd
Signatures of "__iadd__" and "__add__" are incompatible
and I don't understand why, what is a "compatibility" the error is telling about?
To Reproduce
Example
from __future__ import annotations
from typing import Any, overload
class MyClass:
@overload
def __add__(self, other: MyClass) -> MyClass:
...
@overload
def __add__(self, other: Any) -> Any:
...
def __add__(self, other: Any) -> Any:
return (self
if isinstance(other, MyClass)
else NotImplemented)
@overload
def __iadd__(self, other: MyClass) -> MyClass:
...
@overload
def __iadd__(self, other: Any) -> Any:
...
def __iadd__(self, other: Any) -> Any:
if not isinstance(other, MyClass):
return NotImplemented
return self
https://mypy-play.net/?mypy=latest&python=3.8&gist=819e2627d0a17df446a5d7b2b3631f11
Expected Behavior
Ideally as a user of static type analyzer I want to get an error when I misuse a type hint for parameters/return type and get details about what's wrong with my code according to some rules, current message is too vague and the error isn't going away even when I put typing.Any everywhere in one of the methods annotations (only when I remove all typing.overloads). This is also the case when I use typing.Self instead of MyClass
Actual Behavior
error: Signatures of "__iadd__" and "__add__" are incompatible [misc]
Your Environment
- Mypy version used:
mypy 1.1.1 (compiled: yes) - Mypy command-line flags: both none &
--strict - Mypy configuration options from
mypy.ini(and other config files): none - Python version used:
Python3.8
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-play reproduction and compare how the checker evaluates the overloaded add and iadd methods. Trace the compatibility check for these binary-operation dunder signatures and establish the intended diagnostic or accepted typing behavior. Done means the reported case has a clear, justified result and a regression test covers it.
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
- Mostly clear
- Newbie friendliness
- 35/100