Skip to content

Commit

Permalink
Merge branch '8-universal-property-constructor'
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Mar 19, 2024
2 parents ca85e21 + 8684963 commit b6b2af0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/property_utils/properties/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
except ImportError:
from typing_extensions import Self # Python <= 3.10

try:
from typing import TypeAlias # Python >= 3.10 pylint: disable=ungrouped-imports
except ImportError:
from typing_extensions import TypeAlias # Python < 3.10

from property_utils.units.descriptors import (
MeasurementUnit,
Dimension,
Expand Down Expand Up @@ -568,3 +573,6 @@ def _validate_comparison_input(self, other) -> None:
f"cannot compare ({other}) to ({self}); "
f"({other}) must have ({self.unit.to_generic()}) units. "
)


p: TypeAlias = Property
4 changes: 2 additions & 2 deletions src/property_utils/tests/properties/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def test_with_dimension_non_si_units(self):
def test_with_composite_non_si_units(self):
self.assert_result("400.0 a / d")

@args({"unit": Unit1.A**8.19})
@args({"unit": Unit1.A**4})
def test_with_dimension_unregistered_unit_converter(self):
self.assert_impossible_conversion()
self.assert_result("2000000.0 (a^4)")

@args({"unit": Unit3.C})
def test_with_simple_unit_unregistered_unit_converter(self):
Expand Down
20 changes: 20 additions & 0 deletions src/property_utils/tests/units/test_converter_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ def test_with_generic_composite_dimension(self):
self.assertResult(Unit1Unit4Converter)


@add_to(GetConverter_test_suite)
class TestGetUnregisteredDimensionConverter(TestCase):
def subject(self, generic):
return get_converter(generic)

def assert_convert(self, value, from_unit, to_unit):
self.assertResultIsNot(None)
self.assertEqual(self.cachedResult().convert(10, from_unit, to_unit), value)

@args({"generic": Unit1 * (Unit4**2)})
def test_with_unregistered_composite_dimension(self):
self.assert_convert(
10 * 10 * (5**2), Unit1.A * (Unit4.D**2), Unit1.a * (Unit4.d**2)
)

@args({"generic": Unit1**2.45})
def test_with_unregistered_dimension(self):
self.assert_convert(10 * (10**2.45), Unit1.A**2.45, Unit1.a**2.45)


@add_to(RegisterConverter_test_suite)
class TestRegisterConverter(TestCase):
def subject(self, generic):
Expand Down
12 changes: 10 additions & 2 deletions src/property_utils/units/converter_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,17 @@ def get_converter(generic: GenericUnitDescriptor) -> ConverterType:
raise PropertyUtilsTypeError(
f"cannot get converter; argument: {generic} is not a generic unit descriptor. "
)
if isinstance(generic, GenericDimension) and generic.power == 1:
generic = generic.unit_type
elif isinstance(generic, GenericDimension) and generic not in _converters:
register_converter(generic)(
type(f"{generic}_Converter", (ExponentiatedUnitConverter,), {})
)
elif isinstance(generic, GenericCompositeDimension) and generic not in _converters:
register_converter(generic)(
type(f"{generic}_Converter", (CompositeUnitConverter,), {})
)
try:
if isinstance(generic, GenericDimension) and generic.power == 1:
generic = generic.unit_type
return _converters[generic]
except KeyError:
raise UndefinedConverterError(
Expand Down

0 comments on commit b6b2af0

Please sign in to comment.