Skip to content

Commit 405bb32

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 7b79ec0 commit 405bb32

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
@@ -2067,7 +2067,7 @@ def _infer_type_call(self, caller, context):
20672067
col_offset=0,
20682068
end_lineno=0,
20692069
end_col_offset=0,
2070-
parent=Unknown(),
2070+
parent=caller.parent,
20712071
)
20722072

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

2104-
result.parent = caller.parent
21052104
return result
21062105

21072106
def infer_call_result(

astroid/raw_building.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,8 @@ def _astroid_bootstrapping() -> None:
672672
col_offset=0,
673673
end_lineno=0,
674674
end_col_offset=0,
675-
parent=nodes.Unknown(),
675+
parent=astroid_builtin,
676676
)
677-
_UnionTypeType.parent = astroid_builtin
678677
union_type_doc_node = (
679678
nodes.Const(value=types.UnionType.__doc__)
680679
if types.UnionType.__doc__
@@ -709,9 +708,8 @@ def _astroid_bootstrapping() -> None:
709708
col_offset=0,
710709
end_lineno=0,
711710
end_col_offset=0,
712-
parent=nodes.Unknown(),
711+
parent=astroid_builtin,
713712
)
714-
klass.parent = astroid_builtin
715713
doc = _type.__doc__ if isinstance(_type.__doc__, str) else None
716714
klass.postinit(
717715
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,
@@ -1956,7 +1957,9 @@ def test_str_repr_no_warnings(node):
19561957
if name == "self":
19571958
continue
19581959

1959-
if "int" in param_type.annotation:
1960+
if name == "parent" and "NodeNG" in param_type.annotation:
1961+
args[name] = AstroidManager().adhoc_module
1962+
elif "int" in param_type.annotation:
19601963
args[name] = random.randint(0, 50)
19611964
elif (
19621965
"NodeNG" in param_type.annotation

0 commit comments

Comments
 (0)