29
29
from numpy .testing import assert_array_equal
30
30
from sklearn .utils import IS_PYPY
31
31
from sklearn .utils ._testing import (assert_almost_equal ,
32
- assert_warns_message , assert_raise_message ,
33
- assert_no_warnings ,
32
+ assert_raise_message ,
34
33
fails_if_pypy ,
35
34
assert_allclose_dense_sparse ,
36
35
skip_if_32bit )
@@ -386,8 +385,8 @@ def test_countvectorizer_uppercase_in_vocab():
386
385
" be matched with any documents" )
387
386
388
387
vectorizer = CountVectorizer (lowercase = True , vocabulary = vocabulary )
389
- assert_warns_message (UserWarning , message ,
390
- vectorizer .fit_transform , vocabulary )
388
+ with pytest . warns (UserWarning , match = message ):
389
+ vectorizer .fit_transform ( vocabulary )
391
390
392
391
393
392
def test_tf_idf_smoothing ():
@@ -429,8 +428,8 @@ def test_tfidf_no_smoothing():
429
428
tr = TfidfTransformer (smooth_idf = False , norm = 'l2' )
430
429
431
430
in_warning_message = 'divide by zero'
432
- assert_warns_message (RuntimeWarning , in_warning_message ,
433
- tr .fit_transform , X ).toarray ()
431
+ with pytest . warns (RuntimeWarning , match = in_warning_message ):
432
+ tr .fit_transform ( X ).toarray ()
434
433
435
434
436
435
def test_sublinear_tf ():
@@ -1213,27 +1212,29 @@ def _check_stop_words_consistency(estimator):
1213
1212
1214
1213
@fails_if_pypy
1215
1214
def test_vectorizer_stop_words_inconsistent ():
1216
- lstr = " ['and', 'll', 've']"
1215
+ lstr = r"\ ['and', 'll', 've'\ ]"
1217
1216
message = ('Your stop_words may be inconsistent with your '
1218
1217
'preprocessing. Tokenizing the stop words generated '
1219
1218
'tokens %s not in stop_words.' % lstr )
1220
1219
for vec in [CountVectorizer (),
1221
1220
TfidfVectorizer (), HashingVectorizer ()]:
1222
1221
vec .set_params (stop_words = ["you've" , "you" , "you'll" , 'AND' ])
1223
- assert_warns_message (UserWarning , message , vec . fit_transform ,
1224
- ['hello world' ])
1222
+ with pytest . warns (UserWarning , match = message ):
1223
+ vec . fit_transform ( ['hello world' ])
1225
1224
# reset stop word validation
1226
1225
del vec ._stop_words_id
1227
1226
assert _check_stop_words_consistency (vec ) is False
1228
1227
1229
1228
# Only one warning per stop list
1230
- assert_no_warnings (vec .fit_transform , ['hello world' ])
1229
+ with pytest .warns (None ) as record :
1230
+ vec .fit_transform (['hello world' ])
1231
+ assert not len (record )
1231
1232
assert _check_stop_words_consistency (vec ) is None
1232
1233
1233
1234
# Test caching of inconsistency assessment
1234
1235
vec .set_params (stop_words = ["you've" , "you" , "you'll" , 'blah' , 'AND' ])
1235
- assert_warns_message (UserWarning , message , vec . fit_transform ,
1236
- ['hello world' ])
1236
+ with pytest . warns (UserWarning , match = message ):
1237
+ vec . fit_transform ( ['hello world' ])
1237
1238
1238
1239
1239
1240
@skip_if_32bit
0 commit comments