【问题标题】:How pylint-pytest throws F6401 Can-enumerate-pytest-fixturespylint-pytest 如何抛出 F6401 Can-enumerate-pytest-fixtures
【发布时间】:2021-09-07 17:15:30
【问题描述】:

你能解释一下我运行 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) 时提示'F6401'是什么原因吗?

我想知道它是如何工作的,或者它为什么会出现,并且有时会有不同的输出。相同的代码,有时两个,有时更多。我很沮丧。

我确实运行了pytest --fixtures --collect-only,没有任何异常提示,并且我的测试正常。

说明:

在我微调现有代码(包括运行 pylintpytestisort)后,一切正常。我添加了一个新包executor,包含三个模块,一个是base.py的抽象模块,两个分别对应不同的实现模块(local.pydocker.py)。

然后我运行isortpylint 工作正常

然后我在模块的__init__.py文件中导入基类和两个实现类,并添加一个工厂方法。

当我再次运行pylint 时,输入告诉我某些测试模块与F6401 有问题。

再次强调,在我添加此模块之前,一切都很好。但是现在我只是添加了这个模块的源代码,就会出现这个异常。

让我更加困惑的是,提示我的模块不包含任何固定装置。我又跑了pylint,发现F6401的测试模块多了(比上次多了好几倍)。

我一直在为一个新项目使用 PyLint 来检查逐个模块的迁移,当我迁移到这个模块时,我无法继续。

操作系统环境

  • python 3.7
  • 操作系统:Deepin(基本 Debian)
  • IDE:Pycharm

软件包版本

  • 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.

【问题讨论】:

    标签: python pytest pylint


    【解决方案1】:

    调试源代码后发现问题的原因是pylint-pytest在运行pytest从源代码中收集夹具时出错,然后pylint-pytest将错误传递给PyLint。

    我的源代码有一个类型注释错误,导致 pytest 从该模块中查找错误的夹具,并将错误传递给 pylint。但是我不清楚为什么会有不同的输出。

    通过调试源码,我们知道pylint-pytest向pylint注册了自己,当pylint检查所有文件时,会将文件传递给pylint-pytest的FixtureChecker

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

    FixtureChecker中的visit_module方法将文件传递给pytest,运行pytest <module_file> --fixtures --collect-only,同时将FixtureCollector插件加载到pytest中。

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

    pytest_collectreport中,如果pytest报错,会被记录下来,并将错误信息传递给pytest。

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

    我认为这个逻辑没有道理。 Pytest 应该只从测试模块中收集夹具,而不是从所有模块中收集夹具,Pylint-Pytest 应该在 PyLint 检查时过滤掉源代码。

    至此,我的疑虑烟消云散。谢谢。

    【讨论】:

    • 看起来你已经准备好在 pylint-pytest 中提出拉取请求了 :)
    猜你喜欢
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2021-11-30
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多