pylint
pylint copied to clipboard
.py files having precedence over .pyi files causes problems with opencv-python module (and probably others)
Bug description
If module has both .py and .pyi files available, .py file is always used according to the code added in pull request pylint-dev/astroid#2182 Unfortunately, this doesn't always work correctly, as noted in the discussion of that pull request (even pylint-dev/astroid#2195 was created, but not merged) One of the popular libraries where this doesn't work is opencv-python, here is an example code:
import cv2
# Correct line
c = cv2.VideoCapture(0)
# Incorrect line
i = cv2.ThisDoesntExist(0)
Removing /usr/local/lib/python3.10/dist-packages/cv2/__init__.py resolves the issue
Configuration
No response
Command used
pylint demo.py
Pylint output
************* Module demo
demo.py:1:0: C0114: Missing module docstring (missing-module-docstring)
demo.py:4:4: E1101: Module 'cv2' has no 'VideoCapture' member (no-member)
demo.py:6:4: E1101: Module 'cv2' has no 'ThisDoesntExist' member (no-member)
Expected behavior
************* Module demo
demo.py:1:0: C0114: Missing module docstring (missing-module-docstring)
demo.py:6:4: E1101: Module 'cv2' has no 'ThisDoesntExist' member (no-member)
Pylint version
pylint 3.0.2
astroid 3.0.1
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
OS / Environment
Ubuntu 22.04
Additional dependencies
opencv-python==4.8.1.78
Another common library affected by this issue is alembic where op.py generates the functions and op.pyi is the stub for such functions.
My hack is to swap the order of .py and .pyi extensions at https://github.com/pylint-dev/astroid/blob/31ba1dbd8ae86b55330be861e37d6bc372d9a8a5/astroid/modutils.py#L47C42-L47C42
so that get_source_file() will return the .pyi file.
Prime for a retest once #9241 lands.