Skip to content

Commit b87933f

Browse files
committed
Move DeprecationWarning suppression to equivalent function
Rather than suppressing the warning in merge_attrs, handle it at the source in equivalent() where the 'or' operation happens. This is cleaner and includes a clear comment about when the suppression can be removed (when minimum numpy version >= 2.0). The warning only occurs in numpy < 2.0; numpy 2.0+ raises ValueError directly, which we already handle properly in merge_attrs.
1 parent 7adaa40 commit b87933f

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

xarray/core/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,14 @@ def equivalent(first: T, second: T) -> bool:
249249
return duck_array_ops.array_equiv(first, second)
250250
if isinstance(first, list) or isinstance(second, list):
251251
return list_equiv(first, second) # type: ignore[arg-type]
252-
return (first == second) or (pd.isnull(first) and pd.isnull(second)) # type: ignore[call-overload]
252+
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+
with warnings.catch_warnings():
258+
warnings.filterwarnings("ignore", category=DeprecationWarning)
259+
return (first == second) or (pd.isnull(first) and pd.isnull(second)) # type: ignore[call-overload]
253260

254261

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

xarray/structure/merge.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -650,18 +650,10 @@ def merge_attrs(variable_attrs, combine_attrs, context=None):
650650
else:
651651
# Check if values are equivalent
652652
try:
653-
import warnings
654-
655-
# Suppress DeprecationWarning about ambiguous truth values
656-
# since we handle the resulting ValueError appropriately
657-
with warnings.catch_warnings():
658-
warnings.filterwarnings(
659-
"ignore", category=DeprecationWarning
660-
)
661-
if equivalent(attrs[key], value):
662-
# Values are equivalent, keep the attribute
663-
filtered_result[key] = value
664-
# else: Values differ, drop the attribute (don't add to filtered_result)
653+
if equivalent(attrs[key], value):
654+
# Values are equivalent, keep the attribute
655+
filtered_result[key] = value
656+
# else: Values differ, drop the attribute (don't add to filtered_result)
665657
except ValueError:
666658
# Likely an ambiguous truth value from numpy array comparison
667659
# Treat as non-equivalent and drop the attribute

0 commit comments

Comments
 (0)