Fix merge override attrs reference issue - #11506
Conversation
…'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
|
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
|
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. |
There was a problem hiding this comment.
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])) forcombine_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.
| elif combine_attrs == "override": | ||
| return variable_attrs[0] | ||
| return dict(variable_attrs[0]) |
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