Skip to content

Commit 275f508

Browse files
authored
Clarification on non-Module roots (#2536)
* remove last instances of Unknown parents (and None parents in tests) It's a part of the campaign to get rid of non-module roots * 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 d3df248 commit 275f508

9 files changed

+21
-21
lines changed

astroid/brain/brain_argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def infer_namespace(node, context: InferenceContext | None = None):
2121
"Namespace",
2222
lineno=node.lineno,
2323
col_offset=node.col_offset,
24-
parent=AstroidManager().adhoc_module, # this class is not real
24+
parent=AstroidManager().synthetic_root, # this class is not real
2525
end_lineno=node.end_lineno,
2626
end_col_offset=node.end_col_offset,
2727
)

astroid/brain/brain_builtin_inference.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def _extend_builtins(class_transforms):
165165

166166
def on_bootstrap():
167167
"""Called by astroid_bootstrapping()."""
168-
AstroidManager().cache_module(build_module("__astroid_adhoc"))
168+
AstroidManager().cache_module(build_module("__astroid_synthetic"))
169169

170170
_extend_builtins(
171171
{
@@ -653,7 +653,7 @@ def infer_property(
653653
# node.frame. It's somewhere in the builtins module, but we are special
654654
# casing it for each "property()" call, so we are making up the
655655
# definition on the spot, ad-hoc.
656-
parent=AstroidManager().adhoc_module,
656+
parent=AstroidManager().synthetic_root,
657657
)
658658
prop_func.postinit(
659659
body=[],

astroid/brain/brain_namedtuple_enum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def infer_named_tuple(
194194
"""Specific inference function for namedtuple Call node."""
195195
tuple_base: nodes.Name = _extract_single_node("tuple")
196196
class_node, name, attributes = infer_func_form(
197-
node, tuple_base, parent=AstroidManager().adhoc_module, context=context
197+
node, tuple_base, parent=AstroidManager().synthetic_root, context=context
198198
)
199199

200200
call_site = arguments.CallSite.from_call(node, context=context)
@@ -360,7 +360,7 @@ def value(self):
360360
class_node = infer_func_form(
361361
node,
362362
enum_meta,
363-
parent=AstroidManager().adhoc_module,
363+
parent=AstroidManager().synthetic_root,
364364
context=context,
365365
enum=True,
366366
)[0]

astroid/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ def builtins_module(self) -> nodes.Module:
116116
return self.astroid_cache["builtins"]
117117

118118
@property
119-
def adhoc_module(self) -> nodes.Module:
120-
return self.astroid_cache["__astroid_adhoc"]
119+
def synthetic_root(self) -> nodes.Module:
120+
return self.astroid_cache["__astroid_synthetic"]
121121

122122
@property
123123
def prefer_stubs(self) -> bool:

astroid/nodes/node_ng.py

Lines changed: 5 additions & 4 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,
@@ -43,7 +43,6 @@
4343

4444

4545
if TYPE_CHECKING:
46-
from astroid import nodes
4746
from astroid.nodes import _base_nodes
4847

4948

@@ -332,11 +331,13 @@ def root(self) -> nodes.Module:
332331
:returns: The root node.
333332
"""
334333
if not (parent := self.parent):
335-
return self # type: ignore[return-value] # Only 'Module' does not have a parent node.
334+
assert isinstance(self, nodes.Module)
335+
return self
336336

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

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

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
Arguments,
4545
Const,
4646
NodeNG,
47-
Unknown,
4847
_base_nodes,
4948
const_factory,
5049
node_classes,
@@ -1603,7 +1602,7 @@ def infer_call_result(
16031602
col_offset=0,
16041603
end_lineno=0,
16051604
end_col_offset=0,
1606-
parent=AstroidManager().adhoc_module,
1605+
parent=AstroidManager().synthetic_root,
16071606
)
16081607
new_class.hide = True
16091608
new_class.postinit(
@@ -2037,7 +2036,7 @@ def _infer_type_call(self, caller, context):
20372036
col_offset=0,
20382037
end_lineno=0,
20392038
end_col_offset=0,
2040-
parent=Unknown(),
2039+
parent=caller.parent,
20412040
)
20422041

20432042
# Get the bases of the class.
@@ -2071,7 +2070,6 @@ def _infer_type_call(self, caller, context):
20712070
if isinstance(attr, node_classes.Const) and isinstance(attr.value, str):
20722071
result.locals[attr.value] = [value]
20732072

2074-
result.parent = caller.parent
20752073
return result
20762074

20772075
def infer_call_result(

astroid/objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def __init__(self, call, name=None, lineno=None, col_offset=None, parent=None):
279279
name,
280280
lineno=lineno,
281281
col_offset=col_offset,
282-
parent=AstroidManager().adhoc_module,
282+
parent=AstroidManager().synthetic_root,
283283
end_col_offset=0,
284284
end_lineno=0,
285285
)

astroid/raw_building.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,8 @@ def _astroid_bootstrapping() -> None:
670670
col_offset=0,
671671
end_lineno=0,
672672
end_col_offset=0,
673-
parent=nodes.Unknown(),
673+
parent=astroid_builtin,
674674
)
675-
_UnionTypeType.parent = astroid_builtin
676675
union_type_doc_node = (
677676
nodes.Const(value=types.UnionType.__doc__)
678677
if types.UnionType.__doc__
@@ -707,9 +706,8 @@ def _astroid_bootstrapping() -> None:
707706
col_offset=0,
708707
end_lineno=0,
709708
end_col_offset=0,
710-
parent=nodes.Unknown(),
709+
parent=astroid_builtin,
711710
)
712-
klass.parent = astroid_builtin
713711
doc = _type.__doc__ if isinstance(_type.__doc__, str) else None
714712
klass.postinit(
715713
bases=[],

tests/test_nodes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
ParentMissingError,
3838
StatementMissing,
3939
)
40+
from astroid.manager import AstroidManager
4041
from astroid.nodes.node_classes import (
4142
AssignAttr,
4243
AssignName,
@@ -1960,7 +1961,9 @@ def test_str_repr_no_warnings(node):
19601961
if name == "self":
19611962
continue
19621963

1963-
if "int" in param_type.annotation:
1964+
if name == "parent" and "NodeNG" in param_type.annotation:
1965+
args[name] = AstroidManager().synthetic_root
1966+
elif "int" in param_type.annotation:
19641967
args[name] = random.randint(0, 50)
19651968
elif (
19661969
"NodeNG" in param_type.annotation

0 commit comments

Comments
 (0)