Skip to content

Commit 5dc8393

Browse files
committed
Add early exit to Property.to_unit if units are equal
1 parent b09862f commit 5dc8393

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/property_utils/properties/property.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ def to_unit(self, unit: UnitDescriptor) -> Self:
159159
>>> T.to_unit(RelativeTemperatureUnit.FAHRENHEIT)
160160
<Property: 212.0 °F>
161161
"""
162+
if self.unit == unit:
163+
return self.__class__(unit=self.unit, value=self.value)
164+
162165
if not unit.isinstance_equivalent(self.unit.to_generic()):
163166
raise PropertyUnitConversionError(
164167
f"cannot convert {self} to ({unit}) units; 'unit' should be an instance"

src/property_utils/tests/properties/test_property.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ def subject(self, unit):
167167

168168
@args({"unit": Unit1.a})
169169
def test_with_simple_si_units(self):
170-
self.assert_result("200.0 a")
170+
self.assert_result("200 a")
171171

172172
@args({"unit": Unit1.a**2})
173173
def test_with_dimension_si_units(self):
174-
self.assert_result("200.0 (a^2)")
174+
self.assert_result("200 (a^2)")
175175

176176
@args({"unit": (Unit1.a**2) / (Unit4.d**3)})
177177
def test_with_composite_si_units(self):
178-
self.assert_result("200.0 (a^2) / (d^3)")
178+
self.assert_result("200 (a^2) / (d^3)")
179179

180180
@args({"unit": Unit1.A})
181181
def test_with_simple_non_si_units(self):
@@ -210,7 +210,7 @@ def test_with_different_unit_type(self):
210210

211211
@args({"unit": Unit1.A})
212212
def test_with_same_unit(self):
213-
self.assert_result("105.0 A")
213+
self.assert_result("105 A")
214214

215215
@args({"unit": Unit1.a})
216216
def test_with_other_unit(self):
@@ -233,7 +233,7 @@ def test_with_different_dimension(self):
233233

234234
@args({"unit": Unit1.A**2})
235235
def test_with_same_dimension(self):
236-
self.assert_result("52.0 (A^2)")
236+
self.assert_result("52 (A^2)")
237237

238238
@args({"unit": Unit1.a**2})
239239
def test_with_other_unit(self):
@@ -256,7 +256,7 @@ def test_with_different_composite_dimension(self):
256256

257257
@args({"unit": (Unit1.A**2) / (Unit4.D**3)})
258258
def test_with_same_composite_dimension(self):
259-
self.assert_result("20.0 (A^2) / (D^3)")
259+
self.assert_result("20 (A^2) / (D^3)")
260260

261261
@args({"unit": (Unit1.a**2) / (Unit4.d**3)})
262262
def test_with_other_unit(self):

0 commit comments

Comments
 (0)