Skip to content

IndexError in FunctionDef._infer() #817

@eugene57

Description

@eugene57

Steps to reproduce

Run following code:

import astroid
code = "def func(): pass"
funcdef = astroid.extract_node(code)
print(next(funcdef._infer()))

Current behavior

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-4-0a19b019c67b> in <module>
----> 1 print(next(funcdef._infer()))

/dat/uber/prod/python/venv/py_3.7.4_prod_20200604_0/lib/python3.7/site-packages/astroid/inference.py in _cached_generator(func, instance, args, kwargs, _cache)
    957 @wrapt.decorator
    958 def _cached_generator(func, instance, args, kwargs, _cache={}):
--> 959     node = args[0]
    960     try:
    961         return iter(_cache[func, id(node)])

IndexError: tuple index out of range

This is actually a combination of two bugs:

  1. astroid.inference._cached_generator is decorated by wrapt.decorator. And in the mean time you try to access node instance via node = args[0]. This is incorrect, you should use node = instance instead.
  2. NodeNG.infer calls _infer with context as positional argument instead of keyword one. See https://github.com/PyCQA/astroid/blob/master/astroid/node_classes.py#L363 and https://github.com/PyCQA/astroid/blob/master/astroid/node_classes.py#L371. That's why this error never happens in usual workflow.

Since _cached_generator works not as expected, this may also have some impact on inference caching, but I did not encounter any other issues here.

Expected behavior

No error

python -c "from astroid import __pkginfo__; print(__pkginfo__.version)" output

2.4.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions