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.

Should not call `warnings.filterwarnings` in a library

See original GitHub issue

Description

Importing sklearn changes the current warning filters, making it very difficult to debug warnings using standard Python syntax, e.g -W command flag.

Steps/Code to Reproduce

The module sklearn.cross_validation is deprecated, and importing it results in a warning.

$ python3.6 -c 'import mypackage'
/usr/local/lib/python3.6/dist-packages/sklearn/cross_validation.py:41: DeprecationWarning:
This module was deprecated in version 0.18 in favor of the model_selection module into
which all the refactored classes and functions are moved. Also note that the interface of
the new CV iterators are different from that of this module. This module will be removed
in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)

Okay, I’m surprised to see a DeprecationWarning — that’s not supposed to happen in Python unless I ask to see it — but let me try to get a stack trace so I can see where it’s coming from.

$ python3.6 -W error::DeprecationWarning:: -c 'import mypackage'
/usr/local/lib/python3.6/dist-packages/sklearn/cross_validation.py:41: DeprecationWarning:
This module was deprecated in version 0.18 in favor of the model_selection module into
which all the refactored classes and functions are moved. Also note that the interface of
the new CV iterators are different from that of this module. This module will be removed
in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)

Oh that’s weird, the -W error::DeprecationWarning:: should raise the warning an exception, which would show me a full stack trace and tell me where sklearn.cross_validation is being imported from. Why does the configuration flag have no effect?

I tried playing with the flags for a while, then eventually realized that it is not possible to change sklearn’s warning filter, because it is hardcoded in the module!

Expected Results

  1. At the very least, honor the user’s warning filters and don’t change the warning filter to something that the user didn’t ask for.
  2. Ideally, don’t configure warning filters at all. This behavior should be controlled by an application or an end user, not a library.

If the warnings.filterwarnings(…) is removed completely, then the result would be expected Python behavior and I would be able to debug this very quickly:

$ python3.6 -W error::DeprecationWarning:: -c 'import mypackage'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/mypackage/__init__.py", line 5, in <module>
    from .classifiers import (
  File "/usr/local/lib/python3.6/dist-packages/formasaurus/classifiers.py", line 8, in <module>
    from mypackage import formtype_model, mypackage
  File "/usr/local/lib/python3.6/dist-packages/mypackage/formtype_model.py", line 9, in <module>
    from mypackage.annotation import get_annotation_folds
  File "/usr/local/lib/python3.6/dist-packages/mypackage/annotation.py", line 5, in <module>
    from sklearn.cross_validation import LabelKFold
  File "/usr/local/lib/python3.6/dist-packages/sklearn/cross_validation.py", line 41, in <module>
    "This module will be removed in 0.20.", DeprecationWarning)
DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.

sklearn is not special enough to break with standard Python conventions.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
basnijholtcommented, Jul 29, 2019

In the Adaptive documentation we use Juypter-sphinx to execute code.

Whenever there is a warning, Jupyter-sphinx will raise an error. This result in failed builds.

Unfortunately, because of this issue here, filtering out the warnings (like done here) doesn’t work.

This issue is really quite annoying and means that we have to pin the old scikit-learn in order to suppress the warning.

Like many others have remarked before (i.e. https://github.com/scikit-learn/scikit-learn/issues/11792), this is very bad behavior.

0reactions
amuellercommented, Sep 20, 2019

I don’t think we do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

warnings — Warning control — Python 3.11.1 documentation
Rules can be added to the filter by calling filterwarnings() and reset to its default state by calling resetwarnings() . The printing of...
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 >
python warnings (beginner - intermediate) anthony explains ...
today I talk about the warnings module and the argument you *absolutely* need whenever you raise a warning (as well as how to...
Read more >
Working with warnings in Python (Or: When is an exception ...
How are warnings different from Python exceptions? Learn how to send and filter warnings, and why you would want to do so.
Read more >
warnings — Non-fatal Alerts — PyMOTW 3
Since warnings are not fatal, a program may encounter the same warn-able ... to the interpreter or by calling functions found in warnings...
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