Skip to content

Commit

Permalink
DEPR: expire deprecation for mixed temperature units in ufuncs
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Apr 8, 2023
1 parent 79ef518 commit 7e11201
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
21 changes: 11 additions & 10 deletions unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,11 @@ def _multiply_units(unit1, unit2):
return ret.as_coeff_unit()


TEMPERATURE_WARNING = """
Ambiguous operation with heterogeneous temperature units.
In the future, such operations will generate UnitOperationError.
Use delta_degC or delta_degF to avoid the ambiguity.
"""


@lru_cache(maxsize=128, typed=False)
def _preserve_units(unit1, unit2=None):
if unit2 is None or unit1.dimensions is not temperature:
return 1, unit1
if unit1.base_offset == 0.0 and unit2.base_offset != 0.0:
if str(unit1.expr) in ["K", "R"]:
warnings.warn(TEMPERATURE_WARNING, FutureWarning, stacklevel=3)
return 1, unit1
return 1, unit2
return 1, unit1

Expand Down Expand Up @@ -1833,6 +1823,17 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
else:
u1 = 1.0
unit_operator = self._ufunc_registry[ufunc]

if (
unit_operator is _preserve_units
and u0.dimensions is temperature
and u1 is not None
and u1.base_offset != 0.0
and u0.base_offset == 0.0
and str(u0.expr) in ["K", "R"]
):
raise UnitOperationError(ufunc, u0, u1)

if unit_operator in (_preserve_units, _comparison_unit, _arctan2_unit):
# check "is" equality first for speed
if u0 is not u1 and u0 != u1:
Expand Down
10 changes: 2 additions & 8 deletions unyt/tests/test_unyt_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2471,11 +2471,8 @@ def test_delta_degC():
t1 = 10 * degC
t2 = 1 * K
assert t1 + t2 == 11 * degC
# In the current implementation 1*K + 10*degC = 11*K
# In the future, this will generate UnitOperationError
with pytest.warns(FutureWarning):
with pytest.raises(UnitOperationError):
t2 + t1
assert t2 + t1 == 11 * K
t3 = 1 * delta_degC
assert t1 + t3 == 11 * degC
assert t3 + t1 == 11 * degC
Expand All @@ -2487,11 +2484,8 @@ def test_delta_degF():
t1 = 10 * degF
t2 = 1 * R
assert t1 + t2 == 11 * degF
# In the current implementation 1*R + 10*degF = 11*R
# In the future, this will generate UnitOperationError
with pytest.warns(FutureWarning):
with pytest.raises(UnitOperationError):
t2 + t1
assert t2 + t1 == 11 * R
t3 = 1 * delta_degF
assert t1 + t3 == 11 * degF
assert t3 + t1 == 11 * degF
Expand Down

0 comments on commit 7e11201

Please sign in to comment.