Skip to content

Commit b671c23

Browse files
committed
TEST: Suppress deprecation warnings from pytest and numpy
1 parent db82744 commit b671c23

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

nibabel/nicom/tests/test_dicomwrappers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,10 @@ def test_assert_parallel():
361361

362362
@dicom_test
363363
def test_decimal_rescale():
364-
# Test that we don't get back a data array with dtype np.object when our
364+
# Test that we don't get back a data array with dtype object when our
365365
# rescale slope is a decimal
366366
dw = didw.wrapper_from_file(DATA_FILE_DEC_RSCL)
367-
assert dw.get_data().dtype != np.object
367+
assert dw.get_data().dtype != np.dtype(object)
368368

369369

370370
def fake_frames(seq_name, field_name, value_seq):

nibabel/streamlines/tests/test_streamlines.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
import tempfile
44
import numpy as np
5+
import warnings
56

67
import pytest
78

@@ -12,7 +13,7 @@
1213
from nibabel.tmpdirs import InTemporaryDirectory
1314
from numpy.compat.py3k import asbytes
1415

15-
from nibabel.testing import data_path
16+
from nibabel.testing import data_path, error_warnings, clear_and_catch_warnings
1617

1718
from .test_tractogram import assert_tractogram_equal
1819
from ..tractogram import Tractogram, LazyTractogram
@@ -142,7 +143,7 @@ def test_load_empty_file(self):
142143
else:
143144
assert type(tfile.tractogram), LazyTractogram
144145

145-
with pytest.warns(Warning if lazy_load else None):
146+
with pytest.warns(Warning) if lazy_load else error_warnings():
146147
assert_tractogram_equal(tfile.tractogram,
147148
DATA['empty_tractogram'])
148149

@@ -158,7 +159,7 @@ def test_load_simple_file(self):
158159
else:
159160
assert type(tfile.tractogram), LazyTractogram
160161

161-
with pytest.warns(Warning if lazy_load else None):
162+
with pytest.warns(Warning) if lazy_load else error_warnings():
162163
assert_tractogram_equal(tfile.tractogram,
163164
DATA['simple_tractogram'])
164165

@@ -184,7 +185,7 @@ def test_load_complex_file(self):
184185
data = DATA['data_per_streamline']
185186
tractogram.data_per_streamline = data
186187

187-
with pytest.warns(Warning if lazy_load else None):
188+
with pytest.warns(Warning) if lazy_load else error_warnings():
188189
assert_tractogram_equal(tfile.tractogram,
189190
tractogram)
190191

@@ -244,7 +245,8 @@ def test_save_complex_file(self):
244245
((not cls.SUPPORTS_DATA_PER_POINT) +
245246
(not cls.SUPPORTS_DATA_PER_STREAMLINE))
246247

247-
with pytest.warns(Warning if nb_expected_warnings else None) as w:
248+
with clear_and_catch_warnings() as w:
249+
warnings.simplefilter('always')
248250
nib.streamlines.save(complex_tractogram, filename)
249251
assert len(w) == nb_expected_warnings
250252

nibabel/streamlines/tests/test_tck.py

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

1616
import pytest
1717
from numpy.testing import assert_array_equal
18-
from ...testing import data_path
18+
from ...testing import data_path, error_warnings
1919
from .test_tractogram import assert_tractogram_equal
2020

2121
DATA = {}
@@ -47,13 +47,13 @@ class TestTCK(unittest.TestCase):
4747
def test_load_empty_file(self):
4848
for lazy_load in [False, True]:
4949
tck = TckFile.load(DATA['empty_tck_fname'], lazy_load=lazy_load)
50-
with pytest.warns(Warning if lazy_load else None):
50+
with pytest.warns(Warning) if lazy_load else error_warnings():
5151
assert_tractogram_equal(tck.tractogram, DATA['empty_tractogram'])
5252

5353
def test_load_simple_file(self):
5454
for lazy_load in [False, True]:
5555
tck = TckFile.load(DATA['simple_tck_fname'], lazy_load=lazy_load)
56-
with pytest.warns(Warning if lazy_load else None):
56+
with pytest.warns(Warning) if lazy_load else error_warnings():
5757
assert_tractogram_equal(tck.tractogram, DATA['simple_tractogram'])
5858

5959
# Force TCK loading to use buffering.
@@ -88,7 +88,7 @@ def test_load_simple_file_in_big_endian(self):
8888
for lazy_load in [False, True]:
8989
tck = TckFile.load(DATA['simple_tck_big_endian_fname'],
9090
lazy_load=lazy_load)
91-
with pytest.warns(Warning if lazy_load else None):
91+
with pytest.warns(Warning) if lazy_load else error_warnings():
9292
assert_tractogram_equal(tck.tractogram, DATA['simple_tractogram'])
9393
assert tck.header['datatype'] == 'Float32BE'
9494

nibabel/streamlines/tests/test_trk.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from io import BytesIO
99

1010
import pytest
11-
from ...testing import data_path, clear_and_catch_warnings, assert_arr_dict_equal
11+
from ...testing import data_path, clear_and_catch_warnings, assert_arr_dict_equal, error_warnings
1212
from numpy.testing import assert_array_equal
1313

1414
from .test_tractogram import assert_tractogram_equal
@@ -87,19 +87,19 @@ class TestTRK(unittest.TestCase):
8787
def test_load_empty_file(self):
8888
for lazy_load in [False, True]:
8989
trk = TrkFile.load(DATA['empty_trk_fname'], lazy_load=lazy_load)
90-
with pytest.warns(Warning if lazy_load else None):
90+
with pytest.warns(Warning) if lazy_load else error_warnings():
9191
assert_tractogram_equal(trk.tractogram, DATA['empty_tractogram'])
9292

