pylint-dev / pylint-dev/astroid
IndexError when inferring function arguments
- Dominant language
- Python
- Stars
- 582
- Forks
- 357
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
### Steps to reproduce
The following code triggers an `IndexError` in astroid:
```python
def f(x, *y, y: tuple[x]):
pass
```
I found the bug during a local fuzzing run.
### Current behavior
```python-traceback
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
pylint-dev/astroid@cec5b897
pylint-dev/pylint@5ba4f69e7
Python 3.14.7
The crash also affects Pylint 4.0.7
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the crash with the function signature in the issue, then inspect astroid/astroid/protocols.py at _arguments_infer_argname and astroid/astroid/nodes/node_classes.py at default_value, where the traceback ends. Trace how the argument is mapped to its default value and verify that the reproducer completes without an IndexError or AstroidError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100