From b883fead71757227ead714dcd490e5e2674c5964 Mon Sep 17 00:00:00 2001 From: keewis Date: Wed, 5 May 2021 22:58:10 +0200 Subject: [PATCH] combine_attrs merge errors with compat="minimal" are silenced (#5262) --- xarray/core/merge.py | 12 +++++++----- xarray/tests/test_merge.py | 8 ++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/xarray/core/merge.py b/xarray/core/merge.py index ec3c9b0f065..4d83855a15d 100644 --- a/xarray/core/merge.py +++ b/xarray/core/merge.py @@ -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 @@ -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 = {} diff --git a/xarray/tests/test_merge.py b/xarray/tests/test_merge.py index 680c2a3a679..3bfde2726ea 100644 --- a/xarray/tests/test_merge.py +++ b/xarray/tests/test_merge.py @@ -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})