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

Issue linting CFFI modules #1776

Closed
dalcinl opened this issue Sep 9, 2022 · 1 comment · Fixed by #1777
Closed

Issue linting CFFI modules #1776

dalcinl opened this issue Sep 9, 2022 · 1 comment · Fixed by #1777
Assignees
Milestone

Comments

@dalcinl
Copy link

dalcinl commented Sep 9, 2022

Steps to reproduce

  1. Install the following in the user site-packages
pip install -q --user pylint
pip install -q --user git+https://github.com/wolever/python-cffi-example.git
  1. Run the following snipped in a terminal (tested on Fedora 36 with system Python 3.10)
cd /tmp
cat <<EOF > .pylintrc
[MASTER]
extension-pkg-whitelist = cffi_example._person
EOF

cat <<EOF > test.py
from cffi_example._person import ffi, lib
EOF

pylint test.py

Current behavior

************* Module test
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
cffi_example._person.lib
Exception on node <ImportFrom l.1 at 0x7f637397f700> in file '/tmp/test.py'
Traceback (most recent call last):
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/modutils.py", line 590, in _spec_from_modpath
    found_spec = spec.find_spec(modpath, [context])
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 393, in find_spec
    finder, spec = _find_spec_with_path(
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 362, in _find_spec_with_path
    raise ImportError(f"No module named {'.'.join(module_parts)}")
ImportError: No module named cffi_example._person.lib

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dalcinl/.local/lib/python3.10/site-packages/pylint/utils/ast_walker.py", line 90, in walk
    callback(astroid)
  File "/home/dalcinl/.local/lib/python3.10/site-packages/pylint/checkers/imports.py", line 523, in visit_importfrom
    self._add_imported_module(node, f"{imported_module.name}.{name}")
  File "/home/dalcinl/.local/lib/python3.10/site-packages/pylint/checkers/imports.py", line 833, in _add_imported_module
    importedmodname = astroid.modutils.get_module_part(
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/modutils.py", line 438, in get_module_part
    file_from_modpath(
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/modutils.py", line 334, in file_from_modpath
    return file_info_from_modpath(modpath, path, context_file).location
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/modutils.py", line 384, in file_info_from_modpath
    return _spec_from_modpath(modpath, path, context)
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/modutils.py", line 593, in _spec_from_modpath
    found_spec = spec.find_spec(modpath, path)
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 393, in find_spec
    finder, spec = _find_spec_with_path(
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 355, in _find_spec_with_path
    spec = finder_instance.find_module(
  File "/home/dalcinl/.local/lib/python3.10/site-packages/astroid/interpreter/_import/spec.py", line 205, in find_module
    submodule_path = sys.modules[modname].__path__
AttributeError: cffi library 'cffi_example._person' has no function, constant or global variable named '__path__'
test.py:1:0: F0002: test.py: Fatal error while checking 'test.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/home/dalcinl/.cache/pylint/pylint-crash-2022-09-09-21-23-07.txt'. (astroid-error)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

Expected behavior

No traceback!

python -c "from astroid import __pkginfo__; print(__pkginfo__.version)" output

2.12.9

Additional information

CFFI inject objects in sys.modules that are not real module objects:

In [1]: import sys

In [2]: import cffi_example._person

In [3]: lib = sys.modules['cffi_example._person.lib']

In [4]: print(type(lib), type(lib).__base__)
<class '_cffi_backend.Lib'> <class 'object'>

In [5]: lib
Out[5]: <Lib object for 'cffi_example._person'>

As astroid is asking for the missing __path__ attribute, things go bad.
However, looking at astroid sources, I don't think that the root of the issue, but rather a confusion with detection of namespace packages.

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.12.10 milestone Sep 9, 2022
@jacobtylerwalls
Copy link
Member

Thanks for the report, same as pylint-dev/pylint#7399

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants