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

astroid.exceptions.InferenceError when module contains collections.py file #4284

Closed
AudriusButkevicius opened this issue Apr 1, 2021 · 2 comments · Fixed by pylint-dev/astroid#923 or #4334
Labels
Milestone

Comments

@AudriusButkevicius
Copy link

Steps to reproduce

Given multiple files:

a.py
collections.py

a.py:

from typing import List

collections.py:

# Blank

In the real world collections.py is in some util module, and linting any file in the util module blows up.

Current behavior

Result of pylint a.py:

************* Module a
a.py:1:0: C0114: Missing module docstring (missing-module-docstring)
Traceback (most recent call last):
  File "/code/.venv/lib/python3.7/site-packages/astroid/decorators.py", line 136, in raise_if_nothing_inferred
    yield next(generator)
StopIteration: {'node': <Attribute.abc l.1205 at 0x7fc7a681b8d0>, 'context': <astroid.context.InferenceContext object at 0x7fc7a6997e30>}

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

Traceback (most recent call last):
  File "/code/.venv/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/code/.venv/lib/python3.7/site-packages/pylint/__init__.py", line 24, in run_pylint
    PylintRun(sys.argv[1:])
  File "/code/.venv/lib/python3.7/site-packages/pylint/lint/run.py", line 358, in __init__
    linter.check(args)
  File "/code/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 877, in check
    self.get_ast, self._iterate_file_descrs(files_or_modules)
  File "/code/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 910, in _check_files
    self._check_file(get_ast, check_astroid_module, name, filepath, modname)
  File "/code/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 936, in _check_file
    check_astroid_module(ast_node)
  File "/code/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 1071, in check_astroid_module
    ast_node, walker, rawcheckers, tokencheckers
  File "/code/.venv/lib/python3.7/site-packages/pylint/lint/pylinter.py", line 1115, in _check_astroid_module
    walker.walk(ast_node)
  File "/code/.venv/lib/python3.7/site-packages/pylint/utils/ast_walker.py", line 75, in walk
    self.walk(child)
  File "/code/.venv/lib/python3.7/site-packages/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/code/.venv/lib/python3.7/site-packages/pylint/checkers/variables.py", line 1201, in visit_importfrom
    module = node.do_import_module(name_parts[0])
  File "/code/.venv/lib/python3.7/site-packages/astroid/mixins.py", line 101, in do_import_module
    modname, level=level, relative_only=level and level >= 1
  File "/code/.venv/lib/python3.7/site-packages/astroid/scoped_nodes.py", line 647, in import_module
    return MANAGER.ast_from_module_name(absmodname)
  File "/code/.venv/lib/python3.7/site-packages/astroid/manager.py", line 192, in ast_from_module_name
    return self.ast_from_file(found_spec.location, modname, fallback=False)
  File "/code/.venv/lib/python3.7/site-packages/astroid/manager.py", line 101, in ast_from_file
    return AstroidBuilder(self).file_build(filepath, modname)
  File "/code/.venv/lib/python3.7/site-packages/astroid/builder.py", line 139, in file_build
    return self._post_build(module, encoding)
  File "/code/.venv/lib/python3.7/site-packages/astroid/builder.py", line 163, in _post_build
    module = self._manager.visit_transforms(module)
  File "/code/.venv/lib/python3.7/site-packages/astroid/manager.py", line 78, in visit_transforms
    return self._transform.visit(node)
  File "/code/.venv/lib/python3.7/site-packages/astroid/transforms.py", line 89, in visit
    module.body = [self._visit(child) for child in module.body]
  File "/code/.venv/lib/python3.7/site-packages/astroid/transforms.py", line 89, in <listcomp>
    module.body = [self._visit(child) for child in module.body]
  File "/code/.venv/lib/python3.7/site-packages/astroid/transforms.py", line 54, in _visit
    visited = self._visit_generic(value)
  File "/code/.venv/lib/python3.7/site-packages/astroid/transforms.py", line 67, in _visit_generic
    return self._visit(node)
  File "/code/.venv/lib/python3.7/site-packages/astroid/transforms.py", line 57, in _visit
    return self._transform(node)
  File "/code/.venv/lib/python3.7/site-packages/astroid/transforms.py", line 40, in _transform
    ret = transform_func(node)
  File "/code/.venv/lib/python3.7/site-packages/astroid/brain/brain_typing.py", line 179, in infer_typing_alias
    res = next(node.args[0].infer(context=ctx))
  File "/code/.venv/lib/python3.7/site-packages/astroid/node_classes.py", line 366, in infer
    yield from self._infer(context, **kwargs)
  File "/code/.venv/lib/python3.7/site-packages/astroid/decorators.py", line 136, in raise_if_nothing_inferred
    yield next(generator)
  File "/code/.venv/lib/python3.7/site-packages/astroid/decorators.py", line 100, in wrapped
    res = next(generator)
  File "/code/.venv/lib/python3.7/site-packages/astroid/inference.py", line 294, in infer_attribute
    for owner in self.expr.infer(context):
  File "/code/.venv/lib/python3.7/site-packages/astroid/node_classes.py", line 380, in infer
    for i, result in enumerate(generator):
  File "/code/.venv/lib/python3.7/site-packages/astroid/decorators.py", line 141, in raise_if_nothing_inferred
    raise exceptions.InferenceError(**error.args[0]) from error
astroid.exceptions.InferenceError: Inference failed for <Attribute.abc l.1205 at 0x7fc7a681b8d0>.

Expected behavior

No exception

pylint --version output

Result of pylint --version output:

pylint 3.0.0a1
astroid 2.5.2
Python 3.7.9 (default, Jan 14 2021, 11:27:07) 
[GCC 7.3.1 20180712 (Red Hat 7.3.1-12)]

Tried latest commit, same issue.

@AudriusButkevicius
Copy link
Author

AudriusButkevicius commented Apr 1, 2021

Interestingly adding __init__.py file at the root of the module fixes it, but it's not required for python 3.3+ I believe?

@cdce8p
Copy link
Member

cdce8p commented Apr 1, 2021

@Pierre-Sassoulas @hippo91 I believe we've found the first real world issue for #4206

@AudriusButkevicius Thanks for the report! We are currently working on a solution, but this is a complex issue with astroid that will take some more time. You can track the progress here: pylint-dev/astroid#921. Although not yet complete, the MR already fixes it.

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.

2 participants