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
Should not call warnings.filterwarnings
in a library
#9857
Comments
This issue leads to other confusion, e.g. #7963 |
You're right that we may be being bad citizens. We've forcibly shown the DeprecationWarnings for a long time now. And I can't see an easy way to determine whether the user has tried to override default settings. The presence of DeprecationWarning twice in warnings.filters? One way to change this would be to make a custom deprecation warning class that does not inherit from DeprecationWarning. There are questions of backwards compatibility there... |
@mehaase I think that is just an issue of principles vs real world and I am afraid that the latter tends to win in the long run. If there is a way to be a better citizen, given the constraints mentioned by @jnothman (look at #7963 and probably the associated PR #7995 for more details), then a PR is more than welcome. If not, for the few cases where this is annoying, I would just recommend to comment out the line in For the record, numpy modifies warnings.filters as does ipython. |
And while I am at it, let me mention this: we may be able to use the In @mehaase case, the warnings would point at where he imports sklearn.cross_validation, rather than sklearn/cross_validation.py which he can not do anything about. Maybe that is what #7995 was partially about, I don't remember all the details. |
It sounds like it's an issue with python, if libraries are able to (and are
forced to to set defaults) override some user discussed filters. Is there a
good way for library developers to do this?
|
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 Like many others have remarked before (i.e. #11792), this is very bad behavior. |
@basnijholt suggestions for solutions are welcome. As a workaround for your case, can't you Maybe we should just make the backward-incompatible change to a custom warning. I'm quite sure it'll annoy way more users than the current situation does, though. |
We should probably also figure out why "append" doesn't work: |
The default warning filters are:
which will filter out DeprecationWarnings not in |
We could check if the user wanted to error and then leave it at that to fix a small part of the issue. |
If I'm using The only way to avoid the deprecation warnings from sklearn is to filter them out after As previously mentioned we can make the issue go away by appending an 'always' filter and use a NewDepWarning class (that doesn't inherit from DeprecationWarning). class NewDepWarning(Warning):
pass
warnings.simplefilter('always', NewDepWarning, append=True) This way users can place This is backward incompatible in the sense that users who already had their So we will make them unhappy for a little while. But I think it's a necessary evil that's worth it for the long term. People are already unhappy right now anyway. |
If we add a new class, we don't need to add a filter, right? Unless we inherit from DeprecationWarning? That might make it unnecessarily tricky though. |
Oh you're right we don't need the filter. I'm not sure we can do what we want and inherit from DeprecationWarning though. |
I don't think we do. |
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.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.
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 wheresklearn.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
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:sklearn
is not special enough to break with standard Python conventions.The text was updated successfully, but these errors were encountered: