Find the first common ancestor of two nodes in the same tree. Raise ValueError if they are not in the same tree.
(self, other: Self)
| 761 | ) |
| 762 | |
| 763 | def find_common_ancestor(self, other: Self) -> Self: |
| 764 | """ |
| 765 | Find the first common ancestor of two nodes in the same tree. |
| 766 | |
| 767 | Raise ValueError if they are not in the same tree. |
| 768 | """ |
| 769 | if self is other: |
| 770 | return self |
| 771 | |
| 772 | other_paths = [op.path for op in other.parents] |
| 773 | for parent in (self, *self.parents): |
| 774 | if parent.path in other_paths: |
| 775 | return parent |
| 776 | |
| 777 | raise NotFoundInTreeError( |
| 778 | "Cannot find common ancestor because nodes do not lie within the same tree" |
| 779 | ) |
| 780 | |
| 781 | def _path_to_ancestor(self, ancestor: Self) -> NodePath: |
| 782 | """Return the relative path from this node to the given ancestor node""" |
no test coverage detected