From 2c7f27a3a19ed77a92de5308b15d953bfa42fc80 Mon Sep 17 00:00:00 2001 From: Maximos Nikiforakis Date: Mon, 20 May 2024 15:13:18 +0300 Subject: [PATCH] Allows addition and substraction between different property classes with same units --- src/property_utils/properties/property.py | 15 ++++---- .../tests/properties/test_property.py | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/property_utils/properties/property.py b/src/property_utils/properties/property.py index 95e3385..92d77f6 100644 --- a/src/property_utils/properties/property.py +++ b/src/property_utils/properties/property.py @@ -316,10 +316,10 @@ def __add__(self, other) -> Self: >>> x1 + x2 """ - if not isinstance(other, self.__class__): + if not isinstance(other, Property): raise PropertyBinaryOperationError( - f"cannot add {other} to ({self}); {other} is not a {self.__class__}; " - "only same properties can be added to each other. " + f"cannot add {other} to ({self}); {other} is not a Property; " + "only properties can be added to properties. " ) if not self.unit.isinstance_equivalent(other.unit.to_generic()): raise PropertyBinaryOperationError( @@ -364,11 +364,10 @@ def __sub__(self, other) -> Self: >>> t1 - t2 """ - if not isinstance(other, self.__class__): + if not isinstance(other, Property): raise PropertyBinaryOperationError( f"cannot subtract {other} from ({self}); {other} is not a " - f"{self.__class__}; only same properties can be subtracted from each " - "other. " + "Property; only properties can be subtracted from properties. " ) if not self.unit.isinstance_equivalent(other.unit.to_generic()): raise PropertyBinaryOperationError( @@ -389,7 +388,7 @@ def __sub__(self, other) -> Self: ) from None return self.__class__(self.value - prop.value, self.unit) - def __rsub__(self, other) -> Self: + def __rsub__(self, other) -> "Property": """ Defines right subtraction between properties. @@ -400,7 +399,7 @@ def __rsub__(self, other) -> Self: >>> t1 - t2 """ - if not isinstance(other, self.__class__): + if not isinstance(other, Property): raise PropertyBinaryOperationError( f"cannot subtract {self} from ({other}); {other} is not a " f"{self.__class__}; only same properties can be subtracted from each " diff --git a/src/property_utils/tests/properties/test_property.py b/src/property_utils/tests/properties/test_property.py index 4a79ec6..079c1e5 100644 --- a/src/property_utils/tests/properties/test_property.py +++ b/src/property_utils/tests/properties/test_property.py @@ -743,6 +743,20 @@ def test_with_other_aliased_units(self): self.assert_result("25.0 H") +@add_to(property_test_suite) +class TestDifferentPropertyAddition(TestProperty): + def subject(self, left, right): + return left + right + + @args({"left": PropUnit1(10), "right": Property(10, Unit1.a)}) + def test_left_operand(self): + self.assert_result("20 a") + + @args({"left": Property(10, Unit1.a), "right": PropUnit1(10)}) + def test_right_operand(self): + self.assert_result("20 a") + + @add_to(property_test_suite, "__sub__") class TestSimplePropertySubtraction(TestProperty): @@ -834,6 +848,16 @@ def test_with_other_aliased_units(self): self.assert_result("15.0 H") +@add_to(property_test_suite) +class TestDifferentPropertySubtraction(TestProperty): + def subject(self, prop): + return PropUnit1(10) - prop + + @args({"prop": Property(5, Unit1.a)}) + def test_with_base_property_class(self): + self.assert_result("5 a") + + @add_to(property_test_suite, "__rsub__") class TestSimplePropertyRightSubtraction(TestProperty): @@ -883,6 +907,16 @@ def test_with_other_aliased_si_units(self): self.assert_result("-60.0 f / (d^2)") +@add_to(property_test_suite) +class TestDifferentPropertyRightSubtraction(TestProperty): + def subject(self, prop): + return prop - PropUnit1(5) + + @args({"prop": Property(10, Unit1.a)}) + def test_with_base_property_class(self): + self.assert_result("5 a") + + @add_to(property_test_suite, "__pow__") class TestPropertyExponentiation(TestProperty):