Skip to content

Commit

Permalink
assert that the root() is always a Module
Browse files Browse the repository at this point in the history
The nodes are often created in an ad-hoc way, and their parent is not
   always set. We can't control for that invariant fully in the
   constructor, since the parent is sometimes set outside of the
   constructor. The previous commits did their best to clean up such
   situations, but let's add an assert just in case.
  • Loading branch information
temyurchenko committed Sep 13, 2024
1 parent 3236eb8 commit 00a70e9
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions astroid/nodes/node_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
overload,
)

from astroid import util
from astroid import nodes, util
from astroid.context import InferenceContext
from astroid.exceptions import (
AstroidError,
Expand Down Expand Up @@ -332,11 +332,13 @@ def root(self) -> nodes.Module:
:returns: The root node.
"""
if not (parent := self.parent):
return self # type: ignore[return-value] # Only 'Module' does not have a parent node.
assert isinstance(self, nodes.Module)
return self

while parent.parent:
parent = parent.parent
return parent # type: ignore[return-value] # Only 'Module' does not have a parent node.
assert isinstance(parent, nodes.Module)
return parent

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

0 comments on commit 00a70e9

Please sign in to comment.