From f813d25d7a94ca3418b2bc5265b3886b1536db7f Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 26 Mar 2022 10:14:33 -0400 Subject: [PATCH] Avoid adding the name of a parent namedtuple to child's locals --- ChangeLog | 3 +++ astroid/brain/brain_namedtuple_enum.py | 6 +++++- tests/unittest_brain.py | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index fdd1a959ed..8bb38f9d73 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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? diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index e20001176e..dbd96670f9 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -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 class_node.postinit( # set base class=tuple bases=[base_type], diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index ff7c5c6061..989cebda2f 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -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)