Skip to content

Commit 5aa01e5

Browse files
committed
Reduce false positives for merge warning
1 parent aa2072e commit 5aa01e5

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

xarray/structure/merge.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def unique_variable(
102102
variables: list[Variable],
103103
compat: CompatOptions | CombineKwargDefault = "broadcast_equals",
104104
equals: bool | None = None,
105-
) -> Variable:
105+
) -> tuple[bool, Variable]:
106106
"""Return the unique variable from a list of variables or raise MergeError.
107107
108108
Parameters
@@ -128,7 +128,7 @@ def unique_variable(
128128
out = variables[0]
129129

130130
if len(variables) == 1 or compat == "override":
131-
return out
131+
return equals, out
132132

133133
combine_method = None
134134

@@ -170,7 +170,7 @@ def unique_variable(
170170
for var in variables[1:]:
171171
out = getattr(out, combine_method)(var)
172172

173-
return out
173+
return equals, out
174174

175175

176176
def _assert_compat_valid(compat):
@@ -313,7 +313,7 @@ def merge_collected(
313313
else:
314314
variables = [variable for variable, _ in elements_list]
315315
try:
316-
merged_vars[name] = unique_variable(
316+
equals_this_var, merged_vars[name] = unique_variable(
317317
name, variables, compat, equals.get(name)
318318
)
319319
# This is very likely to result in false positives, but there is no way
@@ -322,6 +322,7 @@ def merge_collected(
322322
isinstance(compat, CombineKwargDefault)
323323
and compat == "no_conflicts"
324324
and len(variables) > 1
325+
and not equals_this_var
325326
):
326327
emit_user_level_warning(
327328
compat.warning_message(

xarray/tests/test_merge.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_merge_datasets(self, use_new_combine_kwarg_defaults):
4747
expected = data[["var1", "var2"]]
4848
assert_identical(actual, expected)
4949

50-
actual = xr.merge([data, data], compat="no_conflicts")
50+
actual = xr.merge([data, data])
5151
assert_identical(actual, data)
5252

5353
def test_merge_dataarray_unnamed(self):
@@ -197,13 +197,9 @@ def test_merge_arrays_attrs_variables(
197197

198198
if expect_exception:
199199
with pytest.raises(MergeError, match="combine_attrs"):
200-
actual = xr.merge(
201-
[data1, data2], compat="no_conflicts", combine_attrs=combine_attrs
202-
)
200+
actual = xr.merge([data1, data2], combine_attrs=combine_attrs)
203201
else:
204-
actual = xr.merge(
205-
[data1, data2], compat="no_conflicts", combine_attrs=combine_attrs
206-
)
202+
actual = xr.merge([data1, data2], combine_attrs=combine_attrs)
207203
expected = xr.Dataset(
208204
{"var1": ("dim1", [], expected_attrs)},
209205
coords={"dim1": ("dim1", [], expected_attrs)},
@@ -323,19 +319,17 @@ def test_merge_no_conflicts_multi_var(self):
323319

324320
def test_merge_no_conflicts_preserve_attrs(self):
325321
data = xr.Dataset({"x": ([], 0, {"foo": "bar"})})
326-
actual = xr.merge(
327-
[data, data], compat="no_conflicts", combine_attrs="no_conflicts"
328-
)
322+
actual = xr.merge([data, data], combine_attrs="no_conflicts")
329323
assert_identical(data, actual)
330324

331325
def test_merge_no_conflicts_broadcast(self):
332326
datasets = [xr.Dataset({"x": ("y", [0])}), xr.Dataset({"x": np.nan})]
333-
actual = xr.merge(datasets, compat="no_conflicts")
327+
actual = xr.merge(datasets)
334328
expected = xr.Dataset({"x": ("y", [0])})
335329
assert_identical(expected, actual)
336330

337331
datasets = [xr.Dataset({"x": ("y", [np.nan])}), xr.Dataset({"x": 0})]
338-
actual = xr.merge(datasets, compat="no_conflicts")
332+
actual = xr.merge(datasets)
339333
assert_identical(expected, actual)
340334

341335

@@ -350,20 +344,20 @@ def test_merge(self):
350344

351345
actual = ds2.merge(ds1)
352346
assert_identical(expected, actual)
353-
with pytest.warns(FutureWarning): # this is a false alarm
354-
actual = data.merge(data)
347+
348+
actual = data.merge(data)
355349
assert_identical(data, actual)
356-
actual = data.reset_coords(drop=True).merge(data, compat="no_conflicts")
350+
actual = data.reset_coords(drop=True).merge(data)
357351
assert_identical(data, actual)
358-
actual = data.merge(data.reset_coords(drop=True), compat="no_conflicts")
352+
actual = data.merge(data.reset_coords(drop=True))
359353
assert_identical(data, actual)
360354

361355
with pytest.raises(ValueError, match="conflicting values for variable"):
362356
ds1.merge(ds2.rename({"var3": "var1"}))
363357
with pytest.raises(ValueError, match=r"should be coordinates or not"):
364-
data.reset_coords().merge(data, compat="no_conflicts")
358+
data.reset_coords().merge(data)
365359
with pytest.raises(ValueError, match=r"should be coordinates or not"):
366-
data.merge(data.reset_coords(), compat="no_conflicts")
360+
data.merge(data.reset_coords())
367361

368362
def test_merge_drop_attrs(self):
369363
data = create_test_data()
@@ -417,6 +411,7 @@ def test_merge_compat(self):
417411
assert ds1.identical(ds1.merge(ds2, compat="override"))
418412

419413
def test_merge_compat_minimal(self) -> None:
414+
"""Test that we drop the conflicting bar coordinate."""
420415
# https://github.com/pydata/xarray/issues/7405
421416
# https://github.com/pydata/xarray/issues/7588
422417
ds1 = xr.Dataset(coords={"foo": [1, 2, 3], "bar": 4})
@@ -426,7 +421,7 @@ def test_merge_compat_minimal(self) -> None:
426421
expected = xr.Dataset(coords={"foo": [1, 2, 3]})
427422
assert_identical(actual, expected)
428423

429-
def test_merge_join(self):
424+
def test_merge_join_outer(self):
430425
ds1 = xr.Dataset({"a": ("x", [1, 2]), "x": [0, 1]})
431426
ds2 = xr.Dataset({"b": ("x", [3, 4]), "x": [1, 2]})
432427
expected = xr.Dataset(
@@ -533,11 +528,7 @@ def test_merge_datasets_false_warning(self):
533528
data = create_test_data(add_attrs=False, use_extension_array=True)
534529

535530
with set_options(use_new_combine_kwarg_defaults=False):
536-
with pytest.warns(
537-
FutureWarning,
538-
match="will change from compat='no_conflicts' to compat='override'",
539-
):
540-
old = xr.merge([data, data])
531+
old = xr.merge([data, data])
541532

542533
with set_options(use_new_combine_kwarg_defaults=True):
543534
new = xr.merge([data, data])
@@ -571,11 +562,7 @@ def test_merge_broadcast_equals(self):
571562
ds2 = xr.Dataset({"x": ("y", [0, 0])})
572563

573564
with set_options(use_new_combine_kwarg_defaults=False):
574-
with pytest.warns(
575-
FutureWarning,
576-
match="will change from compat='no_conflicts' to compat='override'",
577-
):
578-
old = ds1.merge(ds2)
565+
old = ds1.merge(ds2)
579566

580567
with set_options(use_new_combine_kwarg_defaults=True):
581568
new = ds1.merge(ds2)

0 commit comments

Comments
 (0)