Add docs on pytest.warns(None) deprecation

This commit is contained in:
Olga Matoula 2022-01-09 22:09:30 +00:00
parent 47df71d23f
commit aeae183c75
4 changed files with 34 additions and 6 deletions

1
changelog/9404.doc.rst Normal file
View File

@ -0,0 +1 @@
Added extra documentation on alternatives to common misuses of `pytest.warns(None)` ahead of its deprecation.

View File

@ -221,11 +221,32 @@ Using ``pytest.warns(None)``
.. deprecated:: 7.0 .. deprecated:: 7.0
:func:`pytest.warns(None) <pytest.warns>` is now deprecated because many people used :func:`pytest.warns(None) <pytest.warns>` is now deprecated because it was frequently misused.
it to mean "this code does not emit warnings", but it actually had the effect of Its correct usage was checking that the code emits at least one warning of any type - like ``pytest.warns()``
checking that the code emits at least one warning of any type - like ``pytest.warns()``
or ``pytest.warns(Warning)``. or ``pytest.warns(Warning)``.
If you are looking to:
- ensure that **no** warnings are emitted, consider using
.. code-block:: python
with warnings.catch_warnings():
warnings.simplefilter("error")
- suppress warnings, you could use
.. code-block:: python
with warnings.catch_warnings():
warnings.simplefilter("ignore")
- ensure that **any** warning is emitted, please use
.. code-block:: python
with pytest.warns():
pass
The ``--strict`` command-line option The ``--strict`` command-line option
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -89,7 +89,10 @@ NODE_CTOR_FSPATH_ARG = UnformattedWarning(
WARNS_NONE_ARG = PytestRemovedIn8Warning( WARNS_NONE_ARG = PytestRemovedIn8Warning(
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n" "Passing None to catch any warning has been deprecated, pass no arguments instead:\n"
" Replace pytest.warns(None) by simply pytest.warns()." "Replace pytest.warns(None) by simply pytest.warns().\n"
"See https://docs.pytest.org/en/latest/deprecations.html"
"#using-pytest-warns-none"
" on asserting no warnings were emitted."
) )
KEYWORD_MSG_ARG = UnformattedWarning( KEYWORD_MSG_ARG = UnformattedWarning(

View File

@ -138,8 +138,11 @@ def test_warns_none_is_deprecated():
with pytest.warns( with pytest.warns(
PytestDeprecationWarning, PytestDeprecationWarning,
match=re.escape( match=re.escape(
"Passing None to catch any warning has been deprecated, pass no arguments instead:\n " "Passing None to catch any warning has been deprecated, pass no arguments instead:\n"
"Replace pytest.warns(None) by simply pytest.warns()." "Replace pytest.warns(None) by simply pytest.warns().\n"
"See https://docs.pytest.org/en/latest/deprecations.html"
"#using-pytest-warns-none"
" on asserting no warnings were emitted."
), ),
): ):
with pytest.warns(None): # type: ignore[call-overload] with pytest.warns(None): # type: ignore[call-overload]