跳到内容

pytest-fixture-incorrect-parentheses-style (PT001)

源自 flake8-pytest-style linter。

修复总是可用的。

作用

根据 lint.flake8-pytest-style.fixture-parentheses 设置,检查带或不带括号的无参数 @pytest.fixture() 装饰器。

为什么这不好?

如果 @pytest.fixture() 不接受任何参数,则括号是可选的。

删除那些不必要的括号要求所有 fixture 都带括号都可以,但最好保持一致。该规则默认为删除不必要的括号,以匹配官方 pytest 项目的文档。

示例

import pytest


@pytest.fixture()
def my_fixture(): ...

建议改为

import pytest


@pytest.fixture
def my_fixture(): ...

修复安全性

如果 pytest.fixture 装饰器中有注释,则此规则的修复被标记为不安全。

例如,在以下情况下,修复将被标记为不安全

import pytest


@pytest.fixture(
    # comment
    # scope = "module"
)
def my_fixture(): ...

Options (选项)

参考