Skip to content

Commit

Permalink
Fix inadvertent deep-copying of child data in DataTree (pydata#9684)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoyer authored Oct 26, 2024
1 parent 5be821b commit dbb98b4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
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

0 comments on commit dbb98b4

Please sign in to comment.