9393
def test_load_simple_file(self):
9494
for lazy_load in [False, True]:
9595
trk = TrkFile.load(DATA['simple_trk_fname'], lazy_load=lazy_load)
96-
with pytest.warns(Warning if lazy_load else None):
96+
with pytest.warns(Warning) if lazy_load else error_warnings():
9797
assert_tractogram_equal(trk.tractogram, DATA['simple_tractogram'])
9898

9999
def test_load_complex_file(self):
100100
for lazy_load in [False, True]:
101101
trk = TrkFile.load(DATA['complex_trk_fname'], lazy_load=lazy_load)
102-
with pytest.warns(Warning if lazy_load else None):
102+
with pytest.warns(Warning) if lazy_load else error_warnings():
103103
assert_tractogram_equal(trk.tractogram, DATA['complex_tractogram'])
104104

105105
def trk_with_bytes(self, trk_key='simple_trk_fname', endian='<'):
@@ -199,7 +199,7 @@ def test_load_complex_file_in_big_endian(self):
199199
for lazy_load in [False, True]:
200200
trk = TrkFile.load(DATA['complex_trk_big_endian_fname'],
201201
lazy_load=lazy_load)
202-
with pytest.warns(Warning if lazy_load else None):
202+
with pytest.warns(Warning) if lazy_load else error_warnings():
203203
assert_tractogram_equal(trk.tractogram, DATA['complex_tractogram'])
204204

205205
def test_tractogram_file_properties(self):

nibabel/tests/test_volumeutils.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
assert_array_equal)
5656
import pytest
5757

58-
from nibabel.testing import nullcontext, assert_dt_equal, assert_allclose_safely, suppress_warnings
58+
from nibabel.testing import (assert_dt_equal, assert_allclose_safely,
59+
suppress_warnings, error_warnings)
5960

6061
pyzstd, HAVE_ZSTD, _ = optional_package("pyzstd")
6162

@@ -623,7 +624,7 @@ def test_a2f_bad_scaling():
623624
(0, np.nan, -np.inf, np.inf)):
624625
arr = np.ones((2,), dtype=in_type)
625626
fobj = BytesIO()
626-
cm = nullcontext()
627+
cm = error_warnings()
627628
if (np.issubdtype(in_type, np.complexfloating) and
628629
not np.issubdtype(out_type, np.complexfloating)):
629630
cm = pytest.warns(np.ComplexWarning)
@@ -674,37 +675,37 @@ def test_a2f_nan2zero_range():
674675
warn_type = np.ComplexWarning if np.issubdtype(dt, np.complexfloating) else None
675676
# No errors from explicit thresholding
676677
# mn thresholding excluding zero
677-
with pytest.warns(warn_type):
678+
with pytest.warns(warn_type) if warn_type else error_warnings():
678679
assert_array_equal([1, 1, 1, 0],
679680
write_return(arr, fobj, np.int8, mn=1))
680681
# mx thresholding excluding zero
681-
with pytest.warns(warn_type):
682+
with pytest.warns(warn_type) if warn_type else error_warnings():
682683
assert_array_equal([-1, -1, -1, 0],
683684
write_return(arr, fobj, np.int8, mx=-1))
684685
# Errors from datatype threshold after scaling
685-
with pytest.warns(warn_type):
686+
with pytest.warns(warn_type) if warn_type else error_warnings():
686687
back_arr = write_return(arr, fobj, np.int8, intercept=128)
687688
assert_array_equal([-128, -128, -127, -128], back_arr)
688689
with pytest.raises(ValueError):
689690
write_return(arr, fobj, np.int8, intercept=129)
690691
with pytest.raises(ValueError):
691692
write_return(arr_no_nan, fobj, np.int8, intercept=129)
692693
# OK with nan2zero false, but we get whatever nan casts to
693-
with pytest.warns(warn_type):
694+
with pytest.warns(warn_type) if warn_type else error_warnings():
694695
nan_cast = np.array(np.nan, dtype=dt).astype(np.int8)
695-
with pytest.warns(warn_type):
696+
with pytest.warns(warn_type) if warn_type else error_warnings():
696697
back_arr = write_return(arr, fobj, np.int8, intercept=129, nan2zero=False)
697698
assert_array_equal([-128, -128, -128, nan_cast], back_arr)
698699
# divslope
699-
with pytest.warns(warn_type):
700+
with pytest.warns(warn_type) if warn_type else error_warnings():
700701
back_arr = write_return(arr, fobj, np.int8, intercept=256, divslope=2)
701702
assert_array_equal([-128, -128, -128, -128], back_arr)
702703
with pytest.raises(ValueError):
703704
write_return(arr, fobj, np.int8, intercept=257.1, divslope=2)
704705
with pytest.raises(ValueError):
705706
write_return(arr_no_nan, fobj, np.int8, intercept=257.1, divslope=2)
706707
# OK with nan2zero false
707-
with pytest.warns(warn_type):
708+
with pytest.warns(warn_type) if warn_type else error_warnings():
708709
back_arr = write_return(arr, fobj, np.int8,
709710
intercept=257.1, divslope=2, nan2zero=False)
710711
assert_array_equal([-128, -128, -128, nan_cast], back_arr)

0 commit comments

Comments
 (0)