Skip to content

Commit aafc278

Browse files
authoredOct 15, 2024··
Add missing memo argument to DataTree.__deepcopy__ (#9631)
as suggested by @headtr1ck in #9628 (comment)
1 parent 7486f4e commit aafc278

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed
 

‎xarray/core/datatree.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -826,12 +826,14 @@ def _replace_node(
826826

827827
self.children = children
828828

829-
def _copy_node(self, inherit: bool, deep: bool = False) -> Self:
829+
def _copy_node(
830+
self, inherit: bool, deep: bool = False, memo: dict[int, Any] | None = None
831+
) -> Self:
830832
"""Copy just one node of a tree."""
831-
new_node = super()._copy_node(inherit=inherit, deep=deep)
833+
new_node = super()._copy_node(inherit=inherit, deep=deep, memo=memo)
832834
data = self._to_dataset_view(rebuild_dims=False, inherit=inherit)
833835
if deep:
834-
data = data.copy(deep=True)
836+
data = data._copy(deep=True, memo=memo)
835837
new_node._set_node_data(data)
836838
return new_node
837839

‎xarray/core/treenode.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,21 @@ def copy(self, *, inherit: bool = True, deep: bool = False) -> Self:
274274
"""
275275
return self._copy_subtree(inherit=inherit, deep=deep)
276276

277-
def _copy_subtree(self, inherit: bool, deep: bool = False) -> Self:
277+
def _copy_subtree(
278+
self, inherit: bool, deep: bool = False, memo: dict[int, Any] | None = None
279+
) -> Self:
278280
"""Copy entire subtree recursively."""
279-
new_tree = self._copy_node(inherit=inherit, deep=deep)
281+
new_tree = self._copy_node(inherit=inherit, deep=deep, memo=memo)
280282
for name, child in self.children.items():
281283
# TODO use `.children[name] = ...` once #9477 is implemented
282-
new_tree._set(name, child._copy_subtree(inherit=False, deep=deep))
284+
new_tree._set(
285+
name, child._copy_subtree(inherit=False, deep=deep, memo=memo)
286+
)
283287
return new_tree
284288

285-
def _copy_node(self, inherit: bool, deep: bool = False) -> Self:
289+
def _copy_node(
290+
self, inherit: bool, deep: bool = False, memo: dict[int, Any] | None = None
291+
) -> Self:
286292
"""Copy just one node of a tree"""
287293
new_empty_node = type(self)()
288294
return new_empty_node
@@ -291,8 +297,7 @@ def __copy__(self) -> Self:
291297
return self._copy_subtree(inherit=True, deep=False)
292298

293299
def __deepcopy__(self, memo: dict[int, Any] | None = None) -> Self:
294-
del memo # nodes cannot be reused in a DataTree
295-
return self._copy_subtree(inherit=True, deep=True)
300+
return self._copy_subtree(inherit=True, deep=True, memo=memo)
296301

297302
def _iter_parents(self: Tree) -> Iterator[Tree]:
298303
"""Iterate up the tree, starting from the current node's parent."""
@@ -693,9 +698,11 @@ def _post_attach(self, parent: Self, name: str) -> None:
693698
_validate_name(name) # is this check redundant?
694699
self._name = name
695700

696-
def _copy_node(self, inherit: bool, deep: bool = False) -> Self:
701+
def _copy_node(
702+
self, inherit: bool, deep: bool = False, memo: dict[int, Any] | None = None
703+
) -> Self:
697704
"""Copy just one node of a tree"""
698-
new_node = super()._copy_node(inherit=inherit, deep=deep)
705+
new_node = super()._copy_node(inherit=inherit, deep=deep, memo=memo)
699706
new_node._name = self.name
700707
return new_node
701708

0 commit comments

Comments
 (0)
Please sign in to comment.