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.

pytest warnings due to deprecation of pytest.warns(None)

See original GitHub issue

Pytest issues warnings about using pytest.warns(None) context manager, where the None part is now being deprecated.

For example, >>> pytest ./sklearn/tests/test_naive_bayes.py results in 79 passed, 4 warnings. After commenting out --disable-pytest-warnings key in [tool:pytest] section of setup.cfg, I could see that some of the warnings are:

PytestRemovedIn8Warning: Passing None has been deprecated. See https://docs.pytest.org/en/latest/how-to/capture-warnings.html#additional-use-cases-of-warnings-in-tests for alternatives in common use cases.

Apparently, pytest.warns(None) is deprecated for being widely misunderstood. I think that while sklearn’s tests do not suffer from this misunderstanding, they might be affected in the future if and when pytest raises an error instead of a deprecation warning. (I couldn’t figure out if this is planned for pytest’s future releases, but in any case, avoiding the deprecation and reducing the warnings count won’t harm.)

I found this message , which is relevant to how pytest.warns(None) is used in scikit-learn tests, and how this could be changed. I opened this issue to share this information and quote the message, as it may save someone time in the future.

The tests were of the form:

with pytest.warns(None) as record:
    <do some stuff>
    <check record for warnings, and possibly raise an error>

The tests were not assuming that the pytest context code would raise an error if a warning occurred. Instead, these tests were doing their own error checking based on record. So they were really using pytest.warns(None) exactly like warnings.catch_warnings(record=True). (There is a pull request to make the change to catch_warnings.)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:35 (35 by maintainers)

github_iconTop GitHub Comments

1reaction
cmarmocommented, Apr 7, 2022

Reopening as some tests still need to be modified.

1reaction
danifernandes-hubcommented, Mar 31, 2022

@adamgonzo I’m working on the utils folder. However, feel free to work on that file

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to capture warnings — pytest documentation
By default pytest will display DeprecationWarning and PendingDeprecationWarning warnings from user code and third-party libraries, as recommended by PEP 565.
Read more >
How to suppress py.test internal deprecation warnings
Disabling all warnings persistently (i.e. using pytest.ini) is almost never a good idea. Disabling just Deprecation warning (and by module) as ...
Read more >
Suppress Warnings in pytest | SukhbinderSingh.com
Recently had this issue with pytest. Were running 100+ tests and some of it was throwing some warnings and the side effect of...
Read more >
Source code for astropy.tests.helper
It makes use of the `pytest`_ testing framework. ... but not within Astropy, where we use our own deprecation warning class) into exceptions...
Read more >
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 >

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