8000 MAINT Don't use clean_warnings_registry in tests (#14085) · scikit-learn/scikit-learn@0eedf99 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0eedf99

Browse files
rthglemaitre
authored andcommitted
MAINT Don't use clean_warnings_registry in tests (#14085)
1 parent 6ab8c86 commit 0eedf99

File tree

6 files changed

+8
-28
lines changed

6 files changed

+8
-28
lines changed

sklearn/feature_selection/tests/test_chi2.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from sklearn.utils.testing import assert_raises
1515
from sklearn.utils.testing import assert_array_almost_equal
1616
from sklearn.utils.testing import assert_array_equal
17-
from sklearn.utils.testing import clean_warning_registry
1817

1918
# Feature 0 is highly informative for class 1;
2019
# feature 1 is the same everywhere;
@@ -72,7 +71,6 @@ def test_chi2_negative():
7271
def test_chi2_unused_feature():
7372
# Unused feature should evaluate to NaN
7473
# and should issue no runtime warning
75-
clean_warning_registry()
7674
with warnings.catch_warnings(record=True) as warned:
7775
warnings.simplefilter('always')
7876
chi, p = chi2([[1, 0], [0, 0]], [1, 0])

sklearn/metrics/tests/test_classification.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from sklearn.datasets import make_multilabel_classification
1414
from sklearn.preprocessing import label_binarize, LabelBinarizer
1515
from sklearn.utils.validation import check_random_state
16-
from sklearn.utils.testing import assert_raises, clean_warning_registry
16+
from sklearn.utils.testing import assert_raises
1717
from sklearn.utils.testing import assert_raise_message
1818
from sklearn.utils.testing import assert_equal
1919
from sklearn.utils.testing import assert_almost_equal
@@ -1598,7 +1598,6 @@ def test_prf_warnings():
15981598
'being set to 0.0 due to no true samples.')
15991599
my_assert(w, msg, f, [-1, -1], [1, 1], average='binary')
16001600

