Avoid adding the name of a parent namedtuple to child's locals#1489
Conversation
| # 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 |
There was a problem hiding this comment.
I borrowed the wording from:
https://github.com/PyCQA/astroid/blob/16322e84801fc143906db33868abf5813875ae30/astroid/objects.py#L265-L267
Pull Request Test Coverage Report for Build 2044799690
💛 - Coveralls |
|
I'm going to release the hot fix in 2.11.2 / 2.13.2, when @DanielNoord is available for review we can integrate his review in 2.12. |
|
I'm not sure this is correct. Doesn't this code check something tricky? from collections import namedtuple
class X(namedtuple("X", ["a", "b", "c"])):
passI believe For: from collections import namedtuple
class Y(namedtuple("X", ["a", "b", "c"])):
pass
(Pdb) klass.locals
{'__module__': [<Const.str l.4 at 0x10ebbfa30>], '__qualname__': [<Const.str l.4 at 0x10ebbf940>], 'X': [<ClassDef.X l.4 at 0x10ebbfb80>]}
(Pdb) klass
<ClassDef.X l.4 at 0x10ebbfd60>
(Pdb) klass.locals["X"]
[<ClassDef.X l.4 at 0x10ebbfb80>]
(Pdb) klass.locals["X"] == klass
FalseNote that these are two different |
|
Yes, they are different So why would we want 'X' to be in the locals of Y when it's not even in X? |
Ah yeah, didn't think about that. It's getting late here, but are there any other examples where something gets defined in the "ancestors space" of a class definition? Do we add them as well? |
Oh it definitely feels janky. I was just trying to restore status quo before the commit that caused the failure. You're right this could be audited and fixed in the |
|
Would you mind opening an issue for it? Then at least we don't forget about it :) |
|
done! |
Description
When subclassing a
namedtuple, the behavior before d30592a was for astroid to avoid adding the name of the namedtuple to the child class's locals. Apparently doing so caused the crash in pylint-dev/pylint#5982.Type of Changes
Related Issue
Refs pylint-dev/pylint#5982