__r<magic>__ methods for reflected (swapped) operands doesnt duck type int to float
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
🐛 Bug Report
It seems like ints are not compatible with floats when used in the __r<magic>__ methods for reflected (swapped) operands like __radd__ the same was as in normal operands if the int/float is used as a type parameter in a generic type.
As a workaround it works if I specify a union type with the int case as well, but would be nice to not have to do that since its not needed otherwise.
To Reproduce
code.py:
from typing import List
class Vec:
def __add__(self, other: List[float]) -> None:
pass
def __radd__(self, other: List[float]) -> None:
pass
r1 = Vec() + [1]
r2 = [1] + Vec() # error line
r3 = [1.0] + Vec()
Type checking the above code with python -m mypy code.py
Expected Behavior
I expect the r2 case to work as well
Actual Behavior
I get this error message for the r2 line: error: Unsupported operand types for + ("List[int]" and "Vec")
Your Environment
- Mypy version used: 0.782
- Mypy configuration options from
mypy.ini(and other config files): No special config - Python version used: CPython 3.8.4, 32 bit.
- Operating system and version: Windows 10
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 issue's code.py reproduction and run python -m mypy code.py using the reported cases. Trace how operand checking handles __add__ versus reflected __radd__ when List[int] is compared with List[float]; done means the r2 case type-checks without requiring a union workaround while existing cases remain correct.
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
- Clearly specified
- Newbie friendliness
- 45/100