Skip to content

AttributeError: 'AssignAttr' object has no attribute 'name' in namedtuple brain #3257

Description

@correctmost

Steps to reproduce

The following code triggers an AttributeError in astroid:

from typing import NamedTuple

class C(NamedTuple):
    a.b: str

C()

I found the bug during a local fuzzing run.

Current behavior

Traceback (most recent call last):
  File "pylint/pylint/checkers/utils.py", line 1378, in safe_infer
    value = next(infer_gen)
  File "astroid/astroid/nodes/node_ng.py", line 160, in infer
    for i, result in enumerate(self._infer(context=context)):
                     ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "astroid/astroid/decorators.py", line 79, in inner
    yield next(generator)
          ~~~~^^^^^^^^^^^
  File "astroid/astroid/decorators.py", line 42, in wrapped
    for res in _func(node, context):
               ~~~~~^^^^^^^^^^^^^^^
  File "astroid/astroid/nodes/node_classes.py", line 1763, in _infer
    for callee in self.func.infer(context):
                  ~~~~~~~~~~~~~~~^^^^^^^^^
  File "astroid/astroid/nodes/node_ng.py", line 160, in infer
    for i, result in enumerate(self._infer(context=context)):
                     ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "astroid/astroid/decorators.py", line 79, in inner
    yield next(generator)
          ~~~~^^^^^^^^^^^
  File "astroid/astroid/decorators.py", line 42, in wrapped
    for res in _func(node, context):
               ~~~~~^^^^^^^^^^^^^^^
  File "astroid/astroid/bases.py", line 185, in _infer_stmts
    for inf in stmt.infer(context=context):
               ~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "astroid/astroid/nodes/node_ng.py", line 140, in infer
    for result in self._explicit_inference(
                  ~~~~~~~~~~~~~~~~~~~~~~~~^
        self,  # type: ignore[arg-type]
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        context,
        ^^^^^^^^
    ):
    ^
  File "astroid/astroid/inference_tip.py", line 67, in inner
    raise e from None
  File "astroid/astroid/inference_tip.py", line 64, in inner
    result = _cache[func, node, context] = list(func(node, context))
                                                ~~~~^^^^^^^^^^^^^^^
  File "astroid/astroid/brain/brain_namedtuple_enum.py", line 577, in infer_typing_namedtuple_class
    annassign.target.name
AttributeError: 'AssignAttr' object has no attribute 'name'. Did you mean: 'frame'?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "pylint/pylint/utils/ast_walker.py", line 87, in walk
    callback(astroid)
    ~~~~~~~~^^^^^^^^^
  File "pylint/pylint/checkers/base/basic_checker.py", line 475, in visit_expr
    inferred = utils.safe_infer(expr) if name[:1].isupper() else None
               ~~~~~~~~~~~~~~~~^^^^^^
  File "pylint/pylint/checkers/utils.py", line 1382, in safe_infer
    raise AstroidError from e
astroid.exceptions.AstroidError

Expected behavior

No crash

Version info

cec5b897
pylint-dev/pylint@5ba4f69e7
Python 3.14.7

The crash also affects Pylint 4.0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions