Skip to content

Commit 0b780b2

Browse files
committed
Reduce false positives for merge warning
1 parent aa2072e commit 0b780b2

File tree

2 files changed

+27
-46
lines changed

2 files changed

+27
-46
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: 22 additions & 42 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)},
@@ -282,26 +278,20 @@ def test_merge_no_conflicts_single_var(self):
282278
ds1 = xr.Dataset({"a": ("x", [1, 2]), "x": [0, 1]})
283279
ds2 = xr.Dataset({"a": ("x", [2, 3]), "x": [1, 2]})
284280
expected = xr.Dataset({"a": ("x", [1, 2, 3]), "x": [0, 1, 2]})
285-
assert expected.identical(
286-
xr.merge([ds1, ds2], compat="no_conflicts", join="outer")
287-
)
288-
assert expected.identical(
289-
xr.merge([ds2, ds1], compat="no_conflicts", join="outer")
290-
)
291-
assert ds1.identical(xr.merge([ds1, ds2], compat="no_conflicts", join="left"))
292-
assert ds2.identical(xr.merge([ds1, ds2], compat="no_conflicts", join="right"))
281+
assert expected.identical(xr.merge([ds1, ds2], join="outer"))
282+
assert expected.identical(xr.merge([ds2, ds1], join="outer"))
283+
assert ds1.identical(xr.merge([ds1, ds2], join="left"))
284+
assert ds2.identical(xr.merge([ds1, ds2], join="right"))
293285
expected = xr.Dataset({"a": ("x", [2]), "x": [1]})
294-
assert expected.identical(
295-
xr.merge([ds1, ds2], compat="no_conflicts", join="inner")
296-
)
286+
assert expected.identical(xr.merge([ds1, ds2], join="inner"))
297287

298288
with pytest.raises(xr.MergeError):
299289
ds3 = xr.Dataset({"a": ("x", [99, 3]), "x": [1, 2]})
300-
xr.merge([ds1, ds3], compat="no_conflicts", join="outer")
290+
xr.merge([ds1, ds3], join="outer")
301291

302292
with pytest.raises(xr.MergeError):
303293
ds3 = xr.Dataset({"a": ("y", [2, 3]), "y": [1, 2]})
304-
xr.merge([ds1, ds3], compat="no_conflicts", join="outer")
294+
xr.merge([ds1, ds3], join="outer")
305295

306296
def test_merge_no_conflicts_multi_var(self):
307297
data = create_test_data(add_attrs=False)
@@ -323,19 +313,17 @@ def test_merge_no_conflicts_multi_var(self):
323313

324314
def test_merge_no_conflicts_preserve_attrs(self):
325315
data = xr.Dataset({"x": ([], 0, {"foo": "bar"})})
326-
actual = xr.merge(
327-
[data, data], compat="no_conflicts", combine_attrs="no_conflicts"
328-
)
316+
actual = xr.merge([data, data], combine_attrs="no_conflicts")
329317
assert_identical(data, actual)
330318

331319
def test_merge_no_conflicts_broadcast(self):
332320
datasets = [xr.Dataset({"x": ("y", [0])}), xr.Dataset({"x": np.nan})]
333-
actual = xr.merge(datasets, compat="no_conflicts")
321+
actual = xr.merge(datasets)
334322
expected = xr.Dataset({"x": ("y", [0])})
335323
assert_identical(expected, actual)
336324

337325
datasets = [xr.Dataset({"x": ("y", [np.nan])}), xr.Dataset({"x": 0})]
338-
actual = xr.merge(datasets, compat="no_conflicts")
326+
actual = xr.merge(datasets)
339327
assert_identical(expected, actual)
340328

341329

@@ -350,20 +338,19 @@ def test_merge(self):
350338

351339
actual = ds2.merge(ds1)
352340
assert_identical(expected, actual)
353-
with pytest.warns(FutureWarning): # this is a false alarm
354-
actual = data.merge(data)
341+
actual = data.merge(data)
355342
assert_identical(data, actual)
356-
actual = data.reset_coords(drop=True).merge(data, compat="no_conflicts")
343+
actual = data.reset_coords(drop=True).merge(data)
357344
assert_identical(data, actual)
358-
actual = data.merge(data.reset_coords(drop=True), compat="no_conflicts")
345+
actual = data.merge(data.reset_coords(drop=True))
359346
assert_identical(data, actual)
360347

361348
with pytest.raises(ValueError, match="conflicting values for variable"):
362349
ds1.merge(ds2.rename({"var3": "var1"}))
363350
with pytest.raises(ValueError, match=r"should be coordinates or not"):
364-
data.reset_coords().merge(data, compat="no_conflicts")
351+
data.reset_coords().merge(data)
365352
with pytest.raises(ValueError, match=r"should be coordinates or not"):
366-
data.merge(data.reset_coords(), compat="no_conflicts")
353+
data.merge(data.reset_coords())
367354

368355
def test_merge_drop_attrs(self):
369356
data = create_test_data()
@@ -417,6 +404,7 @@ def test_merge_compat(self):
417404
assert ds1.identical(ds1.merge(ds2, compat="override"))
418405

419406
def test_merge_compat_minimal(self) -> None:
407+
"""Test that we drop the conflicting bar coordinate."""
420408
# https://github.com/pydata/xarray/issues/7405
421409
# https://github.com/pydata/xarray/issues/7588
422410
ds1 = xr.Dataset(coords={"foo": [1, 2, 3], "bar": 4})
@@ -426,7 +414,7 @@ def test_merge_compat_minimal(self) -> None:
426414
expected = xr.Dataset(coords={"foo": [1, 2, 3]})
427415
assert_identical(actual, expected)
428416

429-
def test_merge_join(self):
417+
def test_merge_join_outer(self):
430418
ds1 = xr.Dataset({"a": ("x", [1, 2]), "x": [0, 1]})
431419
ds2 = xr.Dataset({"b": ("x", [3, 4]), "x": [1, 2]})
432420
expected = xr.Dataset(
@@ -533,11 +521,7 @@ def test_merge_datasets_false_warning(self):
533521
data = create_test_data(add_attrs=False, use_extension_array=True)
534522

535523
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])
524+
old = xr.merge([data, data])
541525

542526
with set_options(use_new_combine_kwarg_defaults=True):
543527
new = xr.merge([data, data])
@@ -571,11 +555,7 @@ def test_merge_broadcast_equals(self):
571555
ds2 = xr.Dataset({"x": ("y", [0, 0])})
572556

573557
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)
558+
old = ds1.merge(ds2)
579559

580560
with set_options(use_new_combine_kwarg_defaults=True):
581561
new = ds1.merge(ds2)

0 commit comments

Comments
 (0)