Skip to content

Commit e39bf01

Browse files
authored
DEP deprecate warns context manager (#815)
1 parent 71b1e87 commit e39bf01

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

doc/whats_new/v0.8.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ Bug fixes
5050
- Fix a bug in :class:`imblearn.FunctionSampler` where validation was performed
5151
even with `validate=False` when calling `fit`.
5252
:pr:`790` by :user:`Guillaume Lemaitre <glemaitre>`.
53+
54+
Deprecation
55+
...........
56+
57+
- The context manager :func:`imblearn.utils.testing.warns` is deprecated in 0.8
58+
and will be removed 1.0.
59+
:pr:`815` by :user:`Guillaume Lemaitre <glemaitre>`.

imblearn/utils/testing.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import inspect
88
import pkgutil
9+
import warnings
910
from contextlib import contextmanager
1011
from importlib import import_module
1112
from re import compile
@@ -18,7 +19,9 @@
1819
from sklearn.utils._testing import ignore_warnings
1920

2021

21-
def all_estimators(type_filter=None,):
22+
def all_estimators(
23+
type_filter=None,
24+
):
2225
"""Get a list of all estimators from imblearn.
2326
2427
This function crawls the module and gets all classes that inherit
@@ -120,6 +123,10 @@ def warns(expected_warning, match=None):
120123
and raise a failure exception otherwise. It can be used within a context
121124
manager ``with``.
122125
126+
.. deprecated:: 0.8
127+
This function is deprecated in 0.8 and will be removed in 0.10.
128+
Use `pytest.warns()` instead.
129+
123130
Parameters
124131
----------
125132
expected_warning : Warning
@@ -134,13 +141,16 @@ def warns(expected_warning, match=None):
134141
135142
Examples
136143
--------
137-
138144
>>> import warnings
139145
>>> from imblearn.utils.testing import warns
140146
>>> with warns(UserWarning, match=r'must be \d+$'):
141147
... warnings.warn("value must be 42", UserWarning)
142-
143148
"""
149+
warnings.warn(
150+
"The warns function is deprecated in 0.8 and will be removed in 0.10. "
151+
"Use pytest.warns() instead."
152+
)
153+
144154
with _warns(expected_warning) as record:
145155
yield
146156

imblearn/utils/tests/test_testing.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Christos Aridas
44
# License: MIT
55

6-
from pytest import raises
6+
import pytest
77

88
from imblearn.base import SamplerMixin
99
from imblearn.utils.testing import all_estimators
@@ -23,17 +23,18 @@ def test_all_estimators():
2323

2424
# check that an error is raised when the type is unknown
2525
type_filter = "rnd"
26-
with raises(ValueError, match="Parameter type_filter must be 'sampler'"):
26+
with pytest.raises(ValueError, match="Parameter type_filter must be 'sampler'"):
2727
all_estimators(type_filter=type_filter)
2828

2929

30+
@pytest.mark.filterwarnings("ignore:The warns function is deprecated in 0.8")
3031
def test_warns():
3132
import warnings
3233

3334
with warns(UserWarning, match=r"must be \d+$"):
3435
warnings.warn("value must be 42", UserWarning)
3536

36-
with raises(AssertionError, match="pattern not found"):
37+
with pytest.raises(AssertionError, match="pattern not found"):
3738
with warns(UserWarning, match=r"must be \d+$"):
3839
warnings.warn("this is not here", UserWarning)
3940

@@ -44,7 +45,17 @@ def test_warns():
4445

4546
a, b, c = ("aaa", "bbbbbbbbbb", "cccccccccc")
4647
expected_msg = r"'{}' pattern not found in \['{}', '{}'\]".format(a, b, c)
47-
with raises(AssertionError, match=expected_msg):
48+
with pytest.raises(AssertionError, match=expected_msg):
4849
with warns(UserWarning, match=r"aaa"):
4950
warnings.warn("bbbbbbbbbb", UserWarning)
5051
warnings.warn("cccccccccc", UserWarning)
52+
53+
54+
# TODO: remove in 0.9
55+
def test_warns_deprecation():
56+
import warnings
57+
58+
with pytest.warns(None) as record:
59+
with warns(UserWarning):
60+
warnings.warn("value must be 42")
61+
assert "The warns function is deprecated" in str(record[0].message)

0 commit comments

Comments
 (0)