Skip to content

Commit 4f48694

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Avoid adding the name of a parent namedtuple to child's locals (#1489)
1 parent 699350e commit 4f48694

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in astroid 2.11.2?
1212
=============================
1313
Release date: TBA
1414

15+
* Avoided adding the name of a parent namedtuple to its child's locals.
16+
17+
Refs PyCQA/pylint#5982
1518

1619

1720
What's New in astroid 2.11.1?

astroid/brain/brain_namedtuple_enum.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ def infer_func_form(
133133
# we know it is a namedtuple anyway.
134134
name = name or "Uninferable"
135135
# we want to return a Class node instance with proper attributes set
136-
class_node = nodes.ClassDef(name, parent=node.parent)
136+
class_node = nodes.ClassDef(name)
137+
# A typical ClassDef automatically adds its name to the parent scope,
138+
# but doing so causes problems, so defer setting parent until after init
139+
# see: https://github.com/PyCQA/pylint/issues/5982
140+
class_node.parent = node.parent
137141
class_node.postinit(
138142
# set base class=tuple
139143
bases=[base_type],

tests/unittest_brain.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ class X(namedtuple("X", ["a", "b", "c"])):
159159
self.assertEqual(
160160
[anc.name for anc in klass.ancestors()], ["X", "tuple", "object"]
161161
)
162+
# See: https://github.com/PyCQA/pylint/issues/5982
163+
self.assertNotIn("X", klass.locals)
162164
for anc in klass.ancestors():
163165
self.assertFalse(anc.parent is None)
164166

0 commit comments

Comments
 (0)