From f8a4e08c613b9cf07eee2b7612c9f024da83a5a8 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 10 Apr 2024 21:14:36 +0100 Subject: [PATCH] kinfs --- pint/delegates/txt_defparser/plain.py | 4 ++-- pint/facets/plain/definitions.py | 2 +- pint/facets/plain/quantity.py | 13 ++++++++++--- pint/testsuite/test_kind.py | 10 ++++++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pint/delegates/txt_defparser/plain.py b/pint/delegates/txt_defparser/plain.py index bbffd08c1..9f9776cac 100644 --- a/pint/delegates/txt_defparser/plain.py +++ b/pint/delegates/txt_defparser/plain.py @@ -235,8 +235,8 @@ def from_string_and_config( if not (s.startswith("[") and "=" in s): return None - name, value, *aliases = (p.strip() for p in s.split("=")) - + name, value, *aliases = (p.strip() for p in s.split("=")) + preferred_unit = None if aliases: if aliases[0] == "_": diff --git a/pint/facets/plain/definitions.py b/pint/facets/plain/definitions.py index dde6d9938..06ceb4770 100644 --- a/pint/facets/plain/definitions.py +++ b/pint/facets/plain/definitions.py @@ -234,7 +234,7 @@ class DerivedDimensionDefinition(DimensionDefinition): #: reference dimensions. reference: UnitsContainer - # preferred unit + # preferred unit preferred_unit: str | None = None @property diff --git a/pint/facets/plain/quantity.py b/pint/facets/plain/quantity.py index 9ea65e1c4..453e6b717 100644 --- a/pint/facets/plain/quantity.py +++ b/pint/facets/plain/quantity.py @@ -76,11 +76,18 @@ def wrapped(self, *args, **kwargs): result = f(self, *args, **kwargs) print(args[0].kind, self.kind, f.__name__) # TODO: Implement this for div, maybe move to mul and div funcs - if hasattr(self, "kind") and hasattr(args[0], "kind") and f.__name__ in ["_mul_div"]: + if ( + hasattr(self, "kind") + and hasattr(args[0], "kind") + and f.__name__ in ["_mul_div"] + ): result_units_container = UnitsContainer({self.kind: 1, args[0].kind: 1}) print(args[0].kind, self.kind) for dim in self._REGISTRY._dimensions.values(): - if hasattr(dim, "reference") and dim.reference == result_units_container: + if ( + hasattr(dim, "reference") + and dim.reference == result_units_container + ): return result.to_kind(dim.name) raise ValueError( f"Cannot multiply quantities with kinds {self.kind} and {args[0].kind}" @@ -776,7 +783,7 @@ def _add_sub(self, other, op): raise DimensionalityError( self._units, other._units, self.dimensionality, other.dimensionality ) - + if hasattr(self, "kind") and hasattr(other, "kind"): if self.kind != other.kind: # TODO: Should this be a KindError? diff --git a/pint/testsuite/test_kind.py b/pint/testsuite/test_kind.py index b6134c373..9e6ecd801 100644 --- a/pint/testsuite/test_kind.py +++ b/pint/testsuite/test_kind.py @@ -1,8 +1,11 @@ -from pint import UnitRegistry +from __future__ import annotations + import pytest -class TestKind(): +from pint import UnitRegistry + +class TestKind: def test_torque_energy(self): ureg = UnitRegistry() Q_ = ureg.Quantity @@ -10,7 +13,7 @@ def test_torque_energy(self): force = Q_(1, "lbf").to_kind("[force]") # to_kind converts to the preferred_unit of the kind assert force.units == ureg.N - + # both force and moment_arm have kind defined. # Torque is defined in default_en: # [torque] = [force] * [moment_arm] @@ -29,4 +32,3 @@ def test_torque_energy(self): # Torque is not energy so cannot be added with pytest.raises(ValueError): energy + torque - \ No newline at end of file