Skip to content

Commit

Permalink
Clarification on non-Module roots (pylint-dev#2536)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
temyurchenko authored Oct 7, 2024
1 parent d3df248 commit 275f508
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion astroid/brain/brain_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def infer_namespace(node, context: InferenceContext | None = None):
"Namespace",
lineno=node.lineno,
col_offset=node.col_offset,
parent=AstroidManager().adhoc_module, # this class is not real
parent=AstroidManager().synthetic_root, # this class is not real
end_lineno=node.end_lineno,
end_col_offset=node.end_col_offset,
)
Expand Down
4 changes: 2 additions & 2 deletions astroid/brain/brain_builtin_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _extend_builtins(class_transforms):

def on_bootstrap():
"""Called by astroid_bootstrapping()."""
AstroidManager().cache_module(build_module("__astroid_adhoc"))
AstroidManager().cache_module(build_module("__astroid_synthetic"))

_extend_builtins(
{
Expand Down Expand Up @@ -653,7 +653,7 @@ def infer_property(
# node.frame. It's somewhere in the builtins module, but we are special
# casing it for each "property()" call, so we are making up the
# definition on the spot, ad-hoc.
parent=AstroidManager().adhoc_module,
parent=AstroidManager().synthetic_root,
)
prop_func.postinit(
body=[],
Expand Down
4 changes: 2 additions & 2 deletions astroid/brain/brain_namedtuple_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def infer_named_tuple(
"""Specific inference function for namedtuple Call node."""
tuple_base: nodes.Name = _extract_single_node("tuple")
class_node, name, attributes = infer_func_form(
node, tuple_base, parent=AstroidManager().adhoc_module, context=context
node, tuple_base, parent=AstroidManager().synthetic_root, context=context
)

call_site = arguments.CallSite.from_call(node, context=context)
Expand Down Expand Up @@ -360,7 +360,7 @@ def value(self):
class_node = infer_func_form(
node,
enum_meta,
parent=AstroidManager().adhoc_module,
parent=AstroidManager().synthetic_root,
context=context,
enum=True,
)[0]
Expand Down
4 changes: 2 additions & 2 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def builtins_module(self) -> nodes.Module:
return self.astroid_cache["builtins"]

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

@property
def prefer_stubs(self) -> bool:
Expand Down
9 changes: 5 additions & 4 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 All @@ -43,7 +43,6 @@


if TYPE_CHECKING:
from astroid import nodes
from astroid.nodes import _base_nodes


Expand Down Expand Up @@ -332,11 +331,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
6 changes: 2 additions & 4 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
Arguments,
Const,
NodeNG,
Unknown,
_base_nodes,
const_factory,
node_classes,
Expand Down Expand Up @@ -1603,7 +1602,7 @@ def infer_call_result(
col_offset=0,
end_lineno=0,
end_col_offset=0,
parent=AstroidManager().adhoc_module,
parent=AstroidManager().synthetic_root,
)
new_class.hide = True
new_class.postinit(
Expand Down Expand Up @@ -2037,7 +2036,7 @@ def _infer_type_call(self, caller, context):
col_offset=0,
end_lineno=0,
end_col_offset=0,
parent=Unknown(),
parent=caller.parent,
)

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

result.parent = caller.parent
return result

def infer_call_result(
Expand Down
2 changes: 1 addition & 1 deletion astroid/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def __init__(self, call, name=None, lineno=None, col_offset=None, parent=None):
name,
lineno=lineno,
col_offset=col_offset,
parent=AstroidManager().adhoc_module,
parent=AstroidManager().synthetic_root,
end_col_offset=0,
end_lineno=0,
)
Expand Down
6 changes: 2 additions & 4 deletions astroid/raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,8 @@ def _astroid_bootstrapping() -> None:
col_offset=0,
end_lineno=0,
end_col_offset=0,
parent=nodes.Unknown(),
parent=astroid_builtin,
)
_UnionTypeType.parent = astroid_builtin
union_type_doc_node = (
nodes.Const(value=types.UnionType.__doc__)
if types.UnionType.__doc__
Expand Down Expand Up @@ -707,9 +706,8 @@ def _astroid_bootstrapping() -> None:
col_offset=0,
end_lineno=0,
end_col_offset=0,
parent=nodes.Unknown(),
parent=astroid_builtin,
)
klass.parent = astroid_builtin
doc = _type.__doc__ if isinstance(_type.__doc__, str) else None
klass.postinit(
bases=[],
Expand Down
5 changes: 4 additions & 1 deletion tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
ParentMissingError,
StatementMissing,
)
from astroid.manager import AstroidManager
from astroid.nodes.node_classes import (
AssignAttr,
AssignName,
Expand Down Expand Up @@ -1960,7 +1961,9 @@ def test_str_repr_no_warnings(node):
if name == "self":
continue

if "int" in param_type.annotation:
if name == "parent" and "NodeNG" in param_type.annotation:
args[name] = AstroidManager().synthetic_root
elif "int" in param_type.annotation:
args[name] = random.randint(0, 50)
elif (
"NodeNG" in param_type.annotation
Expand Down

0 comments on commit 275f508

Please sign in to comment.