Skip to content

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

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

Closed
The-Compiler opened this issue Dec 7, 2021 · 10 comments
Closed
Labels
plugin: warnings related to the warnings builtin plugin
Milestone

Comments

@The-Compiler
Copy link
Member

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 The-Compiler added the plugin: warnings related to the warnings builtin plugin label Dec 7, 2021
@The-Compiler The-Compiler added this to the 7.0 milestone Dec 7, 2021
@Zac-HD
Copy link
Member

Zac-HD commented Dec 7, 2021

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

@The-Compiler
Copy link
Member Author

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?

@Zac-HD Zac-HD closed this as completed Dec 7, 2021
@nedbat
Copy link
Contributor

nedbat commented Dec 8, 2021

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

@Zac-HD
Copy link
Member

Zac-HD commented Dec 8, 2021

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

@nedbat
Copy link
Contributor

nedbat commented Dec 8, 2021

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

@Zac-HD
Copy link
Member

Zac-HD commented Dec 8, 2021

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

@olgarithms
Copy link
Contributor

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!

@nedbat
Copy link
Contributor

nedbat commented Dec 8, 2021

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.

@nicoddemus
Copy link
Member

@olgarithms thanks for the interest in contributing!

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

WarrenWeckesser added a commit to WarrenWeckesser/scipy that referenced this issue Dec 9, 2021
…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
@nicoddemus
Copy link
Member

Please see #9404.

tylerjereddy pushed a commit to tylerjereddy/scipy that referenced this issue Dec 19, 2021
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: warnings related to the warnings builtin plugin
Projects
None yet
Development

No branches or pull requests

5 participants