Skip to content

Commit ce92f94

Browse files
committed
Simplify drop_conflicts implementation
Based on code review, simplified the logic to: - Process attributes in a single loop instead of nested operations - Eliminate intermediate dictionary creation - Make the control flow clearer and more efficient
1 parent be07f64 commit ce92f94

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

xarray/structure/merge.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -633,33 +633,29 @@ def merge_attrs(variable_attrs, combine_attrs, context=None):
633633
elif combine_attrs == "drop_conflicts":
634634
result = {}
635635
dropped_keys = set()
636+
636637
for attrs in variable_attrs:
637-
result.update(
638-
{
639-
key: value
640-
for key, value in attrs.items()
641-
if key not in result and key not in dropped_keys
642-
}
643-
)
644-
# Filter out attributes that have conflicts
645-
filtered_result = {}
646-
for key, value in result.items():
647-
if key not in attrs:
648-
# No conflict, keep the attribute
649-
filtered_result[key] = value
638+
# Process each attribute in the current attrs dict
639+
for key, value in attrs.items():
640+
if key in dropped_keys:
641+
continue # Already marked as conflicted
642+
643+
if key not in result:
644+
# New key, add it
645+
result[key] = value
650646
else:
651-
# Check if values are equivalent
647+
# Existing key, check for conflict
652648
try:
653-
if equivalent(attrs[key], value):
654-
# Values are equivalent, keep the attribute
655-
filtered_result[key] = value
656-
# else: Values differ, drop the attribute (don't add to filtered_result)
649+
if not equivalent(result[key], value):
650+
# Values are different, drop the key
651+
result.pop(key, None)
652+
dropped_keys.add(key)
657653
except ValueError:
658-
# Likely an ambiguous truth value from numpy array comparison
659-
# Treat as non-equivalent and drop the attribute
660-
pass
661-
result = filtered_result
662-
dropped_keys |= {key for key in attrs if key not in result}
654+
# equivalent() failed (likely ambiguous truth value)
655+
# Treat as conflict and drop
656+
result.pop(key, None)
657+
dropped_keys.add(key)
658+
663659
return result
664660
elif combine_attrs == "identical":
665661
result = dict(variable_attrs[0])

0 commit comments

Comments
 (0)