Skip to content

Commit

Permalink
Detailed Error Message for get_dimensionality() (#1874)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristannew authored May 12, 2024
1 parent c0501ff commit 24dd237
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 @@ -49,6 +49,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 @@ -736,7 +736,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 @@ -144,3 +144,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"
)

0 comments on commit 24dd237

Please sign in to comment.