Skip to content

Commit b1cba12

Browse files
committed
Move DeprecationWarning suppression from library to test
The warning only occurs in our specific test case with custom objects that return numpy arrays from __eq__. This is a very edge case scenario, so it's more appropriate to suppress the warning in the test rather than in the library code.
1 parent 116a8f0 commit b1cba12

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

xarray/core/utils.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,7 @@ def equivalent(first: T, second: T) -> bool:
250250
if isinstance(first, list) or isinstance(second, list):
251251
return list_equiv(first, second) # type: ignore[arg-type]
252252

253-
# Suppress DeprecationWarning about ambiguous truth values from numpy < 2.0
254-
# In numpy 2.0+, this will raise ValueError directly, which we handle in callers
255-
# Can remove this suppression when minimum numpy version >= 2.0
256-
import warnings
257-
258-
with warnings.catch_warnings():
259-
warnings.filterwarnings("ignore", category=DeprecationWarning)
260-
return (first == second) or (pd.isnull(first) and pd.isnull(second)) # type: ignore[call-overload]
253+
return (first == second) or (pd.isnull(first) and pd.isnull(second)) # type: ignore[call-overload]
261254

262255

263256
def list_equiv(first: Sequence[T], second: Sequence[T]) -> bool:

xarray/tests/test_merge.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def test_merge_attrs_drop_conflicts_non_bool_eq(self):
244244
than raising an error.
245245
"""
246246
import numpy as np
247+
import warnings
247248

248249
# Test with numpy arrays (which return arrays from ==)
249250
arr1 = np.array([1, 2, 3])
@@ -288,16 +289,21 @@ def __repr__(self):
288289
ds5 = xr.Dataset(attrs={"custom": obj2, "x": 1})
289290
ds6 = xr.Dataset(attrs={"custom": obj3, "y": 2})
290291

291-
# Objects with same value (returning truthy array [True])
292-
actual = xr.merge([ds4, ds5], combine_attrs="drop_conflicts")
293-
assert "custom" in actual.attrs
294-
assert actual.attrs["x"] == 1
292+
# Suppress DeprecationWarning from numpy < 2.0 about ambiguous truth values
293+
# when our custom __eq__ returns arrays that are evaluated in boolean context
294+
with warnings.catch_warnings():
295+
warnings.filterwarnings("ignore", category=DeprecationWarning)
295296

296-
# Objects with different values (returning falsy array [False])
297-
actual = xr.merge([ds4, ds6], combine_attrs="drop_conflicts")
298-
assert "custom" not in actual.attrs # Dropped due to conflict
299-
assert actual.attrs["x"] == 1
300-
assert actual.attrs["y"] == 2
297+
# Objects with same value (returning truthy array [True])
298+
actual = xr.merge([ds4, ds5], combine_attrs="drop_conflicts")
299+
assert "custom" in actual.attrs
300+
assert actual.attrs["x"] == 1
301+
302+
# Objects with different values (returning falsy array [False])
303+
actual = xr.merge([ds4, ds6], combine_attrs="drop_conflicts")
304+
assert "custom" not in actual.attrs # Dropped due to conflict
305+
assert actual.attrs["x"] == 1
306+
assert actual.attrs["y"] == 2
301307

302308
# Test edge case: object whose __eq__ returns empty array (ambiguous truth value)
303309
class EmptyArrayEq:
@@ -317,8 +323,10 @@ def __repr__(self):
317323

318324
# With new behavior: ambiguous truth values are treated as non-equivalent
319325
# So the attribute is dropped instead of raising an error
320-
actual = xr.merge([ds7, ds8], combine_attrs="drop_conflicts")
321-
assert "empty" not in actual.attrs # Dropped due to ambiguous comparison
326+
with warnings.catch_warnings():
327+
warnings.filterwarnings("ignore", category=DeprecationWarning)
328+
actual = xr.merge([ds7, ds8], combine_attrs="drop_conflicts")
329+
assert "empty" not in actual.attrs # Dropped due to ambiguous comparison
322330

323331
# Test with object that returns multi-element array (also ambiguous)
324332
class MultiArrayEq:
@@ -337,8 +345,10 @@ def __repr__(self):
337345
ds10 = xr.Dataset(attrs={"multi": multi_obj2})
338346

339347
# With new behavior: ambiguous arrays are treated as non-equivalent
340-
actual = xr.merge([ds9, ds10], combine_attrs="drop_conflicts")
341-
assert "multi" not in actual.attrs # Dropped due to ambiguous comparison
348+
with warnings.catch_warnings():
349+
warnings.filterwarnings("ignore", category=DeprecationWarning)
350+
actual = xr.merge([ds9, ds10], combine_attrs="drop_conflicts")
351+
assert "multi" not in actual.attrs # Dropped due to ambiguous comparison
342352

343353
# Test with all-True multi-element array (unambiguous truthy)
344354
class AllTrueArrayEq:

0 commit comments

Comments
 (0)