From 11a9c6d3a2176742a586cd15ca6928cd3644a085 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Mon, 27 May 2024 08:47:00 +0200 Subject: [PATCH 1/3] add error for prefixed non multi units --- pint/facets/plain/registry.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py index 09fd220ee..de7f2b4c5 100644 --- a/pint/facets/plain/registry.py +++ b/pint/facets/plain/registry.py @@ -60,7 +60,7 @@ UnitLike, ) from ...compat import Self, TypeAlias, deprecated -from ...errors import DimensionalityError, RedefinitionError, UndefinedUnitError +from ...errors import DimensionalityError, RedefinitionError, UndefinedUnitError, OffsetUnitCalculusError from ...pint_eval import build_eval_tree from ...util import ( ParserHelper, @@ -664,6 +664,9 @@ def get_name(self, name_or_alias: str, case_sensitive: bool | None = None) -> st ) if prefix: + if not self._units[unit_name].is_multiplicative: + raise OffsetUnitCalculusError("Prefixing a unit requires multiplying the unit.") + name = prefix + unit_name symbol = self.get_symbol(name, case_sensitive) prefix_def = self._prefixes[prefix] From c3a345e81a46da66b072f366bc67a4017d0a1418 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Mon, 27 May 2024 15:29:56 +0200 Subject: [PATCH 2/3] test --- pint/facets/plain/registry.py | 13 ++++++++++--- pint/testsuite/test_unit.py | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py index de7f2b4c5..b459635f9 100644 --- a/pint/facets/plain/registry.py +++ b/pint/facets/plain/registry.py @@ -60,7 +60,12 @@ UnitLike, ) from ...compat import Self, TypeAlias, deprecated -from ...errors import DimensionalityError, RedefinitionError, UndefinedUnitError, OffsetUnitCalculusError +from ...errors import ( + DimensionalityError, + OffsetUnitCalculusError, + RedefinitionError, + UndefinedUnitError, +) from ...pint_eval import build_eval_tree from ...util import ( ParserHelper, @@ -665,8 +670,10 @@ def get_name(self, name_or_alias: str, case_sensitive: bool | None = None) -> st if prefix: if not self._units[unit_name].is_multiplicative: - raise OffsetUnitCalculusError("Prefixing a unit requires multiplying the unit.") - + raise OffsetUnitCalculusError( + "Prefixing a unit requires multiplying the unit." + ) + name = prefix + unit_name symbol = self.get_symbol(name, case_sensitive) prefix_def = self._prefixes[prefix] diff --git a/pint/testsuite/test_unit.py b/pint/testsuite/test_unit.py index 2156bbafd..1cca93cec 100644 --- a/pint/testsuite/test_unit.py +++ b/pint/testsuite/test_unit.py @@ -1043,3 +1043,8 @@ def test_alias(self): # Define against unknown name with pytest.raises(KeyError): ureg.define("@alias notexist = something") + + def test_prefix_offset_units(self): + ureg = UnitRegistry() + with pytest.raises(errors.OffsetUnitCalculusError): + ureg.parse_units("kilodegree_Celsius") From e8f11b598a31b2b3ae72c0a1e5983c442bd18f34 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Wed, 29 May 2024 23:32:17 +0100 Subject: [PATCH 3/3] changes --- CHANGES | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES b/CHANGES index 8243e3471..6a7735552 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,8 @@ Pint Changelog (PR #1949) - Fix unhandled TypeError when auto_reduce_dimensions=True and non_int_type=Decimal (PR #1853) +- Creating prefixed offset units now raises an error. + (PR #1998) - Improved error message in `get_dimensionality()` when non existent units are passed. (PR #1874, Issue #1716)