Skip to content

Commit

Permalink
Add check for delta unit to convert (#1905)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito authored May 12, 2024
1 parent 5206fac commit 9a93a68
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Pint Changelog
0.24 (unreleased)
-----------------

- Fix detection of invalid conversion between offset and delta units. (PR #1905)
- Added dBW, decibel Watts, which is used in RF high power applications
- NumPy 2.0 support
(PR #1985, #1971)
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 @@ -119,7 +119,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 @@ -254,6 +254,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 @@ -267,13 +268,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 @@ -989,6 +989,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 9a93a68

Please sign in to comment.