-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shallow copy parent and children in DataTree constructor #9297
Shallow copy parent and children in DataTree constructor #9297
Conversation
xarray/core/datatree.py
Outdated
self.children = children | ||
|
||
# shallow copy to avoid modifying arguments in-place (see GH issue #9196) | ||
self.parent = parent.copy() if parent is not None else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove parent
from the constructor instead?
It's a little weird to only be able to set the direct parent (and corresponding sibling), but not more distant relatives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was going to do that in a follow-up PR, but I can do it in this one instead.
The main delay here is just how some of the tests are written - a lot of them rely upon in-place modification to work. e.g. xarray/xarray/tests/test_datatree.py Line 77 in c7cb9f7
This change makes the constructor a somewhat unhelpful way to create trees from scratch. I could rewrite these tests either
|
When you say it's ugly, it's what I was thinking. It seems closer to what you're testing.
|
I think using You could also avoid all the intermediate variables by writing something like: john = DataTree(children={"Mary": DataTree(children={"Sue": DataTree()})}) Incidentally, it would sure be nice if we could eventually support automatic unpacking of nested dictionaries in the john = DataTree({"Mary": {"Sue": {}}}) Here the nature of the node (child DataTree vs DataArray) could be inferred from the argument type. |
Incidentally, I would try to avoid redundant john = DataTree.from_dict(
{
"/": DataTree(),
"/Mary": DataTree(),
"/Mary/Sue": DataTree(),
}
) or even just john = DataTree.from_dict(
{
"/Mary/Sue": DataTree(),
}
) |
I've now re-written the tests to use approach (2), i.e. to just use |
for more information, see https://pre-commit.ci
I will happily take something better. But this was the error I was getting xarray/tests/test_datatree.py:127: error: Argument 1 to "relative_to" of "NamedNode" has incompatible type "DataTree[Any] | DataArray"; expected "NamedNode[Any]" [arg-type]
for more information, see https://pre-commit.ci
But it doesn't fix all the tests There's three tests that I don't fully know what should be tested or if they still make sense.
@TomNicholas I didn't want to push up unfinished work to this PR again. But 7c530f5?diff=split&w=0
Update edited: I think this is a test for test_setparent_unnamed_child_node_fails still have no idea what these are just 3 commits on https://github.com/flamingbear/xarray/tree/datatree_init_dont_modify_inplace |
I think these are all resolved by either:
|
This PR now does a few related things: |
I believe this is ready to merge. It might imply more changes to @flamingbear 's docs PR though. |
Looks great, thanks Tom! |
* main: (29 commits) Release notes for v2024.09.0 (pydata#9480) Fix `DataTree.coords.__setitem__` by adding `DataTreeCoordinates` class (pydata#9451) Rename DataTree's "ds" and "data" to "dataset" (pydata#9476) Update DataTree repr to indicate inheritance (pydata#9470) Bump pypa/gh-action-pypi-publish in the actions group (pydata#9460) Repo checker (pydata#9450) Add days_in_year and decimal_year to dt accessor (pydata#9105) remove parent argument from DataTree.__init__ (pydata#9465) Fix inheritance in DataTree.copy() (pydata#9457) Implement `DataTree.__delitem__` (pydata#9453) Add ASV for datatree.from_dict (pydata#9459) Make the first argument in DataTree.from_dict positional only (pydata#9446) Fix typos across the code, doc and comments (pydata#9443) DataTree should not be "Generic" (pydata#9445) Disallow passing a DataArray as data into the DataTree constructor (pydata#9444) Support additional dtypes in `resample` (pydata#9413) Shallow copy parent and children in DataTree constructor (pydata#9297) Bump minimum versions for dependencies (pydata#9434) Always include at least one category in random test data (pydata#9436) Avoid deep-copy when constructing groupby codes (pydata#9429) ...
* add tests * fix by shallow copying * correct first few tests * replace constructors in tests with DataTree.from_dict * rewrite simple_datatree fixture to use DataTree.from_dict * fix incorrect creation of nested tree in formatting test * Update doctests for from_dict constructor * swap i and h in doctest example for clarity. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix a few mypy errors. * Bonkers way to set type checking I will happily take something better. But this was the error I was getting xarray/tests/test_datatree.py:127: error: Argument 1 to "relative_to" of "NamedNode" has incompatible type "DataTree[Any] | DataArray"; expected "NamedNode[Any]" [arg-type] * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Removes parent keyword from DataTree constructor But it doesn't fix all the tests There's three tests that I don't fully know what should be tested or if they still make sense. * fix test_setparent_unnamed_child_node_fails * fix test_dont_modify_parent_inplace -> bug? * fix test_create_two_children * make .parent read-only, and remove tests which test the parent setter * update error message to reflect fact that .children is Frozen * fix another test * add test that parent setter tells you to set children instead * fix mypy error due to overriding settable property with read-only property * fix test by not trying to set parent via kwarg --------- Co-authored-by: Matt Savoie <matthew.savoie@colorado.edu> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This seems to fix the examples in #9196, but causes other tests to fail, which I think just reveals that we have existing tests that assume this in-place modification is desired. I will change those tests to match the new intended behaviour.
whats-new.rst
api.rst