pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.31k stars 1.13k forks source link

Errors from astroid brain hooks are shown if module name happens to coincide with a hook ('pytest') #7872

Open patrickdepinguin opened 1 year ago

patrickdepinguin commented 1 year ago

Bug description

Trying to use pylint on a file called 'pytest' gives errors, originating from the file astroid/brain/brain_pytest.py, even if the actual file-under-test is empty. Renaming the file-under-test from pytest to e.g. foobar removes the errors.

Configuration

No response

Command used

$ touch pytest                                                                                                                                                                                               
$ cat pytest                                                                                                                                                                                                 
$ pylint -v pytest

Pylint output

No config file found, using default configuration
************* 
<?>:16:4: W0622: Redefining built-in 'exit' (redefined-builtin)
************* 
<?>:4:4: W0611: Unused import _pytest.mark (unused-import)
************* 
<?>:5:4: W0611: Unused import _pytest.recwarn (unused-import)
************* 
<?>:6:4: W0611: Unused import _pytest.runner (unused-import)
************* 
<?>:7:4: W0611: Unused import _pytest.python (unused-import)
************* 
<?>:8:4: W0611: Unused import _pytest.skipping (unused-import)
************* 
<?>:9:4: W0611: Unused import _pytest.assertion (unused-import)
************* 
<?>:36:4: W0611: Unused import _pytest.freeze_support (unused-import)
************* 
<?>:40:8: W0611: Unused import _pytest.genscript (unused-import)
************* 
<?>:46:4: W0611: Unused import _pytest.debugging (unused-import)
************* 
<?>:50:8: W0611: Unused import _pytest.pdb (unused-import)
************* 
<?>:56:4: W0611: Unused import _pytest.fixtures (unused-import)

Expected behavior

Without knowing more about internals, my view is that:

  1. It is not correct that a hook for 'pytest' is included just because the module-to-be-linted is called pytest. There may or may not be a relation. In this case, the local pytest is intended to be a wrapper around calling pytest, but is not actually importing pytest (it will call it via subprocess).
  2. Even if it is warranted that real pytest imports are done via astroid, the user should not be presented with pylint errors from that module, because they only asked to lint the local module-under-test.

Pylint version

pylint 2.15.5
astroid 2.12.12
Python 3.10.8 (main, Nov 19 2022, 21:53:32) [GCC 11.3.0]

OS / Environment

Gentoo Linux

Additional dependencies

No response

Pierre-Sassoulas commented 1 year ago

Thank you for the report !

mbyrnepr2 commented 1 year ago

Similar situation to https://github.com/PyCQA/pylint/issues/7868. In that case it is a local module named numpy.py