-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Improve pytest.warns() docs to clarify difference with catch_warnings() #9002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Per #9386 we should also expand the deprecation message to suggest use of stdlib |
I wonder if we should instead provide some kind of pytest convenience API for this case, though? I imagine something like |
FYI: We need to replace a few occurrences of |
…ngs`. The use of `pytest.warns(None)` is deprecated in pytest 7.0.0. The alternative suggested by the deprecation warning, `pytest.warns()`, does not match the old behavior. In fact, `pytest.warns()` is intended specifically to raise an error if the code in the context does not raise the given warning, and in pytest 7.0.0rc1, the default warning used by `pytest.warns()` is the generic `Warning` class. This issue is discussed in the following pytest issues: * pytest-dev/pytest#9002 * pytest-dev/pytest#9386 There, the recommended alternative is to use `warnings.catch_warnings`. That is the change made here. Closes scipygh-15186
It would be very helpful if the best way to handle runtime-dependent warnings is documented. In a nutshell, we have parameterized tests that warn under some parameterizations and do not warn under others. We have been using something like expected_warn = FutureWarning if param == "n" else None
with pytest.warns(expected_warn):
... This worked in the sense that it would ensure that FutureWarning was raised when it should even if it silenced any warning otherwise, and so was not strict. What is the best way to handle a situation like this? I had thought of expected_warn = [FutureWarning] if param == "n" else []
with pytest.warns(*expected_warn):
... but it seems a bit unnatural, and I'm not even sure this would work given this thread and others. |
Please see #9404. |
…ngs`. The use of `pytest.warns(None)` is deprecated in pytest 7.0.0. The alternative suggested by the deprecation warning, `pytest.warns()`, does not match the old behavior. In fact, `pytest.warns()` is intended specifically to raise an error if the code in the context does not raise the given warning, and in pytest 7.0.0rc1, the default warning used by `pytest.warns()` is the generic `Warning` class. This issue is discussed in the following pytest issues: * pytest-dev/pytest#9002 * pytest-dev/pytest#9386 There, the recommended alternative is to use `warnings.catch_warnings`. That is the change made here. Closes scipygh-15186
Fixed by #9495 |
We should add some stdlib links to the relevant docs:
warnings.catch_warnings()
with catch_warnings(): simplefilter("error"); ...
I didn't intend to break anything, but nor did I consider that people might be using
pytest.warns()
likewarnings.catch_warnings()
. My apologies for the inconvenience!Originally posted by @Zac-HD in #8677 (comment)
The text was updated successfully, but these errors were encountered: