Checks that assignment of this new parent will not create a cycle.
(self, new_parent: Self | None)
| 111 | self._attach(new_parent, child_name) |
| 112 | |
| 113 | def _check_loop(self, new_parent: Self | None) -> None: |
| 114 | """Checks that assignment of this new parent will not create a cycle.""" |
| 115 | if new_parent is not None: |
| 116 | if new_parent is self: |
| 117 | raise InvalidTreeError( |
| 118 | f"Cannot set parent, as node {self} cannot be a parent of itself." |
| 119 | ) |
| 120 | |
| 121 | if self._is_descendant_of(new_parent): |
| 122 | raise InvalidTreeError( |
| 123 | "Cannot set parent, as intended parent is already a descendant of this node." |
| 124 | ) |
| 125 | |
| 126 | def _is_descendant_of(self, node: Self) -> bool: |
| 127 | return any(n is self for n in node.parents) |
no test coverage detected