Skip to content
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

Fix inadvertent deep-copying of child data in DataTree #9684

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Deprecations
Bug fixes
~~~~~~~~~

- Fix inadvertent deep-copying of child data in DataTree.
By `Stephan Hoyer <https://github.com/shoyer>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def check_alignment(
) -> None:
if parent_ds is not None:
try:
align(node_ds, parent_ds, join="exact")
align(node_ds, parent_ds, join="exact", copy=False)
except ValueError as e:
node_repr = _indented(_without_header(repr(node_ds)))
parent_repr = _indented(dims_and_coords_repr(parent_ds))
Expand Down
13 changes: 13 additions & 0 deletions xarray/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ def test_data_arg(self) -> None:
with pytest.raises(TypeError):
DataTree(dataset=xr.DataArray(42, name="foo")) # type: ignore[arg-type]

def test_child_data_not_copied(self) -> None:
# regression test for https://github.com/pydata/xarray/issues/9683
class NoDeepCopy:
def __deepcopy__(self, memo):
raise TypeError("class can't be deepcopied")

da = xr.DataArray(NoDeepCopy())
ds = xr.Dataset({"var": da})
dt1 = xr.DataTree(ds)
dt2 = xr.DataTree(ds, children={"child": dt1})
dt3 = xr.DataTree.from_dict({"/": ds, "child": ds})
assert_identical(dt2, dt3)


class TestFamilyTree:
def test_dont_modify_children_inplace(self) -> None:
Expand Down
Loading