Skip to content

Commit 38fc4aa

Browse files
committed
Optimize root by not calling root() itself
1 parent 494fdbf commit 38fc4aa

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

astroid/nodes/node_ng.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,12 @@ def root(self) -> nodes.Module:
332332
333333
:returns: The root node.
334334
"""
335-
if self.parent:
336-
return self.parent.root()
337-
return self # type: ignore[return-value] # Only 'Module' does not have a parent node.
335+
if not (parent := self.parent):
336+
return self # type: ignore[return-value] # Only 'Module' does not have a parent node.
337+
338+
while parent.parent:
339+
parent = parent.parent
340+
return parent # type: ignore[return-value] # Only 'Module' does not have a parent node.
338341

339342
def child_sequence(self, child):
340343
"""Search for the sequence that contains this child.

0 commit comments

Comments
 (0)