Skip to content

Commit dbb98b4

Browse files
authored
Fix inadvertent deep-copying of child data in DataTree (#9684)
1 parent 5be821b commit dbb98b4

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Deprecations
3434
Bug fixes
3535
~~~~~~~~~
3636

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

3840
Documentation
3941
~~~~~~~~~~~~~

xarray/core/datatree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def check_alignment(
146146
) -> None:
147147
if parent_ds is not None:
148148
try:
149-
align(node_ds, parent_ds, join="exact")
149+
align(node_ds, parent_ds, join="exact", copy=False)
150150
except ValueError as e:
151151
node_repr = _indented(_without_header(repr(node_ds)))
152152
parent_repr = _indented(dims_and_coords_repr(parent_ds))

xarray/tests/test_datatree.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ def test_data_arg(self) -> None:
6969
with pytest.raises(TypeError):
7070
DataTree(dataset=xr.DataArray(42, name="foo")) # type: ignore[arg-type]
7171

72+
def test_child_data_not_copied(self) -> None:
73+
# regression test for https://github.com/pydata/xarray/issues/9683
74+
class NoDeepCopy:
75+
def __deepcopy__(self, memo):
76+
raise TypeError("class can't be deepcopied")
77+
78+
da = xr.DataArray(NoDeepCopy())
79+
ds = xr.Dataset({"var": da})
80+
dt1 = xr.DataTree(ds)
81+
dt2 = xr.DataTree(ds, children={"child": dt1})
82+
dt3 = xr.DataTree.from_dict({"/": ds, "child": ds})
83+
assert_identical(dt2, dt3)
84+
7285

7386
class TestFamilyTree:
7487
def test_dont_modify_children_inplace(self) -> None:

0 commit comments

Comments
 (0)