Skip to content

Commit a147888

Browse files
committed
set proper parents for namedtuple's and enum's
- 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). it's a part of the campaign to get rid of non-module roots
1 parent b4ac0e2 commit a147888

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

astroid/brain/brain_namedtuple_enum.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ def _extract_namedtuple_arg_or_keyword( # pylint: disable=inconsistent-return-s
7474

7575
def infer_func_form(
7676
node: nodes.Call,
77-
base_type: list[nodes.NodeNG],
77+
base_type: nodes.NodeNG,
78+
*,
79+
parent: nodes.NodeNG,
7880
context: InferenceContext | None = None,
7981
enum: bool = False,
8082
) -> tuple[nodes.ClassDef, str, list[str]]:
@@ -147,15 +149,11 @@ def infer_func_form(
147149
col_offset=node.col_offset,
148150
end_lineno=node.end_lineno,
149151
end_col_offset=node.end_col_offset,
150-
parent=nodes.Unknown(),
152+
parent=parent,
151153
)
152-
# A typical ClassDef automatically adds its name to the parent scope,
153-
# but doing so causes problems, so defer setting parent until after init
154-
# see: https://github.com/pylint-dev/pylint/issues/5982
155-
class_node.parent = node.parent
156154
class_node.postinit(
157155
# set base class=tuple
158-
bases=base_type,
156+
bases=[base_type],
159157
body=[],
160158
decorators=None,
161159
)
@@ -195,25 +193,16 @@ def infer_named_tuple(
195193
node: nodes.Call, context: InferenceContext | None = None
196194
) -> Iterator[nodes.ClassDef]:
197195
"""Specific inference function for namedtuple Call node."""
198-
tuple_base_name: list[nodes.NodeNG] = [
199-
nodes.Name(
200-
name="tuple",
201-
parent=node.root(),
202-
lineno=0,
203-
col_offset=0,
204-
end_lineno=None,
205-
end_col_offset=None,
206-
)
207-
]
196+
tuple_base: nodes.Name = _extract_single_node("tuple")
208197
class_node, name, attributes = infer_func_form(
209-
node, tuple_base_name, context=context
198+
node, tuple_base, parent=AstroidManager().adhoc_module, context=context
210199
)
200+
211201
call_site = arguments.CallSite.from_call(node, context=context)
212-
node = extract_node("import collections; collections.namedtuple")
213-
try:
214-
func = next(node.infer())
215-
except StopIteration as e:
216-
raise InferenceError(node=node) from e
202+
func = util.safe_infer(
203+
_extract_single_node("import collections; collections.namedtuple")
204+
)
205+
assert isinstance(func, nodes.NodeNG)
217206
try:
218207
rename = next(
219208
call_site.infer_argument(func, "rename", context or InferenceContext())
@@ -364,7 +353,17 @@ def value(self):
364353
__members__ = ['']
365354
"""
366355
)
367-
class_node = infer_func_form(node, [enum_meta], context=context, enum=True)[0]
356+
357+
# FIXME arguably, the base here shouldn't be the EnumMeta class definition
358+
# itself, but a reference (Name) to it. Otherwise, the invariant that all
359+
# children of a node have that node as their parent is broken.
360+
class_node = infer_func_form(
361+
node,
362+
enum_meta,
363+
parent=AstroidManager().adhoc_module,
364+
context=context,
365+
enum=True,
366+
)[0]
368367
return iter([class_node.instantiate_class()])
369368

370369

0 commit comments

Comments
 (0)