| 94 | ) |
| 95 | |
| 96 | def _set_parent( |
| 97 | self, new_parent: Self | None, child_name: str | None = None |
| 98 | ) -> None: |
| 99 | # TODO is it possible to refactor in a way that removes this private method? |
| 100 | |
| 101 | if new_parent is not None and not isinstance(new_parent, TreeNode): |
| 102 | raise TypeError( |
| 103 | "Parent nodes must be of type DataTree or None, " |
| 104 | f"not type {type(new_parent)}" |
| 105 | ) |
| 106 | |
| 107 | old_parent = self._parent |
| 108 | if new_parent is not old_parent: |
| 109 | self._check_loop(new_parent) |
| 110 | self._detach(old_parent) |
| 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.""" |