pylint icon indicating copy to clipboard operation
pylint copied to clipboard

cv2.error recognised as exception on Linux, not macOS

Open adamtheturtle opened this issue 6 months ago • 3 comments

Bug description

pylint gives a E0712 catching-non-exception error on macOS Sonoma, but not on Linux.

Create the following file example.py:

"""Example file to show pylint error"""

import cv2

try:
    pass
except cv2.error:
    pass

And the configuration from the section below to avoid no-member errors https://github.com/opencv/opencv-python/issues/570.

I first saw this on GitHub Actions, and I can reproduce no error by running:

$ docker run -it -v .:/code python bash
root# pip install opencv-python pylint
root# cd /code
root# pylint example.py

The difference is a problem because I either get useless-suppression / I0021 or catching-non-exception.

Configuration

pyproject.toml:

[tool.pylint]

    [tool.pylint.'MASTER']

    extension-pkg-whitelist = [
        'cv2',
    ]

    [tool.pylint.typecheck]
    generated-members = ["cv2.*"]

Command used

pylint example.py

Pylint output

macOS:

************* Module example
example.py:7:7: E0712: Catching an exception which doesn't inherit from Exception: cv2.error (catching-non-exception)

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

Linux:

------------------------------------
Your code has been rated at 10.00/10

Expected behavior

No error on either platform. Error on both platforms is preferable to the current state.

Pylint version

macOS:


pylint 3.0.3
astroid 3.0.2
Python 3.12.1 (main, Dec  7 2023, 20:45:44) [Clang 15.0.0 (clang-1500.0.40.1)]

Linux:

pylint 3.0.3
astroid 3.0.2
Python 3.12.1 (main, Dec 19 2023, 16:44:02) [GCC 12.2.0]

OS / Environment

macOS: Apple M2, Sonoma 14.2 Linux: Debian GNU/Linux 12 (bookworm)

Additional dependencies

opencv-python==4.8.1.78

adamtheturtle avatar Dec 28 '23 23:12 adamtheturtle

Hey thanks for reporting :wave:

This one would need some investigation, as there's a symphony of things going on in cv2 to generate cv2.error. 📯

jacobtylerwalls avatar Dec 30 '23 03:12 jacobtylerwalls

The reason for the failure is that on MacOS the cv2/Error/__init__.pyi stub shadows class error(Exception): stub in cv2/__init__.pyi. Could be related to the file system on MacOS being case insensitive. pylint gets confused that cv2.error is a cv2::Error enum and rightfully flags a problem.

neykov avatar Mar 25 '24 19:03 neykov

Here's the relevant code in astroid that is responsible for the quirk:

                file_path = os.path.join(package_directory, package_file_name)
                if os.path.isfile(file_path):

MacOS will happily match cv2/Error/__init__.py.

neykov avatar Mar 27 '24 13:03 neykov