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

Detailed Error Message for get_dimensionality() #1874

Merged
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 @@ -12,6 +12,8 @@ Pint Changelog
(PR #1819)
- Add numpy.linalg.norm implementation.
(PR #1251)
- Improved error message in `get_dimensionality()` when non existent units are passed.
(PR #1874, Issue #1716)

0.22 (2023-05-25)
-----------------
Expand Down
7 changes: 6 additions & 1 deletion pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,12 @@ def _get_dimensionality_recurse(
for key in ref:
exp2 = exp * ref[key]
if _is_dim(key):
reg = self._dimensions[key]
try:
reg = self._dimensions[key]
except KeyError:
raise ValueError(
f"{key} is not defined as dimension in the pint UnitRegistry"
)
if isinstance(reg, DerivedDimensionDefinition):
self._get_dimensionality_recurse(reg.reference, exp2, accumulator)
else:
Expand Down
10 changes: 10 additions & 0 deletions pint/testsuite/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,13 @@ def test_pickle_definition_syntax_error(self, subtests):

with pytest.raises(PintError):
raise ex

def test_dimensionality_error_message(self):
ureg = UnitRegistry(system="SI")
with pytest.raises(ValueError) as error:
ureg.get_dimensionality("[bilbo]")

assert (
str(error.value)
== "[bilbo] is not defined as dimension in the pint UnitRegistry"
)