From 5e790b8490cef0f0bc0792b2c6aab728e6b43105 Mon Sep 17 00:00:00 2001 From: David Linke Date: Tue, 19 Dec 2023 15:25:18 +0100 Subject: [PATCH] Add check for delta unit to convert --- CHANGES | 3 +-- pint/facets/nonmultiplicative/registry.py | 7 ++++++- pint/testsuite/test_unit.py | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 1b2981430..703e80958 100644 --- a/CHANGES +++ b/CHANGES @@ -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) ----------------- diff --git a/pint/facets/nonmultiplicative/registry.py b/pint/facets/nonmultiplicative/registry.py index 67250ea48..dbc67ac01 100644 --- a/pint/facets/nonmultiplicative/registry.py +++ b/pint/facets/nonmultiplicative/registry.py @@ -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,6 +264,8 @@ 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 @@ -270,6 +273,8 @@ def _convert( # 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) diff --git a/pint/testsuite/test_unit.py b/pint/testsuite/test_unit.py index d0f335357..93931f1b8 100644 --- a/pint/testsuite/test_unit.py +++ b/pint/testsuite/test_unit.py @@ -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)