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

Fix namedtuple parent #2555

Merged
Merged
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
48 changes: 23 additions & 25 deletions astroid/brain/brain_namedtuple_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def _extract_namedtuple_arg_or_keyword( # pylint: disable=inconsistent-return-s

def infer_func_form(
node: nodes.Call,
base_type: list[nodes.NodeNG],
base_type: nodes.NodeNG,
*,
parent: nodes.NodeNG,
context: InferenceContext | None = None,
enum: bool = False,
) -> tuple[nodes.ClassDef, str, list[str]]:
Expand Down Expand Up @@ -147,15 +149,10 @@ def infer_func_form(
col_offset=node.col_offset,
end_lineno=node.end_lineno,
end_col_offset=node.end_col_offset,
parent=nodes.Unknown(),
parent=parent,
)
# A typical ClassDef automatically adds its name to the parent scope,
# but doing so causes problems, so defer setting parent until after init
# see: https://github.com/pylint-dev/pylint/issues/5982
class_node.parent = node.parent
class_node.postinit(
# set base class=tuple
bases=base_type,
bases=[base_type],
body=[],
decorators=None,
)
Expand Down Expand Up @@ -195,25 +192,16 @@ def infer_named_tuple(
node: nodes.Call, context: InferenceContext | None = None
) -> Iterator[nodes.ClassDef]:
"""Specific inference function for namedtuple Call node."""
tuple_base_name: list[nodes.NodeNG] = [
nodes.Name(
name="tuple",
parent=node.root(),
lineno=0,
col_offset=0,
end_lineno=None,
end_col_offset=None,
)
]
tuple_base: nodes.Name = _extract_single_node("tuple")
class_node, name, attributes = infer_func_form(
node, tuple_base_name, context=context
node, tuple_base, parent=AstroidManager().adhoc_module, context=context
)

call_site = arguments.CallSite.from_call(node, context=context)
node = extract_node("import collections; collections.namedtuple")
try:
func = next(node.infer())
except StopIteration as e:
raise InferenceError(node=node) from e
func = util.safe_infer(
_extract_single_node("import collections; collections.namedtuple")
)
assert isinstance(func, nodes.NodeNG)
try:
rename = next(
call_site.infer_argument(func, "rename", context or InferenceContext())
Expand Down Expand Up @@ -365,7 +353,17 @@ def value(self):
__members__ = ['']
"""
)
class_node = infer_func_form(node, [enum_meta], context=context, enum=True)[0]

# FIXME arguably, the base here shouldn't be the EnumMeta class definition
# itself, but a reference (Name) to it. Otherwise, the invariant that all
# children of a node have that node as their parent is broken.
class_node = infer_func_form(
node,
enum_meta,
parent=AstroidManager().adhoc_module,
context=context,
enum=True,
)[0]
return iter([class_node.instantiate_class()])


Expand Down
Loading