-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
Description
Bug Report
When attempting to add overrides to the __mul__ and __rmul__ magic methods, different behaviour is observed for the same set of overrides.
This seems similar to the issue in #10755, but that has since been fixed.
To Reproduce
Playground link: https://mypy-play.net/?mypy=latest&python=3.12&gist=1297febcc3b38a2eea422e7073842c31
(Note that this playground is for the latest version (1.14.1), but the same errors are observed on master)
Sample program:
from typing import TypeVar, overload
from decimal import Decimal
TAmount = TypeVar("TAmount", int, Decimal)
class Unit:
@overload
def __mul__(self, other: Unit) -> Unit: ...
@overload
def __mul__(self, other: TAmount) -> Quantity[TAmount]: ...
def __mul__(self, other: Unit | TAmount) -> Unit | Quantity[TAmount]:
raise NotImplementedError
@overload
def __rmul__(self, other: Unit) -> Unit: ...
@overload
def __rmul__(self, other: TAmount) -> Quantity[TAmount]: ...
def __rmul__(self, other: Unit | TAmount) -> Unit | Quantity[TAmount]:
raise NotImplementedError
class Quantity[TAmount]:
def __init__(self, value: TAmount, units: Unit) -> None:
self.value = value
self.units = unitsExpected Behavior
I expect this program to typecheck successfull (or error for both __mul__ and __rmul__)
Actual Behavior
main.py:22: error: Signatures of "__rmul__" of "Unit" and "__mul__" of "Unit | int" are unsafely overlapping [misc]
main.py:22: error: Signatures of "__rmul__" of "Unit" and "__mul__" of "Unit | Decimal" are unsafely overlapping [misc]
Found 2 errors in 1 file (checked 1 source file)
jorenham