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

RuntimeError: generator raised StopIteration #2317

Closed
ghost opened this issue Jul 18, 2018 · 14 comments · Fixed by dcollinsn/judgeapps-dependencies#23, gita/BhagavadGita#137 or rhaamo/ahrl#13 · May be fixed by Resurfed/timer-api#22
Closed
Assignees
Labels

Comments

@ghost
Copy link

ghost commented Jul 18, 2018

Steps to reproduce

  1. pylint

Current behavior

Traceback (most recent call last):
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/protocols.py", line 306, in _arguments_infer_argname
    context.extra_context)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/arguments.py", line 31, in __init__
    self._unpacked_kwargs = self._unpack_keywords(keywords)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/arguments.py", line 77, in _unpack_keywords
    inferred = next(value.infer(context=context))
StopIteration

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

Traceback (most recent call last):
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/bin/pylint", line 11, in <module>
    sys.exit(run_pylint())
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/__init__.py", line 19, in run_pylint
    Run(sys.argv[1:])
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/lint.py", line 1394, in __init__
    linter.check(args)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/lint.py", line 801, in check
    self._do_check(files_or_modules)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/lint.py", line 938, in _do_check
    self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/lint.py", line 1018, in check_astroid_module
    walker.walk(ast_node)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/utils.py", line 1159, in walk
    self.walk(child)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/utils.py", line 1159, in walk
    self.walk(child)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/utils.py", line 1159, in walk
    self.walk(child)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/utils.py", line 1156, in walk
    cb(astroid)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/checkers/typecheck.py", line 1349, in visit_subscript
    inferred = safe_infer(node.value)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/pylint/checkers/utils.py", line 812, in safe_infer
    next(inferit)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/decorators.py", line 84, in wrapped
    res = next(generator)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/bases.py", line 113, in _infer_stmts
    for inferred in stmt.infer(context=context):
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/util.py", line 144, in limit_inference
    yield from islice(iterator, size)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/context.py", line 103, in cache_generator
    for result in generator:
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/decorators.py", line 84, in wrapped
    res = next(generator)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/bases.py", line 113, in _infer_stmts
    for inferred in stmt.infer(context=context):
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/util.py", line 144, in limit_inference
    yield from islice(iterator, size)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/context.py", line 103, in cache_generator
    for result in generator:
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/decorators.py", line 123, in raise_if_nothing_inferred
    yield next(generator)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/decorators.py", line 84, in wrapped
    res = next(generator)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/inference.py", line 197, in infer_call
    context=callcontext,
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/scoped_nodes.py", line 1609, in infer_call_result
    yield from returnnode.value.infer(context)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/util.py", line 144, in limit_inference
    yield from islice(iterator, size)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/context.py", line 103, in cache_generator
    for result in generator:
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/decorators.py", line 84, in wrapped
    res = next(generator)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/bases.py", line 113, in _infer_stmts
    for inferred in stmt.infer(context=context):
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/util.py", line 144, in limit_inference
    yield from islice(iterator, size)
  File "/Users/MYSELF/Sites/MYSITE/MYSITEenv/lib/python3.7/site-packages/astroid/context.py", line 103, in cache_generator
    for result in generator:
RuntimeError: generator raised StopIteration

Expected behavior

Report as normal.

pylint --version output

pylint 2.0.0
astroid 2.0.0.dev4
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)]
@brycepg brycepg added the Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine label Jul 18, 2018
@brycepg
Copy link
Contributor

brycepg commented Jul 18, 2018

Does this still occur after fully upgrading both astroid and pylint?: pip install --upgrade astroid pylint (since your astroid version is out of date)

If it's still reproducable, can you provide a minimal, verifiable example?

@ghost
Copy link
Author

ghost commented Jul 18, 2018

Yes, it claim latest versions are installed. I will try to get it down to class/method but probably only tomorrow. Will also try to rebuild virtualenv from scratch. There is only one, single file in whole project that fails like this.

@ghost
Copy link
Author

ghost commented Jul 18, 2018

Actually, it was easier than I thought:

from django.views.generic import TemplateView


class DVM(TemplateView):
    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        return ctx


class IFDVM(DVM):
    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx['bar'] = 'foo'
        return ctx

is enough to trigger same exception. Without ctx['bar'] = 'foo' in inheriting class it works fine.

@ghost
Copy link
Author

ghost commented Jul 18, 2018

In terms of astroid, whatever I try to do it always pulls version 2.0, which gets reported by pylint as 2.0.0.dev4.

@brycepg
Copy link
Contributor

brycepg commented Jul 18, 2018

If you look in pip freeze it looks like the astroid version is correctly 2.0.0, but pylint is just misreporting the version as 2.0.0dev4 somehow.

I can reproduce; this bug only occurs with Python 3.7

pip freeze:

-e git+git@github.com:brycepg/astroid.git@05e94e94c5428a277bbb578c91f9c4ae6f9c6c79#egg=astroid
Django==2.0.7
isort==4.3.4
lazy-object-proxy==1.3.1
mccabe==0.6.1
-e git+git@github.com:/brycepg/pylint.git@d645d2cb1720c3018fa663e8bff01880703a8861#egg=pylint
pytz==2018.5
six==1.11.0
wrapt==1.10.11

@brycepg brycepg added Bug 🪲 Astroid Related to astroid and removed Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine labels Jul 18, 2018
@brycepg
Copy link
Contributor

brycepg commented Jul 18, 2018

