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

RecursionError when assigning and referencing certain class instance attributes #2740

Closed
jpetrucciani opened this issue Feb 11, 2019 · 1 comment · Fixed by pylint-dev/astroid#660
Assignees
Labels
Bug 🪲 Crash 💥 A bug that makes pylint crash

Comments

@jpetrucciani
Copy link

Steps to reproduce

  1. create a class with an instance attribute of type list
  2. access an index of that list attribute inside a method of the class
  3. set the value of the list attribute while referencing the len() of the list attribute

Here's a snippet of code that triggers this issue:

class Foo:
    def __init__(self):
        self.items = [None] * 10

    def get(self, index):
        return self.items[index]

    def clear(self):
        self.items = [None] * len(self.items)

Current behavior

RecursionError: maximum recursion depth exceeded with default recursion limit.

Increasing the recursion limit in the .pylintrc file like so:

[MASTER]
init-hook='import sys; sys.setrecursionlimit(100000)'

causes a segfault

Full stack trace:

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 0x7fc0a5b76510>, <Call l.9 at 0x7fc0a5623518>)

During handling of the above exception, another exception occurred:

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 0x7fc0a5b76510>, <Call l.9 at 0x7fc0a5623518>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/pylint", line 10, 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 1608, in __init__
    linter.check(args)
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 938, in check
    self._do_check(files_or_modules)
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 1071, in _do_check
    self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
  File "/usr/local/lib/python3.7/site-packages/pylint/lint.py", line 1154, in check_astroid_module
    walker.walk(ast_node)
  File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1269, in walk
    self.walk(child)
  File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1269, in walk
    self.walk(child)
  File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1269, in walk
    self.walk(child)
  [Previous line repeated 1 more time]
  File "/usr/local/lib/python3.7/site-packages/pylint/utils.py", line 1266, in walk
    cb(astroid)
  File "/usr/local/lib/python3.7/site-packages/pylint/checkers/typecheck.py", line 1510, in visit_subscript
    inferred = safe_infer(node.value)
  File "/usr/local/lib/python3.7/site-packages/pylint/checkers/utils.py", line 1077, in safe_infer
    next(inferit)
  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/decorators.py", line 128, in raise_if_nothing_inferred
    yield next(generator)
  File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 314, in infer_attribute
    yield from owner.igetattr(self.attrname, context)
  File "/usr/local/lib/python3.7/site-packages/astroid/bases.py", line 217, in igetattr
    self._wrap_attr(get_attr, context), context, frame=self
  File "/usr/local/lib/python3.7/site-packages/astroid/bases.py", line 135, 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 128, 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/bases.py", line 135, 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 115, in yes_if_nothing_inferred
    for node in func(*args, **kwargs):
  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 468, in _filter_operation_errors
    for result in infer_callable(self, context):
  File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 753, in _infer_binop
    rhs_iter = right.infer(context=rhs_context)
  File "/usr/local/lib/python3.7/site-packages/astroid/node_classes.py", line 348, 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 132, in _transform_wrapper
    result = transform(node, context=context)
  File "/usr/local/lib/python3.7/site-packages/astroid/brain/brain_builtin_inference.py", line 676, in infer_len
    return nodes.Const(helpers.object_len(argument_node))
  File "/usr/local/lib/python3.7/site-packages/astroid/helpers.py", line 245, in object_len
    inferred_node = safe_infer(node, context=context)
  File "/usr/local/lib/python3.7/site-packages/astroid/helpers.py", line 157, in safe_infer
    next(inferit)
  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/decorators.py", line 128, in raise_if_nothing_inferred
    yield next(generator)
  File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 314, in infer_attribute
    yield from owner.igetattr(self.attrname, context)
  File "/usr/local/lib/python3.7/site-packages/astroid/bases.py", line 217, in igetattr
    self._wrap_attr(get_attr, context), context, frame=self
  File "/usr/local/lib/python3.7/site-packages/astroid/bases.py", line 135, 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 128, 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/bases.py", line 135, 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 115, in yes_if_nothing_inferred
    for node in func(*args, **kwargs):
  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 468, in _filter_operation_errors
    for result in infer_callable(self, context):
  File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 753, in _infer_binop
    rhs_iter = right.infer(context=rhs_context)
  File "/usr/local/lib/python3.7/site-packages/astroid/node_classes.py", line 348, 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 132, in _transform_wrapper
    result = transform(node, context=context)
  File "/usr/local/lib/python3.7/site-packages/astroid/brain/brain_builtin_inference.py", line 676, in infer_len
    return nodes.Const(helpers.object_len(argument_node))
  File "/usr/local/lib/python3.7/site-packages/astroid/helpers.py", line 245, in object_len
    inferred_node = safe_infer(node, context=context)
  File "/usr/local/lib/python3.7/site-packages/astroid/helpers.py", line 153, in safe_infer
    value = next(inferit)
  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/decorators.py", line 128, in raise_if_nothing_inferred
    yield next(generator)
  File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 314, in infer_attribute
    yield from owner.igetattr(self.attrname, context)
  File "/usr/local/lib/python3.7/site-packages/astroid/bases.py", line 217, in igetattr
    self._wrap_attr(get_attr, context), context, frame=self
  File "/usr/local/lib/python3.7/site-packages/astroid/bases.py", line 135, 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 128, 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/bases.py", line 135, 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 115, in yes_if_nothing_inferred
    for node in func(*args, **kwargs):
  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 468, in _filter_operation_errors
    for result in infer_callable(self, context):
  File "/usr/local/lib/python3.7/site-packages/astroid/inference.py", line 754, in _infer_binop
    for lhs, rhs in itertools.product(lhs_iter, rhs_iter):
  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 128, in raise_if_nothing_inferred
    yield next(generator)
