python runtime_error ai_generated true

DeprecationWarning: 'pytest.warns(None)' 已弃用

DeprecationWarning: 'pytest.warns(None)' is deprecated

ID: python/pytest-deprecation-warning-as-error

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-02-14首次发现

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

使用 `pytest.warns(None)` 断言无警告自 pytest 7 起已弃用;它现在会引发警告,可能被转换为错误。

English

Using `pytest.warns(None)` to assert no warnings is deprecated since pytest 7; it now raises a warning that may be turned into an error.

generic

解决方案

  1. 90% 成功率
    import warnings
    with warnings.catch_warnings():
        warnings.simplefilter('error')
        my_function()
  2. 85% 成功率
    with pytest.warns(UserWarning):
        my_function()

无效尝试

常见但无效的做法:

  1. 70% 失败

    Hides the deprecation but does not fix the test logic; may break in future pytest versions.

  2. 60% 失败

    Does not integrate with pytest's warning capture and may miss warnings from other threads.