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

pylint runtime error (StopIteration) due to _inference_tip_cached KeyError #2937

Closed
cj-matti-niemenmaa opened this issue May 28, 2019 · 6 comments
Assignees
Labels
Bug 🪲 Crash 💥 A bug that makes pylint crash python 3.7

Comments

@cj-matti-niemenmaa
Copy link

Steps to reproduce

Run pylint on a file containing the following (I called it asdf.py but I doubt it matters):

def get_list():
    return [1] if True else [2]

def find_int():
    return int(get_list()[0])

def foo():
    if True:
        return find_int()
    else:
        return find_int()

x = foo()
assert x / x > 0

(This is obviously a heavily reduced case based on the actual code...)

Current behavior

************* Module asdf
/asdf.py:1:0: C0111: Missing module docstring (missing-docstring)
/asdf.py:1:0: C0111: Missing function docstring (missing-docstring)
/asdf.py:2:11: W0125: Using a conditional statement with a constant value (using-constant-test)
/asdf.py:4:0: C0111: Missing function docstring (missing-docstring)
/asdf.py:7:0: C0102: Black listed name "foo" (blacklisted-name)
/asdf.py:7:0: C0111: Missing function docstring (missing-docstring)
/asdf.py:8:4: R1705: Unnecessary "else" after "return" (no-else-return)
/asdf.py:8:4: W0125: Using a conditional statement with a constant value (using-constant-test)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/astroid/__init__.py", line 91, in _inference_tip_cached
    return iter(_cache[func, node])
