How pylint-pytest throws F6401 Can-enumerate-pytest-fixtures

Viewed 515

Can you explain the prompt 'F6401' when I run pylint pylint-pytest plugin cannot enumerate and collect pytest fixtures. Please run `pytest --fixtures --collect-only path/to/current/module.py` and resolve any potential syntax error or package dependency issues (Can-enumerate-pytest-fixtures) is the reason?

I would like to know how it works, or why it appears, and sometimes has different outputs. The same code, sometimes two, sometimes more. I was depressed.

I did run pytest --fixtures --collect-only without any unusual hints and my tests were normal.

Description:

After I fine-tune my existing code, including running pylint, pytest, and isort, everything works. I added a new package executor with three modules, one is the abstract module of base.py, two are corresponding to different implementation modules(local.py, docker.py).

Then I run isort, and pylint works fine

Then I import the base class and two implementation classes in the module's __init__.py file, and add a factory method.

When I run pylint again, the input tells me that some of the test modules have problems with F6401.

Again, I want to emphasize that everything was fine until I added this module. But now I just added the source code of this module, this exception will appear.

What makes it even more confusing to me is that the module I'm prompted doesn't include any fixtures. I ran pylint again and found that F6401 has more test modules (several times more than last time).

I've been using PyLint for a new project to check for a mode-by-module migration, and when I migrate to this module, I can't continue.

OS env

  • python 3.7
  • os: Deepin(base Debian)
  • IDE: Pycharm

Package versions

  • pylint 3.0.0a3
  • pylint-pytest 1.1.2
  • pyparsing 2.4.7
  • pytest 6.2.3
  • pytest-asyncio 0.14.0
  • pytest-cov 2.11.1
  • pytest-mock 3.5.1

ISSUE about this question.

1 Answers

After debugging the source code, I found out that the cause of my problems was an error in pylint-pytest when running pytest to collect fixtures from source code, and then pylint-pytest passed the error to PyLint.

My source code had a type annotation error that caused pytest to look for a fixture from that module that was wrong, and the error was passed to pylint. But why there is a different output is not clear to me.

From debugging the source code, we know that pylint-pytest registers itself with pylint, and when pylint checks all files, it passes the files to pylint-pytest's FixtureChecker.

https://github.com/reverbc/pylint-pytest/blob/62676386f80989cc0373d77bc5dc74acc635fd7a/pylint_pytest/checkers/fixture.py#L92-L142

The visit_module method in the FixtureChecker passes the file to pytest, running pytest <module_file> --fixtures --collect-only, At the same time load the FixtureCollector plug-in into pytest.

https://github.com/reverbc/pylint-pytest/blob/62676386f80989cc0373d77bc5dc74acc635fd7a/pylint_pytest/checkers/fixture.py#L125-L131

In pytest_collectreport , if an error is reported by pytest, it is logged and the error information is passed to pytest.

https://github.com/reverbc/pylint-pytest/blob/62676386f80989cc0373d77bc5dc74acc635fd7a/pylint_pytest/checkers/fixture.py#L24-L34

I don't think this logic makes sense. Pytest should only collect fixtures from the test modules, and instead of collecting fixtures from all modules, Pylint-Pytest should filter out the source code when PyLint checks.

At this point, my doubts have disappeared. Thanks.

Related