Skip to content

Commit f38ffc1

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 0f3310b commit f38ffc1

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
@@ -21,7 +21,7 @@
2121
overload,
2222
)
2323

24-
from astroid import util
24+
from astroid import nodes, util
2525
from astroid.context import InferenceContext
2626
from astroid.exceptions import (
2727
AstroidError,
@@ -332,11 +332,13 @@ def root(self) -> nodes.Module:
332332
:returns: The root node.
333333
"""
334334
if not (parent := self.parent):
335-
return self # type: ignore[return-value] # Only 'Module' does not have a parent node.
335+
assert isinstance(self, nodes.Module)
336+
return self
336337

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

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

0 commit comments

Comments
 (0)