Skip to content

Commit 8aca6f4

Browse files
tomMoralogrisellesteve
authored
MAINT: remove pytest.warns(None) warnings in pytest 7 (#1264)
Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org> Co-authored-by: Loïc Estève <loic.esteve@ymail.com>
1 parent 067ed4f commit 8aca6f4

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

Diff for: joblib/test/test_dask.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import print_function, division, absolute_import
22
import os
3+
import warnings
34

45
import pytest
56
from random import random
@@ -475,7 +476,7 @@ def func_using_joblib_parallel():
475476
# pytest.warns(UserWarning)) make the test hang. Work-around: return
476477
# the warning record to the client and the warning check is done
477478
# client-side.
478-
with pytest.warns(None) as record:
479+
with warnings.catch_warnings(record=True) as record:
479480
Parallel(n_jobs=2, backend=backend)(
480481
delayed(inc)(i) for i in range(10))
481482

Diff for: joblib/test/test_numpy_pickle.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def test_cache_size_warning(tmpdir, cache_size):
300300
a = rnd.random_sample((10, 2))
301301

302302
warnings.simplefilter("always")
303-
with warns(None) as warninfo:
303+
with warnings.catch_warnings(record=True) as warninfo:
304304
numpy_pickle.dump(a, filename, cache_size=cache_size)
305305
expected_nb_warnings = 1 if cache_size is not None else 0
306306
assert len(warninfo) == expected_nb_warnings
@@ -385,7 +385,7 @@ def _check_pickle(filename, expected_list, mmap_mode=None):
385385
py_version_used_for_writing, 4)
386386
if pickle_reading_protocol >= pickle_writing_protocol:
387387
try:
388-
with warns(None) as warninfo:
388+
with warnings.catch_warnings(record=True) as warninfo:
389389
warnings.simplefilter('always')
390390
warnings.filterwarnings(
391391
'ignore', module='numpy',

Diff for: joblib/test/test_parallel.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
import time
1212
import mmap
13+
import warnings
1314
import threading
1415
from traceback import format_exception
1516
from math import sqrt
@@ -20,8 +21,6 @@
2021
import warnings
2122
import pytest
2223

23-
from importlib import reload
24-
2524
import joblib
2625
from joblib import parallel
2726
from joblib import dump, load
@@ -31,7 +30,7 @@
3130
from joblib.test.common import np, with_numpy
3231
from joblib.test.common import with_multiprocessing
3332
from joblib.testing import (parametrize, raises, check_subprocess_call,
34-
skipif, SkipTest, warns)
33+
skipif, warns)
3534

3635
if mp is not None:
3736
# Loky is not available if multiprocessing is not
@@ -181,7 +180,7 @@ def test_main_thread_renamed_no_warning(backend, monkeypatch):
181180
monkeypatch.setattr(target=threading.current_thread(), name='name',
182181
value='some_new_name_for_the_main_thread')
183182

184-
with warns(None) as warninfo:
183+
with warnings.catch_warnings(record=True) as warninfo:
185184
results = Parallel(n_jobs=2, backend=backend)(
186185
delayed(square)(x) for x in range(3))
187186
assert results == [0, 1, 4]
@@ -197,18 +196,21 @@ def test_main_thread_renamed_no_warning(backend, monkeypatch):
197196

198197

199198
def _assert_warning_nested(backend, inner_n_jobs, expected):
200-
with warnings.catch_warnings(record=True) as records:
199+
with warnings.catch_warnings(record=True) as warninfo:
201200
warnings.simplefilter("always")
202201
parallel_func(backend=backend, inner_n_jobs=inner_n_jobs)
203202

204-
messages = [w.message for w in records]
203+
warninfo = [w.message for w in warninfo]
205204
if expected:
206-
# with threading, we might see more that one records
207-
if messages:
208-
return 'backed parallel loops cannot' in messages[0].args[0]
205+
# with threading, we might see more that one warninfo
206+
if warninfo:
207+
return (
208+
len(warninfo) == 1 and
209+
'backed parallel loops cannot' in warninfo[0].args[0]
210+
)
209211
return False
210212
else:
211-
assert not messages
213+
assert not warninfo
212214
return True
213215

214216

@@ -251,11 +253,11 @@ def test_background_thread_parallelism(backend):
251253
is_run_parallel = [False]
252254

253255
def background_thread(is_run_parallel):
254-
with warns(None) as records:
256+
with warnings.catch_warnings(record=True) as warninfo:
255257
Parallel(n_jobs=2)(
256258
delayed(sleep)(.1) for _ in range(4))
257-
print(len(records))
258-
is_run_parallel[0] = len(records) == 0
259+
print(len(warninfo))
260+
is_run_parallel[0] = len(warninfo) == 0
259261

260262
t = threading.Thread(target=background_thread, args=(is_run_parallel,))
261263
t.start()
@@ -1180,7 +1182,7 @@ def test_memmap_with_big_offset(tmpdir):
11801182

11811183

11821184
def test_warning_about_timeout_not_supported_by_backend():
1183-
with warns(None) as warninfo:
1185+
with warnings.catch_warnings(record=True) as warninfo:
11841186
Parallel(timeout=1)(delayed(square)(i) for i in range(50))
11851187
assert len(warninfo) == 1
11861188
w = warninfo[0]

0 commit comments

Comments
 (0)