Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error for prefixed non multi units #1998

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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 @@ -664,6 +669,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")
Loading