Skip to content

Commit 7c754a6

Browse files
committed
Improve type: ignore for __mul__
These methods have a generic `type: ignore` with no clarification why. The issue is we need the ignore here because otherwise `mypy` will give this error: > Overloaded operator methods can't have wider argument types in > overrides The problem seems to be when the other type implements an **incompatible** __rmul__ method, which is not the case here, so we should be safe. Please see this example: https://github.com/python/mypy/blob/c26f1297d4f19d2d1124a30efc97caebb8c28616/test-data/unit/check-overloading.test#L4738C1-L4769C55 And a discussion in a mypy issue here: python/mypy#4985 (comment) For more information. This commit uses a more specific `type: ignore[override]` and add a comment clarifying this. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent f78393f commit 7c754a6

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,15 @@ def as_megawatts(self) -> float:
606606
"""
607607
return self._base_value / 1e6
608608

609-
@overload # type: ignore
609+
# We need the ignore here because otherwise mypy will give this error:
610+
# > Overloaded operator methods can't have wider argument types in overrides
611+
# The problem seems to be when the other type implements an **incompatible**
612+
# __rmul__ method, which is not the case here, so we should be safe.
613+
# Please see this example:
614+
# https://github.com/python/mypy/blob/c26f1297d4f19d2d1124a30efc97caebb8c28616/test-data/unit/check-overloading.test#L4738C1-L4769C55
615+
# And a discussion in a mypy issue here:
616+
# https://github.com/python/mypy/issues/4985#issuecomment-389692396
617+
@overload # type: ignore[override]
610618
def __mul__(self, scalar: float, /) -> Self:
611619
"""Scale this power by a scalar.
612620
@@ -758,7 +766,8 @@ def as_milliamperes(self) -> float:
758766
"""
759767
return self._base_value * 1e3
760768

761-
@overload # type: ignore
769+
# See comment for Power.__mul__ for why we need the ignore here.
770+
@overload # type: ignore[override]
762771
def __mul__(self, scalar: float, /) -> Self:
763772
"""Scale this current by a scalar.
764773
@@ -885,7 +894,8 @@ def as_kilovolts(self) -> float:
885894
"""
886895
return self._base_value / 1e3
887896

888-
@overload # type: ignore
897+
# See comment for Power.__mul__ for why we need the ignore here.
898+
@overload # type: ignore[override]
889899
def __mul__(self, scalar: float, /) -> Self:
890900
"""Scale this voltage by a scalar.
891901

0 commit comments

Comments
 (0)