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

Crash on some mypy import when using the --extension-pkg-allow-list=mypy #10223

Open
sbrunner opened this issue Feb 5, 2025 · 3 comments
Open
Labels
Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@sbrunner
Copy link

sbrunner commented Feb 5, 2025

Bug description

When parsing the following example.py:

import mypy.build

Configuration

Command used

pylint --extension-pkg-allow-list=mypy example.py

Pylint output

        pylint crashed with a ``AstroidError`` and with the following stacktrace:


Traceback (most recent call last):
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/checkers/imports.py", line 1014, in _get_imported_module
    return importnode.do_import_module(modname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/astroid/nodes/_base_nodes.py", line 168, in do_import_module
    return mymodule.import_module(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 462, in import_module
    return AstroidManager().ast_from_module_name(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/astroid/manager.py", line 251, in ast_from_module_name
    return self.ast_from_module(named_module, modname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/astroid/manager.py", line 356, in ast_from_module
    return AstroidBuilder(self).module_build(module, modname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/astroid/builder.py", line 101, in module_build
    node = self.inspect_build(module, modname=modname, path=path)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/astroid/raw_building.py", line 463, in inspect_build
    self.object_build(node, module)
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/astroid/raw_building.py", line 504, in object_build
    class_node = object_build_class(node, member, name)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/astroid/raw_building.py", line 265, in object_build_class
    return _base_class_object_build(node, member, basenames, localname=localname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/astroid/raw_building.py", line 373, in _base_class_object_build
    instdict = member().__dict__
               ^^^^^^^^^^^^^^^^^
AttributeError: 'ModuleNotFound' object has no attribute '__dict__'

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

Traceback (most recent call last):
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 788, in _lint_file
    check_astroid_module(module)
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 1017, in check_astroid_module
    retval = self._check_astroid_module(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 1069, in _check_astroid_module
    walker.walk(node)
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/utils/ast_walker.py", line 90, in walk
    self.walk(child)
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/utils/ast_walker.py", line 87, in walk
    callback(astroid)
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/checkers/imports.py", line 542, in visit_import
    imported_module = self._get_imported_module(node, name)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/checkers/imports.py", line 1039, in _get_imported_module
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

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

Traceback (most recent call last):
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 752, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "/home/sbrunner/.pyenv/versions/3.11.9/lib/python3.11/site-packages/pylint/lint/pylinter.py", line 790, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Expected behavior

No crash

Pylint version

pylint 3.3.3
astroid 3.3.8
Python 3.11.9 (main, May 14 2024, 13:36:26) [GCC 13.2.0]

OS / Environment

linux (Linux)

Additional dependencies

mypy==1.14.1
@sbrunner sbrunner added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 5, 2025
@Pierre-Sassoulas Pierre-Sassoulas added Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 5, 2025
@sbrunner sbrunner changed the title Crash on come mypy import when using the --extension-pkg-allow-list=mypy Crash on some mypy import when using the --extension-pkg-allow-list=mypy Feb 11, 2025
@DanielNoord
Copy link
Collaborator

What is ModuleNotFound here? I don't understand how something can be a subclass of Exception and not have __dict__.

@sbrunner
Copy link
Author

It's probably this class https://github.com/python/mypy/blob/52c7735ff9e0a1e60c80a31bf6ffd0b0d0d7d8a9/mypy/build.py#L1805 but mypy do a prebuild, and it has this kind or side effect!

>>> import mypy.build
>>> mypy.build.ModuleNotFound().__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'ModuleNotFound' object has no attribute '__dict__'. Did you mean: '__dir__'?

@DanielNoord
Copy link
Collaborator

Ah, it is likely do to this change in mypyc: docs.

That might cause more issues in the code, but I guess it makes sense to guard against this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

3 participants