diff --git a/sklearn/gaussian_process/tests/test_gaussian_process.py b/sklearn/gaussian_process/tests/test_gaussian_process.py index 6d6fa3ab81193..2cacd217cc986 100644 --- a/sklearn/gaussian_process/tests/test_gaussian_process.py +++ b/sklearn/gaussian_process/tests/test_gaussian_process.py @@ -15,6 +15,7 @@ from sklearn.datasets import make_regression from sklearn.utils.testing import assert_greater, assert_true, assert_raises +pytestmark = pytest.mark.filterwarnings('ignore', category=DeprecationWarning) f = lambda x: x * np.sin(x) X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T diff --git a/sklearn/tests/test_common.py b/sklearn/tests/test_common.py index 62a3bee5fc12d..1f4c41ec8285b 100644 --- a/sklearn/tests/test_common.py +++ b/sklearn/tests/test_common.py @@ -22,6 +22,7 @@ from sklearn.utils.testing import assert_greater from sklearn.utils.testing import assert_in from sklearn.utils.testing import ignore_warnings +from sklearn.exceptions import ConvergenceWarning import sklearn from sklearn.cluster.bicluster import BiclusterMixin @@ -91,18 +92,22 @@ def _rename_partial(val): ) def test_non_meta_estimators(name, Estimator, check): # Common tests for non-meta estimators - estimator = Estimator() - set_checking_parameters(estimator) - check(name, estimator) + with ignore_warnings(category=(DeprecationWarning, ConvergenceWarning, + UserWarning, FutureWarning)): + estimator = Estimator() + set_checking_parameters(estimator) + check(name, estimator) @pytest.mark.parametrize("name, Estimator", _tested_non_meta_estimators()) def test_no_attributes_set_in_init(name, Estimator): # input validation etc for non-meta estimators - estimator = Estimator() - # check this on class - check_no_attributes_set_in_init(name, estimator) + with ignore_warnings(category=(DeprecationWarning, ConvergenceWarning, + UserWarning, FutureWarning)): + estimator = Estimator() + # check this on class + check_no_attributes_set_in_init(name, estimator) def test_configure(): diff --git a/sklearn/utils/estimator_checks.py b/sklearn/utils/estimator_checks.py index 4021ede057369..2125067c502bf 100644 --- a/sklearn/utils/estimator_checks.py +++ b/sklearn/utils/estimator_checks.py @@ -301,10 +301,10 @@ def check_estimator(Estimator): for check in _yield_all_checks(name, estimator): try: check(name, estimator) - except SkipTest as message: + except SkipTest as exception: # the only SkipTest thrown currently results from not # being able to import pandas. - warnings.warn(message, SkipTestWarning) + warnings.warn(str(exception), SkipTestWarning) def _boston_subset(n_samples=200): @@ -327,7 +327,6 @@ def set_checking_parameters(estimator): and not isinstance(estimator, BaseSGD)): estimator.set_params(n_iter=5) if "max_iter" in params: - warnings.simplefilter("ignore", ConvergenceWarning) if estimator.max_iter is not None: estimator.set_params(max_iter=min(5, estimator.max_iter)) # LinearSVR, LinearSVC diff --git a/sklearn/utils/testing.py b/sklearn/utils/testing.py index bb67443f97fc8..7b42bb801f095 100644 --- a/sklearn/utils/testing.py +++ b/sklearn/utils/testing.py @@ -137,7 +137,6 @@ def assert_warns(warning_class, func, *args, **kw): result : the return value of `func` """ - # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. @@ -321,7 +320,6 @@ def __call__(self, fn): """Decorator to catch and hide warnings without visual nesting.""" @wraps(fn) def wrapper(*args, **kwargs): - # very important to avoid uncontrolled state propagation clean_warning_registry() with warnings.catch_warnings(): warnings.simplefilter("ignore", self.category) @@ -339,14 +337,14 @@ def __repr__(self): return "%s(%s)" % (name, ", ".join(args)) def __enter__(self): - clean_warning_registry() # be safe and not propagate state + chaos - warnings.simplefilter("ignore", self.category) if self._entered: raise RuntimeError("Cannot enter %r twice" % self) self._entered = True self._filters = self._module.filters self._module.filters = self._filters[:] self._showwarning = self._module.showwarning + clean_warning_registry() + warnings.simplefilter("ignore", self.category) def __exit__(self, *exc_info): if not self._entered: @@ -354,7 +352,7 @@ def __exit__(self, *exc_info): self._module.filters = self._filters self._module.showwarning = self._showwarning self.log[:] = [] - clean_warning_registry() # be safe and not propagate state + chaos + clean_warning_registry() assert_less = _dummy.assertLess @@ -724,8 +722,13 @@ def run_test(*args, **kwargs): def clean_warning_registry(): - """Safe way to reset warnings.""" - warnings.resetwarnings() + """Clean Python warning registry for easier testing of warning messages. + + We may not need to do this any more when getting rid of Python 2, not + entirely sure. See https://bugs.python.org/issue4180 and + https://bugs.python.org/issue21724 for more details. + + """ reg = "__warningregistry__" for mod_name, mod in list(sys.modules.items()): if 'six.moves' in mod_name: diff --git a/sklearn/utils/tests/test_testing.py b/sklearn/utils/tests/test_testing.py index 6b55431d21d7d..eb9512f177ed3 100644 --- a/sklearn/utils/tests/test_testing.py +++ b/sklearn/utils/tests/test_testing.py @@ -211,26 +211,19 @@ def context_manager_no_user_multiple_warning(): assert_warns(DeprecationWarning, context_manager_no_user_multiple_warning) -# This class is inspired from numpy 1.7 with an alteration to check -# the reset warning filters after calls to assert_warns. -# This assert_warns behavior is specific to scikit-learn because -# `clean_warning_registry()` is called internally by assert_warns -# and clears all previous filters. class TestWarns(unittest.TestCase): def test_warn(self): def f(): warnings.warn("yo") return 3 - # Test that assert_warns is not impacted by externally set - # filters and is reset internally. - # This is because `clean_warning_registry()` is called internally by - # assert_warns and clears all previous filters. - warnings.simplefilter("ignore", UserWarning) - assert_equal(assert_warns(UserWarning, f), 3) - - # Test that the warning registry is empty after assert_warns - assert_equal(sys.modules['warnings'].filters, []) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", UserWarning) + filters_orig = warnings.filters[:] + assert_equal(assert_warns(UserWarning, f), 3) + # test that assert_warns doesn't have side effects on warnings + # filters + assert_equal(warnings.filters, filters_orig) assert_raises(AssertionError, assert_no_warnings, f) assert_equal(assert_no_warnings(lambda x: x, 1), 1)