Skip to content

Commit 38edf0e

Browse files
pylint-backport[bot]EljeesPierre-Sassoulas
authored
[Backport maintenance/4.3.x] Give a renamed namedtuple's instance the renamed fields (#3218)
* Give a renamed namedtuple's instance the renamed fields ``infer_func_form`` records the field names as written. When ``rename=True`` those names are replaced with ``_N`` afterwards, but only ``_fields`` and the class body were rebuilt from the renamed list, so the inferred instance kept attributes the namedtuple does not have and lost the duplicated ones. Closes #242 (cherry picked from commit 8771613) * changelog in proper place --------- Co-authored-by: Eljees <[email protected]> Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent a52596a commit 38edf0e

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎ChangeLog‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ What's New in astroid 4.3.1?
1313
============================
1414
Release date: TBA
1515

16+
* Fix inference of the attributes of a ``namedtuple`` created with
17+
``rename=True``. The renamed fields were applied to ``_fields`` and to the
18+
class body, but the instance kept the field names as written, so an instance
19+
of ``namedtuple("Tuple", "abc def", rename=True)`` was inferred as having a
20+
``def`` attribute instead of ``_1``, and duplicate field names collapsed into
21+
a single attribute instead of being renamed.
22+
23+
Closes #242
1624

1725

1826
What's New in astroid 4.3.0?

‎astroid/brain/brain_namedtuple_enum.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ def infer_named_tuple(
229229
except AstroidValueError as exc:
230230
raise UseInferenceDefault("ValueError: " + str(exc)) from exc
231231

232+
if tuple(class_node.instance_attrs) != tuple(attributes):
233+
# ``infer_func_form`` recorded the field names as written, but ``rename=True``
234+
# replaces invalid, keyword or duplicate names with ``_N``, so the instance
235+
# would otherwise carry names the namedtuple does not actually have (and lose
236+
# the duplicated ones entirely). Rebuild them from the renamed fields.
237+
class_node.instance_attrs.clear()
238+
for attr in attributes:
239+
fake_node = nodes.EmptyNode()
240+
fake_node.parent = class_node
241+
fake_node.attrname = attr
242+
class_node.instance_attrs[attr] = [fake_node]
243+
232244
replace_args = ", ".join(f"{arg}=None" for arg in attributes)
233245
field_def = (
234246
" {name} = property(lambda self: self[{index:d}], "

‎tests/brain/test_named_tuple.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,32 @@ def test_namedtuple_rename_duplicates(self) -> None:
115115
self.assertIn("_1", inferred.locals)
116116
self.assertIn("_2", inferred.locals)
117117

118+
def test_namedtuple_rename_keywords_instance_attrs(self) -> None:
119+
"""The instance carries the renamed field, not the name as written.
120+
121+
https://github.com/pylint-dev/astroid/issues/242
122+
"""
123+
node = builder.extract_node("""
124+
from collections import namedtuple
125+
Tuple = namedtuple("Tuple", "abc def", rename=True)
126+
Tuple(1, 2) #@
127+
""")
128+
inferred = next(node.infer())
129+
self.assertEqual(sorted(inferred.instance_attrs), ["_1", "abc"])
130+
131+
def test_namedtuple_rename_duplicates_instance_attrs(self) -> None:
132+
"""Duplicate fields are renamed rather than collapsed on the instance.
133+
134+
https://github.com/pylint-dev/astroid/issues/242
135+
"""
136+
node = builder.extract_node("""
137+
from collections import namedtuple
138+
Tuple = namedtuple("Tuple", "abc abc abc", rename=True)
139+
Tuple(1, 2, 3) #@
140+
""")
141+
inferred = next(node.infer())
142+
self.assertEqual(sorted(inferred.instance_attrs), ["_1", "_2", "abc"])
143+
118144
def test_namedtuple_rename_uninferable(self) -> None:
119145
node = builder.extract_node("""
120146
from collections import namedtuple

0 commit comments

Comments
 (0)