1601-
clean_warning_registry()
16021601
with warnings.catch_warnings(record=True) as record:
16031602
warnings.simplefilter('always')
16041603
precision_recall_fscore_support([0, 0], [0, 0], average="binary")
@@ -1615,7 +1614,6 @@ def test_recall_warnings():
16151614
np.array([[1, 1], [1, 1]]),
16161615
np.array([[0, 0], [0, 0]]),
16171616
average='micro')
1618-
clean_warning_registry()
16191617
with warnings.catch_warnings(record=True) as record:
16201618
warnings.simplefilter('always')
16211619
recall_score(np.array([[0, 0], [0, 0]]),
@@ -1631,7 +1629,6 @@ def test_recall_warnings():
16311629

16321630

16331631
def test_precision_warnings():
1634-
clean_warning_registry()
16351632
with warnings.catch_warnings(record=True) as record:
16361633
warnings.simplefilter('always')
16371634
precision_score(np.array([[1, 1], [1, 1]]),
@@ -1652,7 +1649,6 @@ def test_precision_warnings():
16521649

16531650

16541651
def test_fscore_warnings():
1655-
clean_warning_registry()
16561652
with warnings.catch_warnings(record=True) as record:
16571653
warnings.simplefilter('always')
16581654

sklearn/metrics/tests/test_ranking.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,6 @@ def test_auc_score_non_binary_class():
461461
assert_raise_message(ValueError, "multiclass format is not supported",
462462
roc_auc_score, y_true, y_pred)
463463

464-
clean_warning_registry()
465464
with warnings.catch_warnings(record=True):
466465
rng = check_random_state(404)
467466
y_pred = rng.rand(10)

sklearn/preprocessing/tests/test_data.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
from sklearn.utils.testing import assert_raise_message
2020
from sklearn.utils.testing import assert_almost_equal
21-
from sklearn.utils.testing import clean_warning_registry
2221
from sklearn.utils.testing import assert_array_almost_equal
2322
from sklearn.utils.testing import assert_array_equal
2423
from sklearn.utils.testing import assert_array_less
@@ -941,26 +940,22 @@ def test_scaler_int():
941940
X_csc = sparse.csc_matrix(X)
942941

943942
null_transform = StandardScaler(with_mean=False, with_std=False, copy=True)
944-
clean_warning_registry()
945943
with warnings.catch_warnings(record=True):
946944
X_null = null_transform.fit_transform(X_csr)
947945
assert_array_equal(X_null.data, X_csr.data)
948946
X_orig = null_transform.inverse_transform(X_null)
949947
assert_array_equal(X_orig.data, X_csr.data)
950948

951-
clean_warning_registry()
952949
with warnings.catch_warnings(record=True):
953950
scaler = StandardScaler(with_mean=False).fit(X)
954951
X_scaled = scaler.transform(X, copy=True)
955952
assert not np.any(np.isnan(X_scaled))
956953

957-
clean_warning_registry()
958954
with warnings.catch_warnings(record=True):
959955
scaler_csr = StandardScaler(with_mean=False).fit(X_csr)
960956
X_csr_scaled = scaler_csr.transform(X_csr, copy=True)
961957
assert not np.any(np.isnan(X_csr_scaled.data))
962958

963-
clean_warning_registry()
964959
with warnings.catch_warnings(record=True):
965960
scaler_csc = StandardScaler(with_mean=False).fit(X_csc)
966961
X_csc_scaled = scaler_csc.transform(X_csc, copy=True)

sklearn/tests/test_common.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import pytest
1717

18-
from sklearn.utils.testing import clean_warning_registry
1918
from sklearn.utils.testing import all_estimators
2019
from sklearn.utils.testing import assert_equal
2120
from sklearn.utils.testing import assert_in
@@ -155,7 +154,6 @@ def test_configure():
155154
old_env = os.getenv('SKLEARN_NO_OPENMP')
156155
os.environ['SKLEARN_NO_OPENMP'] = "True"
157156

158-
clean_warning_registry()
159157
with warnings.catch_warnings():
160158
# The configuration spits out warnings when not finding
161159
# Blas/Atlas development headers
@@ -174,7 +172,6 @@ def test_configure():
174172
def _tested_linear_classifiers():
175173
classifiers = all_estimators(type_filter='classifier')
176174

177-
clean_warning_registry()
178175
with warnings.catch_warnings(record=True):
179176
for name, clazz in classifiers:
180177
required_parameters = getattr(clazz, "_required_parameters", [])

sklearn/utils/testing.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ def assert_warns(warning_class, func, *args, **kw):
111111
result : the return value of `func`
112112
113113
"""
114-
clean_warning_registry()
115114
with warnings.catch_warnings(record=True) as w:
116115
# Cause all warnings to always be triggered.
117116
warnings.simplefilter("always")
@@ -160,7 +159,6 @@ def assert_warns_message(warning_class, message, func, *args, **kw):
160159
result : the return value of `func`
161160
162161
"""
163-
clean_warning_registry()
164162
with warnings.catch_warnings(record=True) as w:
165163
# Cause all warnings to always be triggered.
166164
warnings.simplefilter("always")
@@ -236,7 +234,6 @@ def assert_no_warnings(func, *args, **kw):
236234
**kw
237235
"""
238236
# very important to avoid uncontrolled state propagation
239-
clean_warning_registry()
240237
with warnings.catch_warnings(record=True) as w:
241238
warnings.simplefilter('always')
242239

@@ -319,7 +316,6 @@ def __call__(self, fn):
319316
"""Decorator to catch and hide warnings without visual nesting."""
320317
@wraps(fn)
321318
def wrapper(*args, **kwargs):
322-
clean_warning_registry()
323319
with warnings.catch_warnings():
324320
warnings.simplefilter("ignore", self.category)
325321
return fn(*args, **kwargs)
@@ -342,7 +338,6 @@ def __enter__(self):
342338
self._filters = self._module.filters
343339
self._module.filters = self._filters[:]
344340
self._showwarning = self._module.showwarning
345-
clean_warning_registry()
346341
warnings.simplefilter("ignore", self.category)
347342

348343
def __exit__(self, *exc_info):
@@ -351,7 +346,6 @@ def __exit__(self, *exc_info):
351346
self._module.filters = self._filters
352347
self._module.showwarning = self._showwarning
353348
self.log[:] = []
354-
clean_warning_registry()
355349

356350

357351
def assert_raise_message(exceptions, message, function, *args, **kwargs):
@@ -610,15 +604,16 @@ def set_random_state(estimator, random_state=0):
610604
def clean_warning_registry():
611605
"""Clean Python warning registry for easier testing of warning messages.
612606
613-
We may not need to do this any more when getting rid of Python 2, not
614-
entirely sure. See https://bugs.python.org/issue4180 and
607+
When changing warning filters this function is not necessary with
608+
Python3.5+, as __warningregistry__ will be re-set internally.
609+
See https://bugs.python.org/issue4180 and
615610
https://bugs.python.org/issue21724 for more details.
616611
617612
"""
618-
reg = "__warningregistry__"
619-
for mod_name, mod in list(sys.modules.items()):
620-
if hasattr(mod, reg):
621-
getattr(mod, reg).clear()
613+
for mod in sys.modules.values():
614+
registry = getattr(mod, "__warningregistry__", None)
615+
if registry is not None:
616+
registry.clear()
622617

623618

624619
def check_skip_network():

0 commit comments

Comments
 (0)
0