Skip to content

Fix merge override attrs reference issue - #11506

Open
nocdoggo wants to merge 5 commits into
pydata:mainfrom
nocdoggo:fix-merge-attrs-override
Open

Fix merge override attrs reference issue#11506
nocdoggo wants to merge 5 commits into
pydata:mainfrom
nocdoggo:fix-merge-attrs-override

Conversation

@nocdoggo

@nocdoggo nocdoggo commented Aug 11, 2026

Copy link
Copy Markdown

Description\n\nWhen using combine_attrs='override' in xr.merge(), the function was returning a reference to the original attrs dictionary instead of a copy. This meant that modifying the merged dataset's attrs would also modify the source dataset's attrs.\n\nThis change modifies the merge_attrs function to return dict(variable_attrs[0]) instead of variable_attrs[0], creating a shallow copy of the dictionary and preventing unintended side effects.\n\nFixes #4629\n\n### Checklist\n\n\n\n- [x] Closes #4629\n- [x] Tests added\n- [ ] User visible changes (including notable bug fixes) are documented in \n- [ ] New functions/methods are listed in \n\n### AI Disclosure\n\n<!--- Please review our AI & contribution guidelines: https://docs.xarray.dev/en/stable/contribute/ai-policy.html. Remove this section if your PR does not contain AI-generated content. --->\n\n- [x] This PR contains AI-generated content.\n - [x] I have tested any AI-generated content in my PR.\n - [x] I take responsibility for any AI-generated content in my PR.\n <!--- If you used AI to generate code, please specify the tool used and the prompt below. --->\n Tools: OpenHands

SWE-bench and others added 3 commits September 10, 2025 15:04
…'override' in xr.merge(), the function was returning\na reference to the original attrs dictionary instead of a copy. This meant\nthat modifying the merged dataset's attrs would also modify the source\ndataset's attrs.\n\nThis change modifies the merge_attrs function to return dict(variable_attrs[0])\ninstead of variable_attrs[0], creating a shallow copy of the dictionary\nand preventing unintended side effects.\n\nFixes pydata#4629
Copilot AI lite review requested due to automatic review settings August 11, 2026 12:41
@welcome

welcome Bot commented Aug 11, 2026

Copy link
Copy Markdown

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

@nocdoggo

Copy link
Copy Markdown
Author

This pull request was created by an AI agent (OpenHands) on behalf of the user to fix the issue where merge(combine_attrs='override') was not copying attrs but instead referencing the first object's attrs.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an xr.merge(..., combine_attrs="override") side effect where the merged object’s .attrs could share the same underlying dictionary as the source object, so mutating the result could mutate the input.

Changes:

  • In merge_attrs, return a shallow copy (dict(variable_attrs[0])) for combine_attrs="override" instead of returning the original mapping object.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread xarray/core/merge.py
Comment on lines 503 to +504
elif combine_attrs == "override":
return variable_attrs[0]
return dict(variable_attrs[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants