Signatures of "__ior__" and "__or__" are incompatible when assigning __or__ with compatible method
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Dear mypy team,
I was implementing arithmetic operations on a custom class and ran into the following issue:
When I don't define __or__ in the class body, but assign it with a function with a compatible type, mypy complains about incompatible signatures of __ior__ and __or__. (This is also true for other arithmetic dunders, see example).
I wanted to only allow inplace operations on that class and throw a common error on non-inplace operations.
To Reproduce
# this works and does not throw an error
class A:
def __or__(self, v: int) -> A:...
def __ior__(self, v: int) -> None: ...
reveal_type(__or__) # note: Revealed type is "def (_error.A, builtins.int) -> _error.A"
reveal_type(__ior__) # note: Revealed type is "def (_error.A, builtins.int)"
# this errors
class B:
def _no(self, v: int) -> B: ...
__or__ = _no
def __ior__(self, v: int) -> None: ... # error: Signatures of "__ior__" and "__or__" are incompatible
reveal_type(__or__) # note: Revealed type is "def (self: _error.B, v: builtins.int) -> _error.B"
reveal_type(__ior__) # note: Revealed type is "def (_error.B, builtins.int)"
NOTE: the same is true for __and__, __xor__, __add__, __sub__, __mul__, __truediv__, __floordiv__. (I haven't tested others.
Your Environment
- Mypy version used: 0.931
- Python version used: python3.9
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 reported difference between defining or in class A and assigning or from _no in class B, using the Python 3.9 example and mypy 0.931 behavior. Trace the type-checking path that compares ior and or signatures, then verify the result across the other arithmetic dunder methods listed in the report. Done means compatible assigned methods no longer produce the erroneous incompatibility error while genuinely incompatible signatures still do.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100