Skip to content

Commit

Permalink
Add check for delta unit to convert
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito committed Dec 19, 2023
1 parent 83bffe1 commit 5e790b8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 1 addition & 2 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ Pint Changelog
0.24 (unreleased)
-----------------

- Nothing changed yet.

- Fix detection of invalid conversion between offset and delta units. (PR #1905)

0.23 (2023-12-08)
-----------------
Expand Down
7 changes: 6 additions & 1 deletion pint/facets/nonmultiplicative/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _is_multiplicative(self, unit_name: str) -> bool:
Raises
------
UndefinedUnitError
If the unit is not in the registyr.
If the unit is not in the registry.
"""
if unit_name in self._units:
return self._units[unit_name].is_multiplicative
Expand Down Expand Up @@ -250,6 +250,7 @@ def _convert(
src, dst, extra_msg=f" - In destination units, {ex}"
)

# convert if no offset units are present
if not (src_offset_unit or dst_offset_unit):
return super()._convert(value, src, dst, inplace)

Expand All @@ -263,13 +264,17 @@ def _convert(

# clean src from offset units by converting to reference
if src_offset_unit:
if any(u.startswith("delta_") for u in dst):
raise DimensionalityError(src, dst)
value = self._units[src_offset_unit].converter.to_reference(value, inplace)
src = src.remove([src_offset_unit])
# Add reference unit for multiplicative section
src = self._add_ref_of_log_or_offset_unit(src_offset_unit, src)

# clean dst units from offset units
if dst_offset_unit:
if any(u.startswith("delta_") for u in src):
raise DimensionalityError(src, dst)
dst = dst.remove([dst_offset_unit])
# Add reference unit for multiplicative section
dst = self._add_ref_of_log_or_offset_unit(dst_offset_unit, dst)
Expand Down
2 changes: 2 additions & 0 deletions pint/testsuite/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,8 @@ class TestConvertWithOffset(QuantityTestCase):
(({"degC": 2}, {"kelvin": 2}), "error"),
(({"degC": 1, "degF": 1}, {"kelvin": 2}), "error"),
(({"degC": 1, "kelvin": 1}, {"kelvin": 2}), "error"),
(({"delta_degC": 1}, {"degF": 1}), "error"),
(({"delta_degC": 1}, {"degC": 1}), "error"),
]

@pytest.mark.parametrize(("input_tuple", "expected"), convert_with_offset)
Expand Down

0 comments on commit 5e790b8

Please sign in to comment.