Scikit-learn: warnings.filterwarnings -- please don't do this. it's bad behavior.

Created on 10 Aug 2018  路  3Comments  路  Source: scikit-learn/scikit-learn

# Make sure that DeprecationWarning within this package always gets printed
warnings.filterwarnings('always', category=DeprecationWarning,
module=r'^{0}\.'.format(re.escape(__name__)))

via __init__.py

This is really quite bad behavior. If you feel like you really must do this, please use append=True

Most helpful comment

Could you please elaborate on why sklearn overrides a customized warning configuration?

If you add append=True, then you wouldn't be overriding a warning configuration that's not been customized.

The problem is that my applications global state should not be affected by importing your package. I also wouldn't want you doing things like changing my logging configuration.

All 3 comments

Could you please elaborate in what way this is a problem for your application? From the comments, it sound like it should only affect DeprecationWarnings within scikit-learn.

(Also see related discussion in https://github.com/scikit-learn/scikit-learn/pull/6930)

Could you please elaborate on why sklearn overrides a customized warning configuration?

If you add append=True, then you wouldn't be overriding a warning configuration that's not been customized.

The problem is that my applications global state should not be affected by importing your package. I also wouldn't want you doing things like changing my logging configuration.

If you add append=True, then you wouldn't be overriding a warning configuration that's not been customized.

Tried, that, by installing sklearn from sources, and manually raising a deprecation in sklearn.utils.__init__.py. With the default configuration the warning it raised as expected when calling python -c "import sklearn.utils". If we add append=True to warnings.filterwarnings used in sklearn/__init__.py the DeprecationWarning is no longer raised. I have not investigated why.

I agree the current situation is bad, I was just curious to know what kind of practical issues you run into with this setup (beyond the fact that it's bad on principle). Having a good understanding of the use cases, would also help us to come up with the right solution. This problem was discussed in https://github.com/scikit-learn/scikit-learn/issues/9857 (see in particular https://github.com/scikit-learn/scikit-learn/issues/9857#issuecomment-333351913 and https://github.com/scikit-learn/scikit-learn/issues/9857#issuecomment-333359072) which outlines the reason for the current situations.

Closing this as a duplicate of https://github.com/scikit-learn/scikit-learn/issues/9857 (to avoid fragmenting the same discussion over multiple issues). You are very welcome to comment or propose solutions there though.

Was this page helpful?
0 / 5 - 0 ratings