RecursionError: maximum recursion depth exceeded

Expected behavior

Correct pylint analysis with no recursion errors or segfaults

pylint --version output

tried it on the following versions:

pylint 2.2.2
astroid 2.1.0
Python 3.7.2 (default, Jan 23 2019, 02:31:57)
[GCC 6.3.0 20170516]
pylint 2.3.0-dev1
astroid 2.2.0-dev
Python 3.7.2 (default, Jan 23 2019, 02:31:57)
[GCC 6.3.0 20170516]
pylint 2.2.2
astroid 2.1.0
Python 3.6.8 (default, Feb  6 2019, 12:07:20)
[GCC 6.3.0 20170516]
pylint 2.3.0-dev1
astroid 2.2.0-dev
Python 3.6.8 (default, Feb  6 2019, 12:07:20)
[GCC 6.3.0 20170516]
@PCManticore
Copy link
Contributor

Thanks for reporting the issue @jpetrucciani I can reproduce it

@PCManticore PCManticore added Bug 🪲 Crash 💥 A bug that makes pylint crash labels Feb 11, 2019
@brycepg brycepg self-assigned this Mar 30, 2019
brycepg added a commit to brycepg/astroid that referenced this issue Mar 30, 2019
The dropped context was causing InferenceErrors
in self referential assignment

Example:

    self.a = len(self.a)

There is a bigger problem of inference not understanding control flow
that this fix does not solve.

Close pylint-dev/pylint#2736
Close pylint-dev/pylint#2734
Close pylint-dev/pylint#2740
brycepg added a commit to brycepg/astroid that referenced this issue Mar 30, 2019
The dropped context was causing RecursionErrors
in self referential assignment

Example:

    self.a = len(self.a)

There is a bigger problem of inference not understanding control flow
that this fix does not solve.

Close pylint-dev/pylint#2736
Close pylint-dev/pylint#2734
Close pylint-dev/pylint#2740
brycepg added a commit to brycepg/astroid that referenced this issue Mar 30, 2019
The dropped context was causing RecursionErrors
in self referential assignment

Example:

    self.a = len(self.a)

There is a bigger problem of inference not understanding control flow
that this fix does not solve.

Close pylint-dev/pylint#2736
Close pylint-dev/pylint#2734
Close pylint-dev/pylint#2740
PCManticore pushed a commit to pylint-dev/astroid that referenced this issue Apr 1, 2019
The dropped context was causing RecursionErrors
in self referential assignment

Example:

    self.a = len(self.a)

There is a bigger problem of inference not understanding control flow
that this fix does not solve.

Close pylint-dev/pylint#2736
Close pylint-dev/pylint#2734
Close pylint-dev/pylint#2740
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants