File tree 2 files changed +15
-5
lines changed
2 files changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -232,15 +232,17 @@ def merge_collected(
232
232
variables = [variable for variable , _ in elements_list ]
233
233
try :
234
234
merged_vars [name ] = unique_variable (name , variables , compat )
235
- merged_vars [name ].attrs = merge_attrs (
236
- [var .attrs for var in variables ], combine_attrs = combine_attrs
237
- )
238
235
except MergeError :
239
236
if compat != "minimal" :
240
237
# we need more than "minimal" compatibility (for which
241
238
# we drop conflicting coordinates)
242
239
raise
243
240
241
+ if name in merged_vars :
242
+ merged_vars [name ].attrs = merge_attrs (
243
+ [var .attrs for var in variables ], combine_attrs = combine_attrs
244
+ )
245
+
244
246
return merged_vars , merged_indexes
245
247
246
248
@@ -515,11 +517,11 @@ def merge_attrs(variable_attrs, combine_attrs):
515
517
for attrs in variable_attrs [1 :]:
516
518
try :
517
519
result = compat_dict_union (result , attrs )
518
- except ValueError :
520
+ except ValueError as e :
519
521
raise MergeError (
520
522
"combine_attrs='no_conflicts', but some values are not "
521
523
"the same. Merging %s with %s" % (str (result ), str (attrs ))
522
- )
524
+ ) from e
523
525
return result
524
526
elif combine_attrs == "drop_conflicts" :
525
527
result = {}
Original file line number Diff line number Diff line change @@ -202,6 +202,14 @@ def test_merge_attrs_drop_conflicts(self):
202
202
expected = xr .Dataset (attrs = {"a" : 0 , "d" : 0 , "e" : 0 })
203
203
assert_identical (actual , expected )
204
204
205
+ def test_merge_attrs_no_conflicts_compat_minimal (self ):
206
+ """make sure compat="minimal" does not silence errors"""
207
+ ds1 = xr .Dataset ({"a" : ("x" , [], {"a" : 0 })})
208
+ ds2 = xr .Dataset ({"a" : ("x" , [], {"a" : 1 })})
209
+
210
+ with pytest .raises (xr .MergeError , match = "combine_attrs" ):
211
+ xr .merge ([ds1 , ds2 ], combine_attrs = "no_conflicts" , compat = "minimal" )
212
+
205
213
def test_merge_dicts_simple (self ):
206
214
actual = xr .merge ([{"foo" : 0 }, {"bar" : "one" }, {"baz" : 3.5 }])
207
215
expected = xr .Dataset ({"foo" : 0 , "bar" : "one" , "baz" : 3.5 })
You can’t perform that action at this time.
0 commit comments