Skip to content

Commit 1cd226b

Browse files
committed
Remove dunder iadd, isub and imul methods for Quantity
Having them mutating the objects in-place is not the best option as number are assumed to be immutable in Python. If we make them mutable, then bad things will happen if they are used as default arguments in functions, or if we want to provide a zero singleton for example. We also mention explicitly now that Quantities are immutable. This partially reverts 1043360 (the tests are kept). Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 84ade2f commit 1cd226b

File tree

1 file changed

+10
-49
lines changed

1 file changed

+10
-49
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424

2525

2626
class Quantity:
27-
"""A quantity with a unit."""
27+
"""A quantity with a unit.
28+
29+
Quantities try to behave like float and are also immutable.
30+
"""
2831

2932
_base_value: float
3033
"""The value of this quantity in the base unit."""
@@ -249,48 +252,6 @@ def __mul__(self, percent: Percentage) -> Self:
249252
product._base_value = self._base_value * percent.as_fraction()
250253
return product
251254

252-
def __iadd__(self, other: Self) -> Self:
253-
"""Add another quantity to this one.
254-
255-
Args:
256-
other: The other quantity.
257-
258-
Returns:
259-
This quantity.
260-
"""
261-
if not type(other) is type(self):
262-
return NotImplemented
263-
self._base_value += other._base_value
264-
return self
265-
266-
def __isub__(self, other: Self) -> Self:
267-
"""Subtract another quantity from this one.
268-
269-
Args:
270-
other: The other quantity.
271-
272-
Returns:
273-
This quantity.
274-
"""
275-
if not type(other) is type(self):
276-
return NotImplemented
277-
self._base_value -= other._base_value
278-
return self
279-
280-
def __imul__(self, percent: Percentage) -> Self:
281-
"""Multiply this quantity by a percentage.
282-
283-
Args:
284-
percent: The percentage.
285-
286-
Returns:
287-
This quantity.
288-
"""
289-
if not isinstance(percent, Percentage):
290-
return NotImplemented
291-
self._base_value *= percent.as_fraction()
292-
return self
293-
294255
def __gt__(self, other: Self) -> bool:
295256
"""Return whether this quantity is greater than another.
296257
@@ -413,7 +374,7 @@ class Power(
413374
):
414375
"""A power quantity.
415376
416-
Objects of this type are wrappers around `float` values.
377+
Objects of this type are wrappers around `float` values and are immutable.
417378
418379
The constructors accept a single `float` value, the `as_*()` methods return a
419380
`float` value, and each of the arithmetic operators supported by this type are
@@ -586,7 +547,7 @@ class Current(
586547
):
587548
"""A current quantity.
588549
589-
Objects of this type are wrappers around `float` values.
550+
Objects of this type are wrappers around `float` values and are immutable.
590551
591552
The constructors accept a single `float` value, the `as_*()` methods return a
592553
`float` value, and each of the arithmetic operators supported by this type are
@@ -682,7 +643,7 @@ class Voltage(
682643
):
683644
"""A voltage quantity.
684645
685-
Objects of this type are wrappers around `float` values.
646+
Objects of this type are wrappers around `float` values and are immutable.
686647
687648
The constructors accept a single `float` value, the `as_*()` methods return a
688649
`float` value, and each of the arithmetic operators supported by this type are
@@ -804,7 +765,7 @@ class Energy(
804765
):
805766
"""An energy quantity.
806767
807-
Objects of this type are wrappers around `float` values.
768+
Objects of this type are wrappers around `float` values and are immutable.
808769
809770
The constructors accept a single `float` value, the `as_*()` methods return a
810771
`float` value, and each of the arithmetic operators supported by this type are
@@ -923,7 +884,7 @@ class Frequency(
923884
):
924885
"""A frequency quantity.
925886
926-
Objects of this type are wrappers around `float` values.
887+
Objects of this type are wrappers around `float` values and are immutable.
927888
928889
The constructors accept a single `float` value, the `as_*()` methods return a
929890
`float` value, and each of the arithmetic operators supported by this type are
@@ -1036,7 +997,7 @@ class Percentage(
1036997
):
1037998
"""A percentage quantity.
1038999
1039-
Objects of this type are wrappers around `float` values.
1000+
Objects of this type are wrappers around `float` values and are immutable.
10401001
10411002
The constructors accept a single `float` value, the `as_*()` methods return a
10421003
`float` value, and each of the arithmetic operators supported by this type are

0 commit comments

Comments
 (0)