@@ -273,6 +273,14 @@ def parents(self: Tree) -> Tuple[Tree, ...]:
273
273
@property
274
274
def ancestors (self : Tree ) -> Tuple [Tree , ...]:
275
275
"""All parent nodes and their parent nodes, starting with the most distant."""
276
+
277
+ from warnings import warn
278
+
279
+ warn (
280
+ "`ancestors` has been deprecated, and in the future will raise an error."
281
+ "Please use `parents`. Example: `tuple(reversed(node.parents))`" ,
282
+ DeprecationWarning ,
283
+ )
276
284
return tuple (reversed (self .parents ))
277
285
278
286
@property
@@ -369,7 +377,7 @@ def level(self: Tree) -> int:
369
377
depth
370
378
width
371
379
"""
372
- return len (self .ancestors )
380
+ return len (self .parents )
373
381
374
382
@property
375
383
def depth (self : Tree ) -> int :
@@ -609,7 +617,7 @@ def path(self) -> str:
609
617
if self .is_root :
610
618
return "/"
611
619
else :
612
- root , * ancestors = self .ancestors
620
+ root , * ancestors = tuple ( reversed ( self .parents ))
613
621
# don't include name of root because (a) root might not have a name & (b) we want path relative to root.
614
622
names = [* (node .name for node in ancestors ), self .name ]
615
623
return "/" + "/" .join (names )
@@ -660,7 +668,7 @@ def _path_to_ancestor(self, ancestor: NamedNode) -> NodePath:
660
668
raise NotFoundInTreeError (
661
669
"Cannot find relative path to ancestor because nodes do not lie within the same tree"
662
670
)
663
- if ancestor .path not in list (a .path for a in (* self . ancestors , self )):
671
+ if ancestor .path not in list (a .path for a in (self , * self . parents )):
664
672
raise NotFoundInTreeError (
665
673
"Cannot find relative path to ancestor because given node is not an ancestor of this node"
666
674
)
0 commit comments