Skip to content

Commit

Permalink
add error for prefixed non multi units (#1998)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage authored Jul 28, 2024
1 parent 5f2a76a commit 0faac07
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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)

Expand Down
12 changes: 11 additions & 1 deletion pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -667,6 +672,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]
Expand Down
5 changes: 5 additions & 0 deletions pint/testsuite/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 0faac07

Please sign in to comment.