Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

combine_attrs merge errors with compat="minimal" are silenced #5262

Merged
merged 4 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions xarray/core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,17 @@ def merge_collected(
variables = [variable for variable, _ in elements_list]
try:
merged_vars[name] = unique_variable(name, variables, compat)
merged_vars[name].attrs = merge_attrs(
[var.attrs for var in variables], combine_attrs=combine_attrs
)
except MergeError:
if compat != "minimal":
# we need more than "minimal" compatibility (for which
# we drop conflicting coordinates)
raise

if name in merged_vars:
merged_vars[name].attrs = merge_attrs(
[var.attrs for var in variables], combine_attrs=combine_attrs
)

return merged_vars, merged_indexes


Expand Down Expand Up @@ -515,11 +517,11 @@ def merge_attrs(variable_attrs, combine_attrs):
for attrs in variable_attrs[1:]:
try:
result = compat_dict_union(result, attrs)
except ValueError:
except ValueError as e:
raise MergeError(
"combine_attrs='no_conflicts', but some values are not "
"the same. Merging %s with %s" % (str(result), str(attrs))
)
) from e
return result
elif combine_attrs == "drop_conflicts":
result = {}
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ def test_merge_attrs_drop_conflicts(self):
expected = xr.Dataset(attrs={"a": 0, "d": 0, "e": 0})
assert_identical(actual, expected)

def test_merge_attrs_no_conflicts_compat_minimal(self):
"""make sure compat="minimal" does not silence errors"""
ds1 = xr.Dataset({"a": ("x", [], {"a": 0})})
ds2 = xr.Dataset({"a": ("x", [], {"a": 1})})

with pytest.raises(xr.MergeError, match="combine_attrs"):
xr.merge([ds1, ds2], combine_attrs="no_conflicts", compat="minimal")

def test_merge_dicts_simple(self):
actual = xr.merge([{"foo": 0}, {"bar": "one"}, {"baz": 3.5}])
expected = xr.Dataset({"foo": 0, "bar": "one", "baz": 3.5})
Expand Down