@@ -274,15 +274,21 @@ def copy(self, *, inherit: bool = True, deep: bool = False) -> Self:
274
274
"""
275
275
return self ._copy_subtree (inherit = inherit , deep = deep )
276
276
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 :
278
280
"""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 )
280
282
for name , child in self .children .items ():
281
283
# 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
+ )
283
287
return new_tree
284
288
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 :
286
292
"""Copy just one node of a tree"""
287
293
new_empty_node = type (self )()
288
294
return new_empty_node
@@ -291,8 +297,7 @@ def __copy__(self) -> Self:
291
297
return self ._copy_subtree (inherit = True , deep = False )
292
298
293
299
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 )
296
301
297
302
def _iter_parents (self : Tree ) -> Iterator [Tree ]:
298
303
"""Iterate up the tree, starting from the current node's parent."""
@@ -693,9 +698,11 @@ def _post_attach(self, parent: Self, name: str) -> None:
693
698
_validate_name (name ) # is this check redundant?
694
699
self ._name = name
695
700
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 :
697
704
"""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 )
699
706
new_node ._name = self .name
700
707
return new_node
701
708
0 commit comments