pylint
pylint copied to clipboard
`import-error` inconsistency between `except ImportError` and `with contextlib.suppress(ImportError)`
Bug description
There's an inconsistency with import-error between except ImportError and with contextlib.suppress(ImportError).
With the former Pylint doesn't complain when it can't find the module/package, but with the latter it does complain.
This can easily be reproduced with the following code:
# pylint: disable=missing-module-docstring
import contextlib
try:
import bacon # pylint: disable=unused-import
except ImportError:
pass
with contextlib.suppress(ImportError):
import eggs # pylint: disable=unused-import
Configuration
No response
Command used
pylint toast.py
Pylint output
************* Module toast
toast.py:10:4: E0401: Unable to import 'eggs' (import-error)
Expected behavior
It's consistent.
Pylint version
pylint 2.15.0-a0
astroid 2.12.2
Python 3.10.5 (main, Jul 27 2022, 01:21:11) [GCC 10.2.1 20210130 (Red Hat 10.2.1-11)]
OS / Environment
No response
Additional dependencies
No response
Thanks for the report. The node_ignores_exception utility only checks exception handlers. It should check the contextlib.suppress context manager also. This will end up affecting several additional messages.