KeyError: (<function register_builtin_transform.<locals>._transform_wrapper at 0x7f672229eae8>, <Call l.5 at 0x7f67218e5d68>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/astroid/scoped_nodes.py", line 1666, in infer_call_result
    yield from returnnode.value.infer(context)
  File "/usr/local/lib/python3.7/site-packages/astroid/node_classes.py", line 352, in infer
    return self._explicit_inference(self, context, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/astroid/__init__.py", line 93, in _inference_tip_cached
    result = func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/astroid/brain/brain_builtin_inference.py", line 129, in _transform_wrapper
    result = transform(node, context=context)
  File "/usr/local/lib/python3.7/site-packages/astroid/brain/brain_builtin_inference.py", line 716, in infer_int
    first_value = next(call.positional_arguments[0].infer(context=context))
StopIteration

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

Traceback (most recent call last):
  File "/usr/local/bin/pylint", line 11, in <module>
    sys.exit(run_pylint())
  File "/usr/local/lib/python3.7/site-packages/pylint/__init__.py", line 20, in run_pylint
    Run(sys.argv[1:])
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 1696, in __init__
    linter.check(args)
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 983, in check
    self._do_check(files_or_modules)
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 1143, in _do_check
    self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 1227, in check_astroid_module
    walker.walk(ast_node)
  File "/usr/local/lib/python3.7/site-packages/pylint/utils/ast_walker.py", line 77, in walk
    self.walk(child)
  File "/usr/local/lib/python3.7/site-packages/pylint/utils/ast_walker.py", line 77, in walk
    self.walk(child)
  File "/usr/local/lib/python3.7/site-packages/pylint/utils/ast_walker.py", line 74, in walk
    callback(astroid)
  File "/usr/local/lib/python3.7/site-packages/pylint/checkers/base.py", line 1779, in visit_assignname
    if isinstance(utils.safe_infer(assign_type.value), astroid.ClassDef):
  File "/usr/local/lib/python3.7/site-packages/pylint/checkers/utils.py", line 1087, in safe_infer
    next(inferit)
  File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 141, in raise_if_nothing_inferred
    yield from generator
  File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 95, in wrapped
    res = next(generator)
  File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 219, in infer_call
    yield from callee.infer_call_result(caller=self, context=callcontext)
  File "/usr/local/lib/python3.7/site-packages/astroid/scoped_nodes.py", line 1666, in infer_call_result
    yield from returnnode.value.infer(context)
  File "/usr/local/lib/python3.7/site-packages/astroid/util.py", line 160, in limit_inference
    yield from islice(iterator, size)
  File "/usr/local/lib/python3.7/site-packages/astroid/context.py", line 113, in cache_generator
    for result in generator:
  File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 131, in raise_if_nothing_inferred
    yield next(generator)
  File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 95, in wrapped
    res = next(generator)
  File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 219, in infer_call
    yield from callee.infer_call_result(caller=self, context=callcontext)
RuntimeError: generator raised StopIteration

Expected behavior

All the lints, but no exception.

pylint --version output

Reproduces on the latest pylint and astroid:

pylint 2.4.0-dev0
astroid 2.3.0
Python 3.7.1 (default, Nov 16 2018, 22:26:09)
[GCC 6.3.0 20170516]

Originally spotted on the current release versions:

pylint 2.3.1
astroid 2.2.5
Python 3.7.3 (default, Mar 26 2019, 21:43:19)
[GCC 8.2.1 20181127]
@UncleanlyCleric
Copy link

I'm getting a similar error on one of my machines:

`Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/astroid/protocols.py", line 492, in _infer_context_manager
enter = next(inferred.igetattr("enter", context=context))
StopIteration

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

Traceback (most recent call last):
File "/usr/local/bin/pylint", line 10, in
sys.exit(run_pylint())
File "/usr/local/lib/python3.7/site-packages/pylint/init.py", line 20, in run_pylint
Run(sys.argv[1:])
File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 1628, in init
linter.check(args)
File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 943, in check
self._do_check(files_or_modules)
File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 1075, in _do_check
self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 1158, in check_astroid_module
walker.walk(ast_node)
File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1303, in walk
self.walk(child)
File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1303, in walk
self.walk(child)
File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1303, in walk
self.walk(child)
[Previous line repeated 2 more times]
File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1300, in walk
cb(astroid)
File "/usr/local/lib/python3.7/site-packages/pylint/checkers/logging.py", line 215, in visit_call
result, name = is_logger_class()
File "/usr/local/lib/python3.7/site-packages/pylint/checkers/logging.py", line 197, in is_logger_class
for inferred in node.func.infer():
File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 141, in raise_if_nothing_inferred
yield from generator
File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 95, in wrapped
res = next(generator)
File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 279, in infer_attribute
for owner in self.expr.infer(context):
File "/usr/local/lib/python3.7/site-packages/astroid/util.py", line 160, in limit_inference
yield from islice(iterator, size)
File "/usr/local/lib/python3.7/site-packages/astroid/context.py", line 113, in cache_generator
for result in generator:
File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 141, in raise_if_nothing_inferred
yield from generator
File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 95, in wrapped
res = next(generator)
File "/usr/local/lib/python3.7/site-packages/astroid/bases.py", line 137, in _infer_stmts
for inferred in stmt.infer(context=context):
File "/usr/local/lib/python3.7/site-packages/astroid/util.py", line 160, in limit_inference
yield from islice(iterator, size)
File "/usr/local/lib/python3.7/site-packages/astroid/context.py", line 113, in cache_generator
for result in generator:
File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 131, in raise_if_nothing_inferred
yield next(generator)
File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 92, in wrapped
generator = _func(node, context, **kwargs)
File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 832, in infer_assign
stmts = list(self.assigned_stmts(context=context))
File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 131, in raise_if_nothing_inferred
yield next(generator)
File "/usr/local/lib/python3.7/site-packages/astroid/protocols.py", line 537, in with_assigned_stmts
yield from _infer_context_manager(self, mgr, context)
RuntimeError: generator raised StopIteration`

This only started this morning. I've tried upgrading pylint and astroid however that hasn't seemed to help.

pylint 2.3.1 astroid 2.2.5 Python 3.7.2 (default, Feb 12 2019, 08:16:38) [Clang 10.0.0 (clang-1000.11.45.5)]

@PCManticore PCManticore added Bug 🪲 Crash 💥 A bug that makes pylint crash labels May 30, 2019
@PCManticore
Copy link
Contributor

Thank you for reporting this issue, I can reproduce the bug.

@PCManticore PCManticore self-assigned this May 30, 2019
@nickdrozd
Copy link
Collaborator

Slightly simpler reproducing example:

def find_int():
    return int(([1] if True else [1])[0])

def foo():
    return find_int()
    return find_int()

x = foo()
assert True

@PCManticore
Copy link
Contributor

Thanks for the report @matti-cujo This is fixed now in astroid's master.

@danihodovic
Copy link

@PCManticore when will a new astroid release be published containing this fix?

@PCManticore
Copy link
Contributor

Hopefully this month @danihodovic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Crash 💥 A bug that makes pylint crash python 3.7
Projects
None yet
Development

No branches or pull requests

5 participants