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
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
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
Loading