[prerelease] pytest.warns() doesn't seem to work properly for coverage.py
From @nedbat via Twitter:
This message could be better, because removing None doesn’t work now:
PytestRemovedIn8Warning: Passing None to catch any warning has been deprecated, pass no arguments instead: Replace pytest.warns(None) by simply pytest.warns().When I remove None and run with pytest 7, I get failures:
No idea off-hand what's going on there, as that seems exactly what we did in #8677 too.
cc @Zac-HD @olgarithms
Ah, this is an intentional change to avoid the case where with pytest.warns() was entirely vacuous and would pass whether any or no warnings were emitted. We have #9002 open to document this properly (and I've added it to the 7.0 milestone); in the meantime it looks like @nedbat actually wants:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
Ah, makes sense! I guess in @nedbat's scenario indeed the warning is "optional". So I suppose we can close this one as a duplicate then?
I wanted to assert that no warnings were raised. Why would I add an ignore filter to do that? Maybe I'm not understanding...
warnings.simplefilter("error") then.
(I assumed based on the "so we don't see..." comment, thinking that was library rather than test code, and forgetting to wonder why pytest would be there then.)
It seems a shame to have lost a compact notation for "I assert this won't have any warnings"...
It seems a shame to have lost a compact notation for "I assert this won't have any warnings"...
We never actually had that though! Before, it would pass for any or no warnings, which would be useless if it wasn't also misleading.
I agree that a concise notation for this would be good, and propose a new context manager with pytest.does_not_warn():
I agree that a concise notation for this would be good, and propose a new context manager
with pytest.does_not_warn():
I'd happily give a go at this over the holidays!
We never actually had that though! Before, it would pass for any or no warnings, which would be useless if it wasn't also misleading.
I see! I was definitely misunderstanding it then. If a new context manager isn't available in 7.0, I can write one of my own for my test suite. Thanks.
@olgarithms thanks for the interest in contributing!
However before tackling this, I suggest opening a new issue to discuss it.
Please see https://github.com/pytest-dev/pytest/issues/9404.
