Skip to content

Commit 782cf0b

Browse files
committed
Add check for delta unit to convert
1 parent 83bffe1 commit 782cf0b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pint/facets/nonmultiplicative/registry.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def _is_multiplicative(self, unit_name: str) -> bool:
120120
Raises
121121
------
122122
UndefinedUnitError
123-
If the unit is not in the registyr.
123+
If the unit is not in the registry.
124124
"""
125125
if unit_name in self._units:
126126
return self._units[unit_name].is_multiplicative
@@ -250,6 +250,13 @@ def _convert(
250250
src, dst, extra_msg=f" - In destination units, {ex}"
251251
)
252252

253+
# Check for delta units and make sure src and dst are delta units.
254+
src_delta = any(u.startswith("delta_") for u in src)
255+
dst_delta = any(u.startswith("delta_") for u in dst)
256+
if (src_delta and not dst_delta) or (dst_delta and not src_delta):
257+
raise DimensionalityError(src, dst)
258+
259+
# convert if no offset units are present
253260
if not (src_offset_unit or dst_offset_unit):
254261
return super()._convert(value, src, dst, inplace)
255262

pint/testsuite/test_unit.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,9 @@ class TestConvertWithOffset(QuantityTestCase):
978978
(({"degC": 2}, {"kelvin": 2}), "error"),
979979
(({"degC": 1, "degF": 1}, {"kelvin": 2}), "error"),
980980
(({"degC": 1, "kelvin": 1}, {"kelvin": 2}), "error"),
981+
(({"delta_degC": 1}, {"kelvin": 1}), "error"),
982+
(({"delta_degC": 1}, {"degF": 1}), "error"),
983+
(({"delta_degC": 1}, {"degC": 1}), "error"),
981984
]
982985

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

0 commit comments

Comments
 (0)