Skip to content

Avoid adding the name of a parent namedtuple to child's locals #1489

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

Merged
merged 1 commit into from
Mar 26, 2022
Merged
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
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ What's New in astroid 2.11.2?
=============================
Release date: TBA

* Avoided adding the name of a parent namedtuple to its child's locals.

Refs PyCQA/pylint#5982


What's New in astroid 2.11.1?
Expand Down
6 changes: 5 additions & 1 deletion astroid/brain/brain_namedtuple_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ def infer_func_form(
# we know it is a namedtuple anyway.
name = name or "Uninferable"
# we want to return a Class node instance with proper attributes set
class_node = nodes.ClassDef(name, parent=node.parent)
class_node = nodes.ClassDef(name)
# 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/PyCQA/pylint/issues/5982
class_node.parent = node.parent
Comment on lines +137 to +140
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class_node.postinit(
# set base class=tuple
bases=[base_type],
Expand Down
2 changes: 2 additions & 0 deletions tests/unittest_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ class X(namedtuple("X", ["a", "b", "c"])):
self.assertEqual(
[anc.name for anc in klass.ancestors()], ["X", "tuple", "object"]
)
# See: https://github.com/PyCQA/pylint/issues/5982
self.assertNotIn("X", klass.locals)
for anc in klass.ancestors():
self.assertFalse(anc.parent is None)

Expand Down