Here's a reproduction without django:

class ContextMixin:
    def get_context_data(self, **kwargs):
        return kwargs


class DVM(ContextMixin):
    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        return ctx


class IFDVM(DVM):
    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx['bar'] = 'foo'
        return ctx

@paul-nameless
Copy link

paul-nameless commented Jul 19, 2018

Even simpler way to reproduce

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 89, in wrapped
    res = next(generator)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 104, in wrapped
    raise StopIteration
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 16, in run_pylint
    Run(sys.argv[1:])
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 1347, in __init__
    linter.check(args)
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 768, in check
    self._do_check(files_or_modules)
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 901, in _do_check
    self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 980, in check_astroid_module
    walker.walk(ast_node)
  File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1014, in walk
    self.walk(child)
  File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1011, in walk
    cb(astroid)
  File "/usr/local/lib/python3.7/site-packages/pylint/checkers/variables.py", line 1260, in visit_importfrom
    module = node.do_import_module(name_parts[0])
  File "/usr/local/lib/python3.7/site-packages/astroid/mixins.py", line 119, in do_import_module
    relative_only=level and level >= 1)
  File "/usr/local/lib/python3.7/site-packages/astroid/scoped_nodes.py", line 593, in import_module
    return MANAGER.ast_from_module_name(absmodname)
  File "/usr/local/lib/python3.7/site-packages/astroid/manager.py", line 154, in ast_from_module_name
    return self.ast_from_file(found_spec.location, modname, fallback=False)
  File "/usr/local/lib/python3.7/site-packages/astroid/manager.py", line 80, in ast_from_file
    return AstroidBuilder(self).file_build(filepath, modname)
  File "/usr/local/lib/python3.7/site-packages/astroid/builder.py", line 153, in file_build
    return self._post_build(module, encoding)
  File "/usr/local/lib/python3.7/site-packages/astroid/builder.py", line 173, in _post_build
    self.delayed_assattr(delayed)
  File "/usr/local/lib/python3.7/site-packages/astroid/builder.py", line 232, in delayed_assattr
    for inferred in node.expr.infer():
  File "/usr/local/lib/python3.7/site-packages/astroid/decorators.py", line 89, in wrapped
    res = next(generator)
  File "/usr/local/lib/python3.7/site-packages/astroid/bases.py", line 95, in _infer_stmts
    for inferred in stmt.infer(context=context):
  File "/usr/local/lib/python3.7/site-packages/astroid/context.py", line 71, in cache_generator
    for result in generator:
RuntimeError: generator raised StopIteration

# pylint --version
pylint 1.9.2,
astroid 1.6.5
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)]

# cat test.py
from enum import Enum```

@paul-nameless
Copy link

Upgrading to astroid 2.0 fixed my issue
pip install -U astroid

@ghost
Copy link
Author

ghost commented Jul 19, 2018

● pylint test2.py
************* Module test2
test2.py:1:0: W0611: Unused Enum imported from enum (unused-import)

-----------------------------------
Your code has been rated at 0.00/10

● cat test2.py
from enum import Enum

Correct, doesn't error on 2.0.0 (astroid) - must be different issue then.

@ghost
Copy link
Author

ghost commented Jul 19, 2018

Just upgraded astroid, but still getting the same error.

pylint 2.0.0
astroid 2.0.1
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)]

@paul-nameless
Copy link

With next versions everything works fine for me:

pylint 1.9.2,
astroid 2.0.0.dev4
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)]

@brycepg brycepg self-assigned this Jul 19, 2018
@ghost
Copy link
Author

ghost commented Jul 19, 2018

This issue concerns pylint 2.0.0.

brycepg added a commit to pylint-dev/astroid that referenced this issue Jul 20, 2018
Empty generators and next calls within a generator without a default argument
now cause a cascade effect which results in a RuntimeError.

Raise InferenceError instead of return None to avoid the above problem.

Close pylint-dev/pylint#2317
@ghost
Copy link
Author

ghost commented Jul 23, 2018

Issue still present on:

pylint 2.0.1
astroid 2.0.1
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)]

@brycepg
Copy link
Contributor

brycepg commented Jul 23, 2018

My PR to fix this is still in the process of being reviewed

clrpackages pushed a commit to clearlinux-pkgs/astroid that referenced this issue Aug 2, 2018
…ethods.

Bryce Guinta (4):
      Add new Changelog heading
      Fix changelog section underline
      Add test for checking that a RuntimeError is no longer emitted in Python 3.7
      Fix lint error

Claudiu Popa (6):
      Fix unused import
      Add note to remove the already built dists
      Rename method for making it clearer
      Wrap the inference functions with raise_if_nothing_inferred where we didn't have any guards against them
      infer_call_result can raise InferenceError so make sure to handle that for the call sites where it is used
      Prepare 2.0.2

Nick Drozd (1):
      Cut obsolete "explicit StopIteration" comments

Ville Skyttä (2):
      Spelling fixes
      Use final Python 3.7 in Travis

What's New in astroid 2.0.2?
============================

Release Date: 2018-08-01

   * Stop repeat inference attempt causing a RuntimeError in Python3.7

     Close pylint-dev/pylint#2317

   *  infer_call_result can raise InferenceError so make sure to handle that for the call sites
      where it is used

     infer_call_result started recently to raise InferenceError for objects for which it
     could not find any returns. Previously it was silently raising a StopIteration,
     which was especially leaking when calling builtin methods.

(NEWS truncated at 15 lines)