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 check for delta unit to convert #1905

Merged
merged 2 commits into from
May 12, 2024
Merged
Changes from 1 commit
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
Next Next commit
Add check for delta unit to convert
dalito committed Dec 19, 2023
commit 5e790b8490cef0f0bc0792b2c6aab728e6b43105
3 changes: 1 addition & 2 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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)
-----------------
7 changes: 6 additions & 1 deletion pint/facets/nonmultiplicative/registry.py
Original file line number Diff line number Diff line change
@@ -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
@@ -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)

@@ -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)
2 changes: 2 additions & 0 deletions pint/testsuite/test_unit.py
Original file line number Diff line number Diff line change
@@ -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)