Skip to content

Commit

Permalink
combine_attrs merge errors with compat="minimal" are silenced (pydata…
Browse files Browse the repository at this point in the history
  • Loading branch information
keewis authored May 5, 2021
1 parent bd4650c commit b883fea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
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

0 comments on commit b883fea

Please sign in to comment.