diff --git a/src/textual/widgets/_tree.py b/src/textual/widgets/_tree.py index df355ccdf..27bb2487f 100644 --- a/src/textual/widgets/_tree.py +++ b/src/textual/widgets/_tree.py @@ -352,6 +352,18 @@ class TreeNode(Generic[TreeDataType]): class RemoveRootError(Exception): """Exception raised when trying to remove a tree's root node.""" + def _remove(self) -> None: + """Remove the current node and all its children. + + Note: + This is the internal support method for `remove`. + """ + for child in reversed(self._children): + child._remove() + assert self._parent is not None + del self._parent._children[self._parent._children.index(self)] + del self._tree._tree_nodes[self.id] + def remove(self) -> None: """Remove this node from the tree. @@ -360,8 +372,7 @@ class TreeNode(Generic[TreeDataType]): """ if self.is_root: raise self.RemoveRootError("Attempt to remove the root node of a Tree.") - assert self._parent is not None - del self._parent._children[self._parent._children.index(self)] + self._remove() self._tree._invalidate()