Skip to content

Commit ebae45f

Browse files
committed
assert that the root() is always a Module
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.
1 parent 2f4fff1 commit ebae45f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

astroid/nodes/node_ng.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
overload,
2424
)
2525

26-
from astroid import util
26+
from astroid import nodes, util
2727
from astroid.context import InferenceContext
2828
from astroid.exceptions import (
2929
AstroidError,
@@ -333,11 +333,13 @@ def root(self) -> nodes.Module:
333333
:returns: The root node.
334334
"""
335335
if not (parent := self.parent):
336-
return self # type: ignore[return-value] # Only 'Module' does not have a parent node.
336+
assert isinstance(self, nodes.Module)
337+
return self
337338

338339
while parent.parent:
339340
parent = parent.parent
340-
return parent # type: ignore[return-value] # Only 'Module' does not have a parent node.
341+
assert isinstance(parent, nodes.Module)
342+
return parent
341343

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

0 commit comments

Comments
 (0)