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 crashes if PySide is loaded with init hook [Regression] #7616

Closed
CarliJoy opened this issue Oct 13, 2022 · 2 comments
Closed

Pylint crashes if PySide is loaded with init hook [Regression] #7616

CarliJoy opened this issue Oct 13, 2022 · 2 comments
Labels
Astroid Related to astroid Crash 💥 A bug that makes pylint crash Duplicate 🐫 Duplicate of an already existing issue

Comments

@CarliJoy
Copy link
Contributor

CarliJoy commented Oct 13, 2022

Bug description

We are using the init-hook to load a venv, so we don't have to install pylint in all project venv and can use pre-commit hooks easier.

Our last pylint version was

pylint 2.13.9
astroid 2.11.7

With the update to

pylint 2.15.4
astroid 2.12.11

pylint crahses when trying to lint a file that contains a PySide6 import, when adding PySide to extension-pkg-allow-list.

To reproduce:

  1. Create a venv for your script that contains the required PySide installation. python3.10 -m venv qtenv
# pip freeze
PySide6==6.3.2
PySide6-Addons==6.3.2
PySide6-Essentials==6.3.2
shiboken6==6.3.2
  1. Create a venv with pylint python3.10 -m venv pylint_env; pylint_env/bin/pip install pylint
  2. Create a simple testfile.py
from PySide6.QtCore import Qt
  1. Try to lint the file (see below)

Configuration

No response

Command used

./pylint_env/bin/pylint --extension-pkg-allow-list=PySide6 --init-hook 'import sys;sys.path.append("/absolute/path/to/qtenv/lib/python3.10/site-packages");sys.path.append("/absolute/path/to/qtenv/lib64/python3.10/site-packages");print(sys.path)'  testfile.py

Pylint output

pylint crashed with a AstroidError and with the following stacktrace:

Traceback (most recent call last):
  File "pylint_env/lib64/python3.10/site-packages/astroid/modutils.py", line 590, in _spec_from_modpath
    found_spec = spec.find_spec(modpath, [context])
  File "pylint_env/lib64/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 392, in find_spec
    finder, spec = _find_spec_with_path(
  File "pylint_env/lib64/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 361, in _find_spec_with_path
    raise ImportError(f"No module named {'.'.join(module_parts)}")
ImportError: No module named PySide6.QtCore.Qt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pylint_env/lib64/python3.10/site-packages/pylint/lint/pylinter.py", line 782, in _lint_file
    check_astroid_module(module)
  File "pylint_env/lib64/python3.10/site-packages/pylint/lint/pylinter.py", line 1052, in check_astroid_module
    retval = self._check_astroid_module(
  File "pylint_env/lib64/python3.10/site-packages/pylint/lint/pylinter.py", line 1102, in _check_astroid_module
    walker.walk(node)
  File "pylint_env/lib64/python3.10/site-packages/pylint/utils/ast_walker.py", line 93, in walk
    self.walk(child)
  File "pylint_env/lib64/python3.10/site-packages/pylint/utils/ast_walker.py", line 90, in walk
    callback(astroid)
  File "pylint_env/lib64/python3.10/site-packages/pylint/checkers/imports.py", line 523, in visit_importfrom
    self._add_imported_module(node, f"{imported_module.name}.{name}")
  File "pylint_env/lib64/python3.10/site-packages/pylint/checkers/imports.py", line 833, in _add_imported_module
    importedmodname = astroid.modutils.get_module_part(
  File "pylint_env/lib64/python3.10/site-packages/astroid/modutils.py", line 438, in get_module_part
    file_from_modpath(
  File "pylint_env/lib64/python3.10/site-packages/astroid/modutils.py", line 334, in file_from_modpath
    return file_info_from_modpath(modpath, path, context_file).location
  File "pylint_env/lib64/python3.10/site-packages/astroid/modutils.py", line 384, in file_info_from_modpath
    return _spec_from_modpath(modpath, path, context)
  File "pylint_env/lib64/python3.10/site-packages/astroid/modutils.py", line 593, in _spec_from_modpath
    found_spec = spec.find_spec(modpath, path)
  File "pylint_env/lib64/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 392, in find_spec
    finder, spec = _find_spec_with_path(
  File "pylint_env/lib64/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 354, in _find_spec_with_path
    spec = finder_instance.find_module(
  File "pylint_env/lib64/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 203, in find_module
    if util.is_namespace(modname) and modname in sys.modules:
  File "pylint_env/lib64/python3.10/site-packages/astroid/interpreter/_import/util.py", line 42, in is_namespace
    found_spec = _find_spec_from_path(
  File "/usr/lib64/python3.10/importlib/util.py", line 58, in _find_spec_from_path
    return _find_spec(name, path)
  File "<frozen importlib._bootstrap>", line 945, in _find_spec
  File "<frozen importlib._bootstrap_external>", line 1448, in find_spec
  File "<frozen importlib._bootstrap_external>", line 1222, in __init__
  File "<frozen importlib._bootstrap_external>", line 1238, in _get_parent_path
AttributeError: module 'PySide6.QtCore' has no attribute '__path__'

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

Traceback (most recent call last):
  File "pylint_env/lib64/python3.10/site-packages/pylint/lint/pylinter.py", line 747, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "pylint_env/lib64/python3.10/site-packages/pylint/lint/pylinter.py", line 784, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Expected behavior

Pylint find the import like in the previous version used and does not crash.

Pylint version

pylint 2.15.4
astroid 2.12.11
Python 3.10.7 (main, Sep  7 2022, 00:00:00) [GCC 11.3.1 20220421 (Red Hat 11.3.1-2)]

OS / Environment

Fedora release 35 (Thirty Five)

Notes

Using --unsafe-load-any-extension=y and/or --disable=c-extension-no-member won't crash but it also doesn't find the import and shows.

testfile.py:1:0: E0611: No name 'Qt' in module 'PySide6.QtCore' (no-name-in-module)
@CarliJoy CarliJoy added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 13, 2022
@CarliJoy
Copy link
Contributor Author

Installing pylint in the same env as PySide works as expected but this is unfortunately not an option for us.

@Pierre-Sassoulas Pierre-Sassoulas added Crash 💥 A bug that makes pylint crash Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 13, 2022
@jacobtylerwalls
Copy link
Member

Thanks for the report. I'm going to mark this as a duplicate of #7592 because in the next patch release of astroid we're going to include AttributeError among the exception types we catch in the relevant portion of is_namespace.

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Oct 14, 2022
@jacobtylerwalls jacobtylerwalls added Astroid Related to astroid Duplicate 🐫 Duplicate of an already existing issue and removed Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning labels Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astroid Related to astroid Crash 💥 A bug that makes pylint crash Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

3 participants