Skip to content

Commit

Permalink
Add missing memo argument to DataTree.__deepcopy__ (pydata#9631)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoyer authored Oct 15, 2024
1 parent 7486f4e commit aafc278
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
8 changes: 5 additions & 3 deletions xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,12 +826,14 @@ def _replace_node(

self.children = children

def _copy_node(self, inherit: bool, deep: bool = False) -> Self:
def _copy_node(
self, inherit: bool, deep: bool = False, memo: dict[int, Any] | None = None
) -> Self:
"""Copy just one node of a tree."""
new_node = super()._copy_node(inherit=inherit, deep=deep)
new_node = super()._copy_node(inherit=inherit, deep=deep, memo=memo)
data = self._to_dataset_view(rebuild_dims=False, inherit=inherit)
if deep:
data = data.copy(deep=True)
data = data._copy(deep=True, memo=memo)
new_node._set_node_data(data)
return new_node

Expand Down
23 changes: 15 additions & 8 deletions xarray/core/treenode.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,21 @@ def copy(self, *, inherit: bool = True, deep: bool = False) -> Self:
"""
return self._copy_subtree(inherit=inherit, deep=deep)

def _copy_subtree(self, inherit: bool, deep: bool = False) -> Self:
def _copy_subtree(
self, inherit: bool, deep: bool = False, memo: dict[int, Any] | None = None
) -> Self:
"""Copy entire subtree recursively."""
new_tree = self._copy_node(inherit=inherit, deep=deep)
new_tree = self._copy_node(inherit=inherit, deep=deep, memo=memo)
for name, child in self.children.items():
# TODO use `.children[name] = ...` once #9477 is implemented
new_tree._set(name, child._copy_subtree(inherit=False, deep=deep))
new_tree._set(
name, child._copy_subtree(inherit=False, deep=deep, memo=memo)
)
return new_tree

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

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

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

def _copy_node(self, inherit: bool, deep: bool = False) -> Self:
def _copy_node(
self, inherit: bool, deep: bool = False, memo: dict[int, Any] | None = None
) -> Self:
"""Copy just one node of a tree"""
new_node = super()._copy_node(inherit=inherit, deep=deep)
new_node = super()._copy_node(inherit=inherit, deep=deep, memo=memo)
new_node._name = self.name
return new_node

Expand Down

0 comments on commit aafc278

Please sign in to comment.