Skip to content

IndexError when inferring function arguments #3259

Description

@correctmost

Steps to reproduce

The following code triggers an IndexError in astroid:

def f(x, *y, y: tuple[x]):
    pass

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/bases.py", line 185, in _infer_stmts
    for inf in stmt.infer(context=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/nodes/node_classes.py", line 453, in _infer
    stmts = list(self.assigned_stmts(context=context))
  File "astroid/astroid/protocols.py", line 526, in _arguments_infer_argname
    yield from self.default_value(name).infer(context)
               ~~~~~~~~~~~~~~~~~~^^^^^^
  File "astroid/astroid/nodes/node_classes.py", line 952, in default_value
    return self.defaults[idx]
           ~~~~~~~~~~~~~^^^^^
IndexError: list index out of range

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/variables.py", line 3485, in visit_subscript
    inferred_slice = utils.safe_infer(node.slice)
  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