Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify on non-Module roots #2536

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -43,7 +43,7 @@


if TYPE_CHECKING:
from astroid import nodes

Check warning on line 46 in astroid/nodes/node_ng.py

View workflow job for this annotation

GitHub Actions / Checks

W0404

Reimport 'nodes' (imported line 24)
from astroid.nodes import _base_nodes


Expand Down Expand Up @@ -332,11 +332,13 @@
: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
3 changes: 1 addition & 2 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
from astroid.interpreter.dunder_lookup import lookup
from astroid.interpreter.objectmodel import ClassModel, FunctionModel, ModuleModel
from astroid.manager import AstroidManager
from astroid.nodes import (
Arguments,
Const,
NodeNG,
Unknown,
_base_nodes,
const_factory,
node_classes,
)

Check warning on line 51 in astroid/nodes/scoped_nodes/scoped_nodes.py

View workflow job for this annotation

GitHub Actions / Checks

W0611

Unused Unknown imported from astroid.nodes
from astroid.nodes.scoped_nodes.mixin import ComprehensionScope, LocalsDictNodeNG
from astroid.nodes.scoped_nodes.utils import builtin_lookup
from astroid.nodes.utils import Position
Expand Down Expand Up @@ -2037,7 +2037,7 @@
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 +2071,6 @@
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
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().adhoc_module
elif "int" in param_type.annotation:
args[name] = random.randint(0, 50)
elif (
"NodeNG" in param_type.annotation
Expand Down
Loading