Skip to content

Commit 579836c

Browse files
committed
Converts empty CompositeDimension to NON_DIMENSIONAL when produced from multiplication or division
1 parent 258ac67 commit 579836c

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/property_utils/properties/property.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def __mul__(self, other) -> "Property":
217217
if isinstance(other, Property):
218218
_other = self._unit_preconversion(other)
219219
return Property(
220-
self.value * _other.value, (self.unit * _other.unit).simplified()
220+
self.value * _other.value, self._simplify_units(self.unit * _other.unit)
221221
)
222222
raise PropertyBinaryOperationError(
223223
f"cannot multiply {self} with {other}; "
@@ -268,7 +268,7 @@ def __truediv__(self, other) -> "Property":
268268
raise PropertyBinaryOperationError(
269269
f"cannot divide {self} with {other}; denominator's value is zero. "
270270
) from None
271-
return Property(value, (self.unit / _other.unit).simplified())
271+
return Property(value, self._simplify_units(self.unit / _other.unit))
272272
raise PropertyBinaryOperationError(
273273
f"cannot divide {self} with {other}; "
274274
"denominator must be numeric or Property. "
@@ -299,7 +299,7 @@ def __rtruediv__(self, other) -> "Property":
299299
raise PropertyBinaryOperationError(
300300
f"cannot divide {self} with {other}; denominator's value is zero. "
301301
) from None
302-
return Property(value, (other.unit / self.unit).simplified())
302+
return Property(value, self._simplify_units(other.unit / self.unit))
303303
raise PropertyBinaryOperationError(
304304
f"cannot divide {self} with {other}; "
305305
"numerator must be numeric or Property. "
@@ -727,3 +727,15 @@ def _simple_unit_preconversion(self, unit: MeasurementUnit) -> MeasurementUnit:
727727
multiplication or division with this property.
728728
"""
729729
return self._dimension_unit_preconversion(unit**1).unit
730+
731+
def _simplify_units(self, unit: CompositeDimension) -> UnitDescriptor:
732+
"""
733+
Simplifies the composite dimension and returns NON_DIMENSIONAL if the simplified
734+
composite does not have units.
735+
"""
736+
unit = unit.simplified()
737+
738+
if unit.has_no_units():
739+
return NonDimensionalUnit.NON_DIMENSIONAL
740+
741+
return unit

src/property_utils/tests/properties/test_property.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ def test_with_exponentiated_property(self):
346346
def test_with_inverse_property(self):
347347
self.assert_result("10.4 ")
348348

349+
@args({"other": Property(2, Unit1.A ** (-1))})
350+
def test_produced_units_with_inverse_property(self):
351+
self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL)
352+
349353
@args({"other": Property(4, Unit2.B)})
350354
def test_with_other_propperty(self):
351355
self.assert_result("20.8 A * B")
@@ -499,6 +503,10 @@ def test_with_exponentiated_property(self):
499503
def test_with_same_property(self):
500504
self.assert_result("3.0 ")
501505

506+
@args({"other": Property(2, Unit1.A)})
507+
def test_produced_units_with_same_property(self):
508+
self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL)
509+
502510
@args({"other": Property(2, Unit2.B)})
503511
def test_with_other_propperty(self):
504512
self.assert_result("3.0 A / B")
@@ -639,6 +647,10 @@ def test_with_exponentiated_property(self):
639647
def test_with_same_property(self):
640648
self.assert_result("0.2 ")
641649

650+
@args({"other": Property(2, Unit1.A)})
651+
def test_produced_units_with_same_property(self):
652+
self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL)
653+
642654
@args({"other": Property(2, Unit2.B)})
643655
def test_with_other_propperty(self):
644656
self.assert_result("0.2 B / A")

0 commit comments

Comments
 (0)