diff --git a/src/barril/units/_quantity.py b/src/barril/units/_quantity.py index ecfb060..0faab7c 100644 --- a/src/barril/units/_quantity.py +++ b/src/barril/units/_quantity.py @@ -9,6 +9,7 @@ UnitDatabase, UnitsError, FixUnitIfIsLegacy, + ComposedUnitError, ) from oop_ext.interface._interface import ImplementsInterface @@ -677,9 +678,17 @@ def Convert(self, value, to_unit): :returns: An object with values to the passed unit. """ - return self._unit_database.Convert( - self._category, self._CreateUnitsWithJoinedExponentsString(), to_unit, value - ) + try: + return self._unit_database.Convert( + self._composing_categories, self._composing_units, to_unit, value + ) + except ComposedUnitError: + return self._unit_database.Convert( + self._category, + self._CreateUnitsWithJoinedExponentsString(), + to_unit, + value, + ) @classmethod def _GetComparison(cls, operator, use_literals=False): diff --git a/src/barril/units/_tests/test_scalar.py b/src/barril/units/_tests/test_scalar.py index aaaf305..0a9810a 100644 --- a/src/barril/units/_tests/test_scalar.py +++ b/src/barril/units/_tests/test_scalar.py @@ -383,6 +383,13 @@ class MyScalar(units.Scalar): scalar = Scalar("length", 1.18, "m") assert scalar.GetFormatted() == "1.18 [m]" + # Check that squared works + scalar2 = scalar * scalar + assert scalar2.GetFormatted("m2") == "1.3924 [m2]" + assert scalar2.GetFormattedValue("m2") == "1.3924" + assert scalar2.GetFormatted((("m", 2),)) == "1.3924 [('m', 2)]" + assert scalar2.GetFormattedValue((("m", 2),)) == "1.3924" + def testCopyProperties(unit_database_well_length): """