diff --git a/CHANGES b/CHANGES index 4c8a444ff..4b4884d54 100644 --- a/CHANGES +++ b/CHANGES @@ -33,6 +33,8 @@ Pint Changelog - Add `dim_sort` function to _formatter_helpers. - Add `dim_order` and `default_sort_func` properties to FullFormatter. (PR #1926, fixes Issue #1841) +- Minimum version requirement added for typing_extensions>=4.0.0. + (PR #1996) - Documented packages using pint. (PR #1960) - Fixed bug causing operations between arrays of quantity scalars and quantity holding @@ -46,6 +48,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) diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py index b0b52f99d..c8ce3f2f0 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 +from ...errors import ( + DimensionalityError, + OffsetUnitCalculusError, + RedefinitionError, + UndefinedUnitError, +) from ...pint_eval import build_eval_tree from ...util import ( ParserHelper, @@ -666,6 +671,11 @@ 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] 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") diff --git a/requirements.txt b/requirements.txt index 5cd99e53b..c62365819 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ platformdirs>=2.1.0 -typing_extensions +typing_extensions>=4.0.0 flexcache>=0.3 flexparser>=0.3