pytest icon indicating copy to clipboard operation
pytest copied to clipboard

[prerelease] pytest.warns() doesn't seem to work properly for coverage.py

Open The-Compiler opened this issue 4 years ago • 10 comments

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:

image

No idea off-hand what's going on there, as that seems exactly what we did in #8677 too.

cc @Zac-HD @olgarithms

The-Compiler avatar Dec 07 '21 12:12 The-Compiler

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")

Zac-HD avatar Dec 07 '21 12:12 Zac-HD

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?

The-Compiler avatar Dec 07 '21 13:12 The-Compiler

I wanted to assert that no warnings were raised. Why would I add an ignore filter to do that? Maybe I'm not understanding...

nedbat avatar Dec 08 '21 02:12 nedbat

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.)

Zac-HD avatar Dec 08 '21 04:12 Zac-HD

It seems a shame to have lost a compact notation for "I assert this won't have any warnings"...

nedbat avatar Dec 08 '21 12:12 nedbat

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():

Zac-HD avatar Dec 08 '21 13:12 Zac-HD

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!

olgarithms avatar Dec 08 '21 13:12 olgarithms

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.

nedbat avatar Dec 08 '21 17:12 nedbat

@olgarithms thanks for the interest in contributing!

However before tackling this, I suggest opening a new issue to discuss it.

nicoddemus avatar Dec 09 '21 12:12 nicoddemus

Please see https://github.com/pytest-dev/pytest/issues/9404.

nicoddemus avatar Dec 10 '21 10:12 nicoddemus