Skip to content

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

@patrickdepinguin

Description

@patrickdepinguin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🪲Needs astroid Brain 🧠Needs a brain tip in astroid (then an astroid upgrade)Needs astroid updateNeeds an astroid update (probably a release too) before being mergable

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions