跳到内容

pytest-incorrect-mark-parentheses-style (PT023)

源自 flake8-pytest-style linter。

修复总是可用的。

作用

检查带或不带括号的无参数 @pytest.mark.<marker>() 装饰器,具体取决于lint.flake8-pytest-style.mark-parentheses 设置。

默认情况下,此规则会删除不必要的括号,以匹配官方 pytest 项目的文档。

为什么这不好?

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

删除这些不必要的括号要求所有 fixture 都使用括号都可以,但最好保持一致。

示例

import pytest


@pytest.mark.foo()
def test_something(): ...

建议改为

import pytest


@pytest.mark.foo
def test_something(): ...

修复安全性

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

import pytest


@pytest.mark.foo(
    # comment
)
def test_something(): ...

Options (选项)

参考