python runtime_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 60% fail

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