|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import warnings |
| 4 | + |
3 | 5 | import numpy as np
|
| 6 | +import pandas as pd |
4 | 7 | import pytest
|
5 | 8 |
|
6 | 9 | import xarray as xr
|
@@ -235,6 +238,267 @@ def test_merge_attrs_drop_conflicts(self):
|
235 | 238 | expected = xr.Dataset(attrs={"a": 0, "d": 0, "e": 0})
|
236 | 239 | assert_identical(actual, expected)
|
237 | 240 |
|
| 241 | + def test_merge_attrs_drop_conflicts_numpy_arrays(self): |
| 242 | + """Test drop_conflicts with numpy arrays.""" |
| 243 | + # Test with numpy arrays (which return arrays from ==) |
| 244 | + arr1 = np.array([1, 2, 3]) |
| 245 | + arr2 = np.array([1, 2, 3]) |
| 246 | + arr3 = np.array([4, 5, 6]) |
| 247 | + |
| 248 | + ds1 = xr.Dataset(attrs={"arr": arr1, "scalar": 1}) |
| 249 | + ds2 = xr.Dataset(attrs={"arr": arr2, "scalar": 1}) # Same array values |
| 250 | + ds3 = xr.Dataset(attrs={"arr": arr3, "other": 2}) # Different array values |
| 251 | + |
| 252 | + # Arrays are considered equivalent if they have the same values |
| 253 | + actual = xr.merge([ds1, ds2], combine_attrs="drop_conflicts") |
| 254 | + assert "arr" in actual.attrs # Should keep the array since they're equivalent |
| 255 | + assert actual.attrs["scalar"] == 1 |
| 256 | + |
| 257 | + # Different arrays cause the attribute to be dropped |
| 258 | + actual = xr.merge([ds1, ds3], combine_attrs="drop_conflicts") |
| 259 | + assert "arr" not in actual.attrs # Should drop due to conflict |
| 260 | + assert "other" in actual.attrs |
| 261 | + |
| 262 | + def test_merge_attrs_drop_conflicts_custom_eq_returns_array(self): |
| 263 | + """Test drop_conflicts with custom objects that return arrays from __eq__.""" |
| 264 | + |
| 265 | + # Test with custom objects that return non-bool from __eq__ |
| 266 | + class CustomEq: |
| 267 | + """Object whose __eq__ returns a non-bool value.""" |
| 268 | + |
| 269 | + def __init__(self, value): |
| 270 | + self.value = value |
| 271 | + |
| 272 | + def __eq__(self, other): |
| 273 | + if not isinstance(other, CustomEq): |
| 274 | + return False |
| 275 | + # Return a numpy array (truthy if all elements are non-zero) |
| 276 | + return np.array([self.value == other.value]) |
| 277 | + |
| 278 | + def __repr__(self): |
| 279 | + return f"CustomEq({self.value})" |
| 280 | + |
| 281 | + obj1 = CustomEq(42) |
| 282 | + obj2 = CustomEq(42) # Same value |
| 283 | + obj3 = CustomEq(99) # Different value |
| 284 | + |
| 285 | + ds4 = xr.Dataset(attrs={"custom": obj1, "x": 1}) |
| 286 | + ds5 = xr.Dataset(attrs={"custom": obj2, "x": 1}) |
| 287 | + ds6 = xr.Dataset(attrs={"custom": obj3, "y": 2}) |
| 288 | + |
| 289 | + # Suppress DeprecationWarning from numpy < 2.0 about ambiguous truth values |
| 290 | + # when our custom __eq__ returns arrays that are evaluated in boolean context |
| 291 | + with warnings.catch_warnings(): |
| 292 | + warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 293 | + |
| 294 | + # Objects returning arrays are dropped (non-boolean return) |
| 295 | + actual = xr.merge([ds4, ds5], combine_attrs="drop_conflicts") |
| 296 | + assert "custom" not in actual.attrs # Dropped - returns array, not bool |
| 297 | + assert actual.attrs["x"] == 1 |
| 298 | + |
| 299 | + # Different values also dropped (returns array, not bool) |
| 300 | + actual = xr.merge([ds4, ds6], combine_attrs="drop_conflicts") |
| 301 | + assert "custom" not in actual.attrs # Dropped - returns non-boolean |
| 302 | + assert actual.attrs["x"] == 1 |
| 303 | + assert actual.attrs["y"] == 2 |
| 304 | + |
| 305 | + def test_merge_attrs_drop_conflicts_ambiguous_array_returns(self): |
| 306 | + """Test drop_conflicts with objects returning ambiguous arrays from __eq__.""" |
| 307 | + |
| 308 | + # Test edge case: object whose __eq__ returns empty array (ambiguous truth value) |
| 309 | + class EmptyArrayEq: |
| 310 | + def __eq__(self, other): |
| 311 | + if not isinstance(other, EmptyArrayEq): |
| 312 | + return False |
| 313 | + return np.array([]) # Empty array has ambiguous truth value |
| 314 | + |
| 315 | + def __repr__(self): |
| 316 | + return "EmptyArrayEq()" |
| 317 | + |
| 318 | + empty_obj1 = EmptyArrayEq() |
| 319 | + empty_obj2 = EmptyArrayEq() |
| 320 | + |
| 321 | + ds7 = xr.Dataset(attrs={"empty": empty_obj1}) |
| 322 | + ds8 = xr.Dataset(attrs={"empty": empty_obj2}) |
| 323 | + |
| 324 | + # With new behavior: ambiguous truth values are treated as non-equivalent |
| 325 | + # So the attribute is dropped instead of raising an error |
| 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 |
| 330 | + |
| 331 | + # Test with object that returns multi-element array (also ambiguous) |
| 332 | + class MultiArrayEq: |
| 333 | + def __eq__(self, other): |
| 334 | + if not isinstance(other, MultiArrayEq): |
| 335 | + return False |
| 336 | + return np.array([True, False]) # Multi-element array is ambiguous |
| 337 | + |
| 338 | + def __repr__(self): |
| 339 | + return "MultiArrayEq()" |
| 340 | + |
| 341 | + multi_obj1 = MultiArrayEq() |
| 342 | + multi_obj2 = MultiArrayEq() |
| 343 | + |
| 344 | + ds9 = xr.Dataset(attrs={"multi": multi_obj1}) |
| 345 | + ds10 = xr.Dataset(attrs={"multi": multi_obj2}) |
| 346 | + |
| 347 | + # With new behavior: ambiguous arrays are treated as non-equivalent |
| 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 |
| 352 | + |
| 353 | + def test_merge_attrs_drop_conflicts_all_true_array(self): |
| 354 | + """Test drop_conflicts with all-True multi-element array from __eq__.""" |
| 355 | + |
| 356 | + # Test with all-True multi-element array (unambiguous truthy) |
| 357 | + class AllTrueArrayEq: |
| 358 | + def __eq__(self, other): |
| 359 | + if not isinstance(other, AllTrueArrayEq): |
| 360 | + return False |
| 361 | + return np.array([True, True, True]) # All True, but still multi-element |
| 362 | + |
| 363 | + def __repr__(self): |
| 364 | + return "AllTrueArrayEq()" |
| 365 | + |
| 366 | + alltrue1 = AllTrueArrayEq() |
| 367 | + alltrue2 = AllTrueArrayEq() |
| 368 | + |
| 369 | + ds11 = xr.Dataset(attrs={"alltrue": alltrue1}) |
| 370 | + ds12 = xr.Dataset(attrs={"alltrue": alltrue2}) |
| 371 | + |
| 372 | + # Multi-element arrays are ambiguous even if all True |
| 373 | + actual = xr.merge([ds11, ds12], combine_attrs="drop_conflicts") |
| 374 | + assert "alltrue" not in actual.attrs # Dropped due to ambiguous comparison |
| 375 | + |
| 376 | + def test_merge_attrs_drop_conflicts_nested_arrays(self): |
| 377 | + """Test drop_conflicts with NumPy object arrays containing nested arrays.""" |
| 378 | + # Test 1: NumPy object arrays with nested arrays |
| 379 | + # These can have complex comparison behavior |
| 380 | + x = np.array([None], dtype=object) |
| 381 | + x[0] = np.arange(3) |
| 382 | + y = np.array([None], dtype=object) |
| 383 | + y[0] = np.arange(10, 13) |
| 384 | + |
| 385 | + ds1 = xr.Dataset(attrs={"nested_array": x, "common": 1}) |
| 386 | + ds2 = xr.Dataset(attrs={"nested_array": y, "common": 1}) |
| 387 | + |
| 388 | + # Different nested arrays should cause attribute to be dropped |
| 389 | + actual = xr.merge([ds1, ds2], combine_attrs="drop_conflicts") |
| 390 | + assert ( |
| 391 | + "nested_array" not in actual.attrs |
| 392 | + ) # Dropped due to different nested arrays |
| 393 | + assert actual.attrs["common"] == 1 |
| 394 | + |
| 395 | + # Test with identical nested arrays |
| 396 | + # Note: Even identical nested arrays will be dropped because comparison |
| 397 | + # raises ValueError due to ambiguous truth value |
| 398 | + z = np.array([None], dtype=object) |
| 399 | + z[0] = np.arange(3) # Same as x |
| 400 | + ds3 = xr.Dataset(attrs={"nested_array": z, "other": 2}) |
| 401 | + |
| 402 | + actual = xr.merge([ds1, ds3], combine_attrs="drop_conflicts") |
| 403 | + assert ( |
| 404 | + "nested_array" not in actual.attrs |
| 405 | + ) # Dropped due to ValueError in comparison |
| 406 | + assert actual.attrs["other"] == 2 |
| 407 | + |
| 408 | + def test_merge_attrs_drop_conflicts_dataset_attrs(self): |
| 409 | + """Test drop_conflicts with xarray.Dataset objects as attributes.""" |
| 410 | + # xarray.Dataset objects as attributes (raises TypeError in equivalent) |
| 411 | + attr_ds1 = xr.Dataset({"foo": 1}) |
| 412 | + attr_ds2 = xr.Dataset({"bar": 1}) # Different dataset |
| 413 | + attr_ds3 = xr.Dataset({"foo": 1}) # Same as attr_ds1 |
| 414 | + |
| 415 | + ds4 = xr.Dataset(attrs={"dataset_attr": attr_ds1, "scalar": 42}) |
| 416 | + ds5 = xr.Dataset(attrs={"dataset_attr": attr_ds2, "scalar": 42}) |
| 417 | + ds6 = xr.Dataset(attrs={"dataset_attr": attr_ds3, "other": 99}) |
| 418 | + |
| 419 | + # Different datasets raise TypeError and should be dropped |
| 420 | + actual = xr.merge([ds4, ds5], combine_attrs="drop_conflicts") |
| 421 | + assert "dataset_attr" not in actual.attrs # Dropped due to TypeError |
| 422 | + assert actual.attrs["scalar"] == 42 |
| 423 | + |
| 424 | + # Identical datasets are also dropped (comparison returns Dataset, not bool) |
| 425 | + actual = xr.merge([ds4, ds6], combine_attrs="drop_conflicts") |
| 426 | + assert "dataset_attr" not in actual.attrs # Dropped - returns Dataset, not bool |
| 427 | + assert actual.attrs["other"] == 99 |
| 428 | + |
| 429 | + def test_merge_attrs_drop_conflicts_pandas_series(self): |
| 430 | + """Test drop_conflicts with Pandas Series as attributes.""" |
| 431 | + # Pandas Series (raises ValueError due to ambiguous truth value) |
| 432 | + series1 = pd.Series([1, 2]) |
| 433 | + series2 = pd.Series([3, 4]) # Different values |
| 434 | + series3 = pd.Series([1, 2]) # Same as series1 |
| 435 | + |
| 436 | + ds7 = xr.Dataset(attrs={"series": series1, "value": "a"}) |
| 437 | + ds8 = xr.Dataset(attrs={"series": series2, "value": "a"}) |
| 438 | + ds9 = xr.Dataset(attrs={"series": series3, "value": "a"}) |
| 439 | + |
| 440 | + # Suppress potential warnings from pandas comparisons |
| 441 | + with warnings.catch_warnings(): |
| 442 | + warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 443 | + warnings.filterwarnings("ignore", category=FutureWarning) |
| 444 | + |
| 445 | + # Different series raise ValueError and get dropped |
| 446 | + actual = xr.merge([ds7, ds8], combine_attrs="drop_conflicts") |
| 447 | + assert "series" not in actual.attrs # Dropped due to ValueError |
| 448 | + assert actual.attrs["value"] == "a" |
| 449 | + |
| 450 | + # Even identical series raise ValueError in equivalent() and get dropped |
| 451 | + # because Series comparison returns another Series with ambiguous truth value |
| 452 | + actual = xr.merge([ds7, ds9], combine_attrs="drop_conflicts") |
| 453 | + assert "series" not in actual.attrs # Dropped due to ValueError |
| 454 | + assert actual.attrs["value"] == "a" |
| 455 | + |
| 456 | + def test_merge_attrs_drop_conflicts_eq_returns_string(self): |
| 457 | + """Test objects whose __eq__ returns strings are dropped.""" |
| 458 | + |
| 459 | + # Case 1: Objects whose __eq__ returns non-boolean strings |
| 460 | + class ReturnsString: |
| 461 | + def __init__(self, value): |
| 462 | + self.value = value |
| 463 | + |
| 464 | + def __eq__(self, other): |
| 465 | + # Always returns a string (non-boolean) |
| 466 | + return "comparison result" |
| 467 | + |
| 468 | + obj1 = ReturnsString("A") |
| 469 | + obj2 = ReturnsString("B") # Different object |
| 470 | + |
| 471 | + ds1 = xr.Dataset(attrs={"obj": obj1}) |
| 472 | + ds2 = xr.Dataset(attrs={"obj": obj2}) |
| 473 | + |
| 474 | + actual = xr.merge([ds1, ds2], combine_attrs="drop_conflicts") |
| 475 | + |
| 476 | + # Strict behavior: drops attribute because __eq__ returns non-boolean |
| 477 | + assert "obj" not in actual.attrs |
| 478 | + |
| 479 | + def test_merge_attrs_drop_conflicts_eq_returns_number(self): |
| 480 | + """Test objects whose __eq__ returns numbers are dropped.""" |
| 481 | + |
| 482 | + # Case 2: Objects whose __eq__ returns numbers |
| 483 | + class ReturnsZero: |
| 484 | + def __init__(self, value): |
| 485 | + self.value = value |
| 486 | + |
| 487 | + def __eq__(self, other): |
| 488 | + # Always returns 0 (non-boolean) |
| 489 | + return 0 |
| 490 | + |
| 491 | + obj3 = ReturnsZero("same") |
| 492 | + obj4 = ReturnsZero("same") # Different object, same value |
| 493 | + |
| 494 | + ds3 = xr.Dataset(attrs={"zero": obj3}) |
| 495 | + ds4 = xr.Dataset(attrs={"zero": obj4}) |
| 496 | + |
| 497 | + actual = xr.merge([ds3, ds4], combine_attrs="drop_conflicts") |
| 498 | + |
| 499 | + # Strict behavior: drops attribute because __eq__ returns non-boolean |
| 500 | + assert "zero" not in actual.attrs |
| 501 | + |
238 | 502 | def test_merge_attrs_no_conflicts_compat_minimal(self):
|
239 | 503 | """make sure compat="minimal" does not silence errors"""
|
240 | 504 | ds1 = xr.Dataset({"a": ("x", [], {"a": 0})})
|
|
0 commit comments