跳到内容

legacy-form-pytest-raises (RUF061)

有时提供修复。

此规则不稳定且处于预览状态。使用需要 --preview 标志。

作用

检查 pytest.raisespytest.warnspytest.deprecated_call 的非上下文管理器用法。

为什么这不好?

上下文管理器形式更易读,更易扩展,并支持额外的 kwargs。

示例

import pytest


excinfo = pytest.raises(ValueError, int, "hello")
pytest.warns(UserWarning, my_function, arg)
pytest.deprecated_call(my_deprecated_function, arg1, arg2)

建议改为

import pytest


with pytest.raises(ValueError) as excinfo:
    int("hello")
with pytest.warns(UserWarning):
    my_function(arg)
with pytest.deprecated_call():
    my_deprecated_function(arg1, arg2)

参考