Skip to content

Commit 1ffe542

Browse files
committed
Allows addition and substraction between different property classes with same units
1 parent 25cde12 commit 1ffe542

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/property_utils/properties/property.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ def __add__(self, other) -> Self:
316316
>>> x1 + x2
317317
<Property: 20 m>
318318
"""
319-
if not isinstance(other, self.__class__):
319+
if not isinstance(other, Property):
320320
raise PropertyBinaryOperationError(
321-
f"cannot add {other} to ({self}); {other} is not a {self.__class__}; "
322-
"only same properties can be added to each other. "
321+
f"cannot add {other} to ({self}); {other} is not a Property; "
322+
"only properties can be added to properties. "
323323
)
324324
if not self.unit.isinstance_equivalent(other.unit.to_generic()):
325325
raise PropertyBinaryOperationError(
@@ -364,11 +364,10 @@ def __sub__(self, other) -> Self:
364364
>>> t1 - t2
365365
<Property: 1.0 min>
366366
"""
367-
if not isinstance(other, self.__class__):
367+
if not isinstance(other, Property):
368368
raise PropertyBinaryOperationError(
369369
f"cannot subtract {other} from ({self}); {other} is not a "
370-
f"{self.__class__}; only same properties can be subtracted from each "
371-
"other. "
370+
"Property; only properties can be subtracted from properties. "
372371
)
373372
if not self.unit.isinstance_equivalent(other.unit.to_generic()):
374373
raise PropertyBinaryOperationError(
@@ -389,7 +388,7 @@ def __sub__(self, other) -> Self:
389388
) from None
390389
return self.__class__(self.value - prop.value, self.unit)
391390

392-
def __rsub__(self, other) -> Self:
391+
def __rsub__(self, other) -> "Property":
393392
"""
394393
Defines right subtraction between properties.
395394
@@ -400,7 +399,7 @@ def __rsub__(self, other) -> Self:
400399
>>> t1 - t2
401400
<Property: 1.0 min>
402401
"""
403-
if not isinstance(other, self.__class__):
402+
if not isinstance(other, Property):
404403
raise PropertyBinaryOperationError(
405404
f"cannot subtract {self} from ({other}); {other} is not a "
406405
f"{self.__class__}; only same properties can be subtracted from each "

src/property_utils/tests/properties/test_property.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,20 @@ def test_with_other_aliased_units(self):
743743
self.assert_result("25.0 H")
744744

745745

746+
@add_to(property_test_suite)
747+
class TestDifferentPropertyAddition(TestProperty):
748+
def subject(self, left, right):
749+
return left + right
750+
751+
@args({"left": PropUnit1(10), "right": Property(10, Unit1.a)})
752+
def test_left_operand(self):
753+
self.assert_result("20 a")
754+
755+
@args({"left": Property(10, Unit1.a), "right": PropUnit1(10)})
756+
def test_right_operand(self):
757+
self.assert_result("20 a")
758+
759+
746760
@add_to(property_test_suite, "__sub__")
747761
class TestSimplePropertySubtraction(TestProperty):
748762

@@ -834,6 +848,16 @@ def test_with_other_aliased_units(self):
834848
self.assert_result("15.0 H")
835849

836850

851+
@add_to(property_test_suite)
852+
class TestDifferentPropertySubtraction(TestProperty):
853+
def subject(self, prop):
854+
return PropUnit1(10) - prop
855+
856+
@args({"prop": Property(5, Unit1.a)})
857+
def test_with_base_property_class(self):
858+
self.assert_result("5 a")
859+
860+
837861
@add_to(property_test_suite, "__rsub__")
838862
class TestSimplePropertyRightSubtraction(TestProperty):
839863

@@ -883,6 +907,16 @@ def test_with_other_aliased_si_units(self):
883907
self.assert_result("-60.0 f / (d^2)")
884908

885909

910+
@add_to(property_test_suite)
911+
class TestDifferentPropertyRightSubtraction(TestProperty):
912+
def subject(self, prop):
913+
return prop - PropUnit1(5)
914+
915+
@args({"prop": Property(10, Unit1.a)})
916+
def test_with_base_property_class(self):
917+
self.assert_result("5 a")
918+
919+
886920
@add_to(property_test_suite, "__pow__")
887921
class TestPropertyExponentiation(TestProperty):
888922

0 commit comments

Comments
 (0)