Repository navigation
fix(datatree): reject variable-named attribute assignment (#9928) - #11934
Open
aniruddhaadak80 wants to merge 3 commits into
Open
aniruddhaadak80 wants to merge 3 commits into
aniruddhaadak80 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Contributor
Author
|
CI follow-up: the Mypy jobs flagged |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes #9928.
DataTreehas a__dict__(for dynamically defined attributes, see GH9068) in addition to__slots__. Variables and child nodes are normally found throughAttrAccessMixin.__getattr__, which walks_attr_sources. Assigningtree.variable = valuetherefore creates an ordinary instance attribute that shadows that lookup, while the stored dataset is unchanged:The value written by attribute assignment is also the one used when the tree is exported, so the two views silently disagree.
The fix adds a
DataTree.__setattr__that checks whether the name resolves to a data variable, coordinate variable, or immediate child node. If it does, anAttributeErroris raised pointing to__setitem__assignment. This matches the longstandingDatasetbehaviour ("Use setitem style assignment ... instead of assigning variables") and removes the silent divergence. Normal attributes (dt.name, user attributes, private slots) anddt.dataset = ...are unaffected.A
doc/whats-new.rstentry was added under the unreleased "Bug Fixes" section.Checklist
whats-new.rstapi.rst(no new public functions/methods)Tests
New test in
xarray/tests/test_datatree.py::TestAccessasserts that assigning todt.a(whereais a data variable) raisesAttributeError, leaves__dict__untouched, and leavesdt.aanddt["a"]identical.Actual results in a worktree-local environment (Python 3.12, NumPy 2.5.3, pandas 3.0.6, Windows). The local
sitecustomize.pyshim onPYTHONPATHthat gives the tagless editable dask checkout a parseable__version__was reused; nothing from it is in the diff.TestIndexing::test_sel_isel_error_has_node_infofails both with and without the change (pre-existing in this environment).TestTreeFromDict::test_roundtrip_to_dictalso failed on the unmodified baseline but passed with the fix; it is flaky and unrelated to attribute assignment. No new failures are introduced.Limitations
attrsstill use ordinary attribute semantics; extending the check there would be a larger API change.AI Disclosure
Tools: OpenCode (AI coding agent)
An AI coding agent traced the shadowing to
DataTree's__dict__/__getattr__interaction, drafted the__setattr__guard and regression test, and ran the verification above. I reviewed the diff and each claimed result.