Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError in FunctionDef._infer() #817

Closed
eugene57 opened this issue Jul 14, 2020 · 1 comment · Fixed by #1477
Closed

IndexError in FunctionDef._infer() #817

eugene57 opened this issue Jul 14, 2020 · 1 comment · Fixed by #1477
Labels
Milestone

Comments

@eugene57
Copy link

eugene57 commented Jul 14, 2020

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

@PCManticore
Copy link
Contributor

Thanks for the great report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants