Skip to content

Commit 24f65cc

Browse files
committed
ensure that all nodes have a Module as their eventual root
that involved several changes - creating an "adhoc" module, specifically for nodes that are not based in the real syntactic tree, but created "ad-hoc". See "__astroid_adhoc" and 'AstroidManager().adhoc_module'. - eliminating all "Unknown" nodes as parents. They are not 'Module's. Most of the time, it is sufficient to either specify the actual parent in the constructor or the adhoc module from above. - fixing construction of in-place properties (`bar = property(getter)`). They just create a nameless object, not the one with the name of the getter. Thus, the name was changed to "<property>". Furthermore, the definition of that property is not attached to any scope, as it's again nameless. - using "tuple" ClassDef for a base of 'namedtuple' instead of a Name. We're already doing it for "enum"s, and I don't know how to ensure that the Name actually refers to the actual tuple, and not something shadowing it in the scope. Removed the test that asserted that the base is a Name and not a ClassDef. If it is actually useful, it should be checked and reworked comprehensively across all nodes (cf. Enum). - appending a node to the body of the frame when it is also the parent. If it's not a parent, then the node should belong to the "body" of the parent if it existed. An example is a definition within an "if", where the parent is the If node, but the frame is the whole module. See FunctionDef.__init__. - fixing inference of the "special" attributes of Instances. Before these changes, the FunctionDef attributes wouldn't get wrapped into a BoundMethod. This was facilitated by extracting the logic of inferring attributes into 'FunctionDef._infer_attrs' and reusing it in BaseInstance. This issue wasn't visible before, because the special attributes were simply not found due not being attached to the parent (the instance). Which in turn was caused by not providing the actual parent in the constructor. - enforcing a non-None parent in various builders in raw_building.py - fixing tests to accomodate for changes attach classes to their parent; fix bugs uncovered by this 1. some '__doc__' fields of standard library symbols (e.g. WrapperDescriptorType.__doc__) don't return a string, they return a 'getset_descriptor'. Thus, an attempt to print "as string" fails. The solution is to check that __doc__ is an instance of str. 2. A "temporary_class" was attached to a function node, when it shouldn't have been. The solution is to reattach it to the adhoc module.
1 parent 6dba72c commit 24f65cc

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ def _infer_type_call(self, caller, context):
20372037
col_offset=0,
20382038
end_lineno=0,
20392039
end_col_offset=0,
2040-
parent=Unknown(),
2040+
parent=caller.parent,
20412041
)
20422042

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

2074-
result.parent = caller.parent
20752074
return result
20762075

20772076
def infer_call_result(

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().adhoc_module
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)