Skip to content

Commit 5952c15

Browse files
Fix crash on non-name assignment targets in typing.NamedTuple body
infer_typing_namedtuple_class read `target.name` for every assignment target in the class body. An AssignAttr or Subscript target has no `name`, and a Tuple target's `name` is "tuple", which is not in the class locals, so `class C(NamedTuple): t.a = None` crashed inference. Collect the AssignName nodes of each target instead, which also copies the names bound by tuple unpacking to the generated class. Closes #3190
1 parent 331e9d9 commit 5952c15

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎ChangeLog‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ Release date: TBA
2727

2828
Closes #3189
2929

30+
* Fix a crash when the body of a ``typing.NamedTuple`` subclass contains an
31+
assignment whose target is not a single name, such as ``cat.color = "black"``,
32+
``basket[0] = "apple"`` or ``apple, banana = "red", "yellow"``. The names bound
33+
by unpacking are now copied to the inferred class as well.
34+
35+
Closes #3190
36+
3037
* The documentation build now fails on any Sphinx warning, so a cross-reference
3138
that does not resolve is caught by CI instead of quietly rendering as plain
3239
text. ``Uninferable``, ``Position`` and the ``BadOperationMessage`` classes

‎astroid/brain/brain_namedtuple_enum.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,12 @@ def infer_typing_namedtuple_class(class_node, context: InferenceContext | None =
571571
for body_node in class_node.body:
572572
if isinstance(body_node, nodes.Assign):
573573
for target in body_node.targets:
574-
attr = target.name
575-
generated_class_node.locals[attr] = class_node.locals[attr]
574+
# A target is not necessarily a single name: ``cat.color = ...``
575+
# and ``basket[0] = ...`` define no class attribute at all, while
576+
# ``apple, banana = ...`` defines one per unpacked element.
577+
for assign_name in target.nodes_of_class(nodes.AssignName):
578+
attr = assign_name.name
579+
generated_class_node.locals[attr] = class_node.locals[attr]
576580
elif isinstance(body_node, nodes.ClassDef):
577581
generated_class_node.locals[body_node.name] = [body_node]
578582

‎tests/brain/test_brain.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,36 @@ class Foo:
495495
attr = next(attr_def.infer())
496496
self.assertEqual(attr.value, "bar")
497497

498+
def test_namedtuple_class_form_assignment_targets(self):
499+
"""An assignment in the body may not bind a single plain name.
500+
501+
Regression test for https://github.com/pylint-dev/astroid/issues/3190
502+
"""
503+
attribute, subscript, unpacking = builder.extract_node("""
504+
from typing import NamedTuple
505+
506+
class Attribute(NamedTuple):
507+
cat.color = "black"
508+
509+
class Subscript(NamedTuple):
510+
basket[0] = "apple"
511+
512+
class Unpacking(NamedTuple):
513+
apple, banana = "red", "yellow"
514+
515+
Attribute() #@
516+
Subscript() #@
517+
Unpacking() #@
518+
""")
519+
# Inference must not raise ``AttributeError`` or ``KeyError``.
520+
for node in (attribute, subscript, unpacking):
521+
self.assertIsInstance(next(node.infer()), astroid.Instance)
522+
523+
# The names bound by unpacking are still class attributes.
524+
inferred = next(unpacking.infer())
525+
for name, value in (("apple", "red"), ("banana", "yellow")):
526+
self.assertEqual(next(inferred.getattr(name)[0].infer()).value, value)
527+
498528
def test_tuple_type(self):
499529
node = builder.extract_node("""
500530
from typing import Tuple

0 commit comments

Comments
 (0)