question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Context manager to ensure no warnings are issued

See original GitHub issue

Follow up to https://github.com/pytest-dev/pytest/issues/9386, starting this thread to discuss how to support the use case of “inside the given with block, no warning should be generated”.

Seems most users assumed this idiom:

with pytest.warns(None):
    ...

Would ensure that the code inside the with block would not issue any warning, raising an error if it did.

However that was never the case, and pytest.warns(None) would do nothing (warning or no warning generated). To warn users about this common mistake, we added a deprecation warning when None was passed to pytest.warns (https://github.com/pytest-dev/pytest/pull/8677).

When 7.0.0rc1 was released, some issues were reported about this: https://github.com/pytest-dev/pytest/issues/9402, #9386.

Users would like a way to ensure a block of code does not raise any warning, something which we never really supported.

Our current suggestion is to use warnings.catch_warnings(), followed by simplefilter("error"):

with warnings.catch_warnings():
    warnings.simplefilter("error")
    ...

However as mentioned in https://github.com/pytest-dev/pytest/issues/9402, this does not apply directly if you have a dynamic check:

warn_typ = FutureWarning if input == "nc" else None
with pytest.warns(warn_typ):
    ...

So far we have the following proposals:

  1. Keep things as is, improving the docs and suggesting catch_warnings (https://github.com/pytest-dev/pytest/issues/9002).
  2. Introduce a new context manager, pytest.does_not_warn() (https://github.com/pytest-dev/pytest/issues/9386#issuecomment-988800080).
  3. Change pytest.warns(None) to mean that no warning should be raised (https://github.com/pytest-dev/pytest/issues/9402#issuecomment-990777522).

I think this is worth reaching a consensus before the 7.0.0 release is out.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:42 (27 by maintainers)

github_iconTop GitHub Comments

3reactions
olgarithmscommented, Dec 17, 2021

Happy to take this!

2reactions
olgarithmscommented, Jan 4, 2022

Since this is sparking some debate, I agree it is preferable to delay the API. I am going to add more detail in the deprecation message as suggested then instead!

Read more comments on GitHub >

github_iconTop Results From Across the Web

warnings — Warning control — Python 3.11.1 documentation
While within the context manager all warnings will simply be ignored. This allows you to use known-deprecated code without having to see the...
Read more >
python - How to suppress a third-party warning using ...
Easiest way would be as the warnings module suggests here: with warnings.catch_warnings(): warnings.simplefilter("ignore") import paramiko.
Read more >
Simple Guide to Handle Warning Messages in Python
Inside the context manager, we first clear all filters using resetwarnings() method, add the filter to ignore deprecation warnings occurring ...
Read more >
26.4. warnings - Jython v2.5.2 documentation
Python programmers issue warnings by calling the warn() function defined in this ... to suppress the warning using the catch_warnings context manager:.
Read more >
Context Managers - Fabric documentation
However, this implementation may change in the future, so we do not recommend manually altering env.cwd – only the behavior of cd will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found