Skip to content

Commit

Permalink
Allows addition and substraction between different property classes w…
Browse files Browse the repository at this point in the history
…ith same units
  • Loading branch information
Maxcode123 committed May 20, 2024
1 parent 25cde12 commit 2c7f27a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/property_utils/properties/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ def __add__(self, other) -> Self:
>>> x1 + x2
<Property: 20 m>
"""
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(
Expand Down Expand Up @@ -364,11 +364,10 @@ def __sub__(self, other) -> Self:
>>> t1 - t2
<Property: 1.0 min>
"""
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(
Expand All @@ -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.
Expand All @@ -400,7 +399,7 @@ def __rsub__(self, other) -> Self:
>>> t1 - t2
<Property: 1.0 min>
"""
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 "
Expand Down
34 changes: 34 additions & 0 deletions src/property_utils/tests/properties/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit 2c7f27a

Please sign in to comment.