Skip to content

Commit 9753e8b

Browse files
committed
applying suggestions from 865
1 parent 755509a commit 9753e8b

12 files changed

+53
-82
lines changed

nibabel/tests/test_arraywriters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ def test_nan2zero():
508508
assert_array_equal(np.isnan(data_back), [True, False])
509509
# Deprecation warning for nan2zero as argument to `to_fileobj`
510510
with error_warnings():
511-
with pytest.raises(DeprecationWarning):
511+
with pytest.deprecated_call():
512512
aw.to_fileobj(BytesIO(), 'F', True)
513-
with pytest.raises(DeprecationWarning):
513+
with pytest.deprecated_call():
514514
aw.to_fileobj(BytesIO(), 'F', nan2zero=True)
515515
# Error if nan2zero is not the value set at initialization
516516
with pytest.raises(WriterError):
@@ -530,9 +530,9 @@ def test_nan2zero():
530530
assert_array_equal(data_back, [astype_res, 99])
531531
# Deprecation warning for nan2zero as argument to `to_fileobj`
532532
with error_warnings():
533-
with pytest.raises(DeprecationWarning):
533+
with pytest.deprecated_call():
534534
aw.to_fileobj(BytesIO(), 'F', False)
535-
with pytest.raises(DeprecationWarning):
535+
with pytest.deprecated_call():
536536
aw.to_fileobj(BytesIO(), 'F', nan2zero=False)
537537
# Error if nan2zero is not the value set at initialization
538538
with pytest.raises(WriterError):

nibabel/tests/test_deprecator.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,8 @@ def test_dep_func(self):
6969
# Test function deprecation
7070
dec = self.dep_func
7171
func = dec('foo')(func_no_doc)
72-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
73-
warnings.simplefilter('always')
72+
with pytest.deprecated_call():
7473
assert func() == None
75-
assert len(w) == 1
76-
assert w[0].category is DeprecationWarning
7774
assert func.__doc__ == 'foo\n'
7875
func = dec('foo')(func_doc)
7976
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
@@ -135,11 +132,8 @@ def test_dep_func(self):
135132
assert w[0].category is UserWarning
136133

137134
func = dec('foo', error_class=CustomError)(func_no_doc)
138-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
139-
warnings.simplefilter('always')
135+
with pytest.deprecated_call():
140136
assert func() == None
141-
assert len(w) == 1
142-
assert w[0].category is DeprecationWarning
143137

144138
func = dec('foo', until='1.8', error_class=CustomError)(func_no_doc)
145139
with pytest.raises(CustomError):
@@ -162,11 +156,8 @@ def test_deprecator_maker(self):
162156

163157
dec = self.dep_maker(error_class=CustomError)
164158
func = dec('foo')(func_no_doc)
165-
with clear_and_catch_warnings(modules=[_OWN_MODULE]) as w:
166-
warnings.simplefilter('always')
159+
with pytest.deprecated_call():
167160
assert func() == None
168-
assert len(w) == 1
169-
assert w[0].category is DeprecationWarning
170161

171162
func = dec('foo', until='1.8')(func_no_doc)
172163
with pytest.raises(CustomError):

nibabel/tests/test_fileslice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_is_fancy():
4747
assert not is_fancy((None, 1))
4848
assert not is_fancy((1, None))
4949
# Chack that actual False returned (rather than falsey)
50-
assert is_fancy(1) == False
50+
assert is_fancy(1) is False
5151

5252

5353
def test_canonical_slicers():

nibabel/tests/test_funcs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_closest_canonical():
138138
# And a case where the Analyze image has to be flipped
139139
img = AnalyzeImage(arr, np.diag([-1, 1, 1, 1]))
140140
xyz_img = as_closest_canonical(img)
141-
assert not img is xyz_img
141+
assert img is not xyz_img
142142
out_arr = xyz_img.get_fdata()
143143
assert_array_equal(out_arr, np.flipud(arr))
144144

@@ -156,7 +156,7 @@ def test_closest_canonical():
156156
img = Nifti1Image(arr, np.diag([-1, 1, 1, 1]))
157157
img.header.set_dim_info(0, 1, 2)
158158
xyz_img = as_closest_canonical(img)
159-
assert not img is xyz_img
159+
assert img is not xyz_img
160160
assert img.header.get_dim_info() == xyz_img.header.get_dim_info()
161161
out_arr = xyz_img.get_fdata()
162162
assert_array_equal(out_arr, np.flipud(arr))
@@ -181,7 +181,7 @@ def test_closest_canonical():
181181
img.header.set_dim_info(0, 1, 2)
182182

183183
xyz_img = as_closest_canonical(img)
184-
assert not img is xyz_img
184+
assert img is not xyz_img
185185
# Check both the original and new objects
186186
assert img.header.get_dim_info() == (0, 1, 2)
187187
assert xyz_img.header.get_dim_info() == (0, 2, 1)

nibabel/tests/test_image_api.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,8 @@ def validate_header(self, imaker, params):
113113
def validate_header_deprecated(self, imaker, params):
114114
# Check deprecated header API
115115
img = imaker()
116-
with clear_and_catch_warnings() as w:
117-
warnings.simplefilter('always', DeprecationWarning)
116+
with pytest.deprecated_call():
118117
hdr = img.get_header()
119-
assert len(w) == 1
120118
assert hdr is img.header
121119

122120
def validate_filenames(self, imaker, params):
@@ -171,7 +169,7 @@ def validate_no_slicing(self, imaker, params):
171169
def validate_get_data_deprecated(self, imaker, params):
172170
# Check deprecated header API
173171
img = imaker()
174-
with assert_warns(DeprecationWarning):
172+
with pytest.deprecated_call():
175173
data = img.get_data()
176174
assert_array_equal(np.asanyarray(img.dataobj), data)
177175

@@ -395,10 +393,8 @@ def _check_array_caching(self, imaker, meth_name, caching):
395393
def validate_data_deprecated(self, imaker, params):
396394
# Check _data property still exists, but raises warning
397395
img = imaker()
398-
with warnings.catch_warnings(record=True) as warns:
399-
warnings.simplefilter("always")
396+
with pytest.deprecated_call():
400397
assert_data_similar(img._data, params)
401-
assert warns.pop(0).category == DeprecationWarning
402398
# Check setting _data raises error
403399
fake_data = np.zeros(img.shape).astype(img.get_data_dtype())
404400
with pytest.raises(AttributeError):
@@ -502,10 +498,8 @@ def validate_affine(self, imaker, params):
502498
def validate_affine_deprecated(self, imaker, params):
503499
# Check deprecated affine API
504500
img = imaker()
505-
with clear_and_catch_warnings() as w:
506-
warnings.simplefilter('always', DeprecationWarning)
501+
with pytest.deprecated_call():
507502
assert_almost_equal(img.get_affine(), params['affine'], 6)
508-
assert len(w) == 1
509503
assert img.get_affine().dtype == np.float64
510504
aff = img.get_affine()
511505
aff[0, 0] = 1.5

nibabel/tests/test_orientations.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,6 @@ def test_inv_ornt_aff():
380380

381381
def test_orientation_affine_deprecation():
382382
aff1 = inv_ornt_aff([[0, 1], [1, -1], [2, 1]], (3, 4, 5))
383-
with warnings.catch_warnings(record=True) as warns:
384-
warnings.simplefilter('always')
383+
with pytest.deprecated_call():
385384
aff2 = orientation_affine([[0, 1], [1, -1], [2, 1]], (3, 4, 5))
386-
assert len(warns) == 1
387-
assert warns[0].category == DeprecationWarning
388385
assert_array_equal(aff1, aff2)

nibabel/tests/test_parrec.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,8 @@ def test_affine_regression():
263263

264264
def test_get_voxel_size_deprecated():
265265
hdr = PARRECHeader(HDR_INFO, HDR_DEFS)
266-
with clear_and_catch_warnings(modules=[parrec], record=True) as wlist:
267-
simplefilter('always')
266+
with pytest.deprecated_call():
268267
hdr.get_voxel_size()
269-
assert wlist[0].category == DeprecationWarning
270268

271269

272270
def test_get_sorted_slice_indices():
@@ -304,9 +302,9 @@ def test_sorting_dual_echo_T1():
304302
sorted_echos = t1_hdr.image_defs['echo number'][sorted_indices]
305303
n_half = len(t1_hdr.image_defs) // 2
306304
# first half (volume 1) should all correspond to echo 1
307-
assert np.all(sorted_echos[:n_half] == 1) == True
305+
assert np.all(sorted_echos[:n_half] == 1)
308306
# second half (volume 2) should all correspond to echo 2
309-
assert np.all(sorted_echos[n_half:] == 2) == True
307+
assert np.all(sorted_echos[n_half:] == 2)
310308

311309
# check volume labels
312310
vol_labels = t1_hdr.get_volume_labels()
@@ -350,10 +348,10 @@ def test_sorting_multiple_echos_and_contrasts():
350348
assert (np.all(sorted_echos[istart:iend] == current_echo) ==
351349
True)
352350
# outermost sort index is image_type_mr
353-
assert np.all(sorted_types[:ntotal//4] == 0) == True
354-
assert np.all(sorted_types[ntotal//4:ntotal//2] == 1) == True
355-
assert np.all(sorted_types[ntotal//2:3*ntotal//4] == 2) == True
356-
assert np.all(sorted_types[3*ntotal//4:ntotal] == 3) == True
351+
assert np.all(sorted_types[:ntotal//4] == 0)
352+
assert np.all(sorted_types[ntotal//4:ntotal//2] == 1)
353+
assert np.all(sorted_types[ntotal//2:3*ntotal//4] == 2)
354+
assert np.all(sorted_types[3*ntotal//4:ntotal] == 3)
357355

358356
# check volume labels
359357
vol_labels = t1_hdr.get_volume_labels()

nibabel/tests/test_parrec_data.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def test_fieldmap():
6060
fieldmap_nii = pjoin(BALLS, 'NIFTI', 'fieldmap.nii.gz')
6161
load(fieldmap_par)
6262
top_load(fieldmap_nii)
63-
# TODO dj: i believe this shouldn't be here
64-
#raise pytest.skip('Fieldmap remains puzzling')
63+
raise pytest.skip('Fieldmap remains puzzling')
6564

6665

6766
@needs_nibabel_data('parrec_oblique')

nibabel/tests/test_recoder.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ def test_recoder_1():
2222
assert rc.code[1] == 1
2323
assert rc.code[2] == 2
2424
with pytest.raises(KeyError):
25-
rc.code.__getitem__(3)
25+
rc.code[3]
2626

2727
def test_recoder_2():
2828
# with explicit name for code
2929
codes = ((1,), (2,))
3030
rc = Recoder(codes, ['code1'])
3131
with pytest.raises(AttributeError):
32-
rc.__getattribute__('code')
32+
rc.code
3333
assert rc.code1[1] == 1
3434
assert rc.code1[2] == 2
3535

@@ -41,20 +41,20 @@ def test_recoder_3():
4141
assert rc.code[1] == 1
4242
assert rc.code[2] == 2
4343
with pytest.raises(KeyError):
44-
rc.code.__getitem__(3)
44+
rc.code[3]
4545
assert rc.code['one'] == 1
4646
assert rc.code['two'] == 2
4747
with pytest.raises(KeyError):
48-
rc.code.__getitem__('three')
48+
rc.code['three']
4949
with pytest.raises(AttributeError):
50-
rc.__getattribute__('label')
50+
rc.label
5151

5252
def test_recoder_3():
5353
# with explicit column names
5454
codes = ((1, 'one'), (2, 'two'))
5555
rc = Recoder(codes, ['code1', 'label'])
5656
with pytest.raises(AttributeError):
57-
rc.__getattribute__('code')
57+
rc.code
5858
assert rc.code1[1] == 1
5959
assert rc.code1['one'] == 1
6060
assert rc.label[1] == 'one'
@@ -119,7 +119,7 @@ def test_add_codes():
119119
rc = Recoder(codes)
120120
assert rc.code['two'] == 2
121121
with pytest.raises(KeyError):
122-
rc.code.__getitem__('three')
122+
rc.code['three']
123123
rc.add_codes(((3, 'three'), (1, 'number 1')))
124124
assert rc.code['three'] == 3
125125
assert rc.code['number 1'] == 1
@@ -151,7 +151,7 @@ def test_dtmapper():
151151
# dict-like that will lookup on dtypes, even if they don't hash properly
152152
d = DtypeMapper()
153153
with pytest.raises(KeyError):
154-
d.__getitem__(1)
154+
d[1]
155155
d[1] = 'something'
156156
assert d[1] == 'something'
157157
assert list(d.keys()) == [1]
@@ -182,7 +182,7 @@ def test_dtmapper():
182182
sw_dt = canonical_dt.newbyteorder(swapped_code)
183183
d[sw_dt] = 'spam'
184184
with pytest.raises(KeyError):
185-
d.__getitem__(canonical_dt)
185+
d[canonical_dt]
186186
assert d[sw_dt] == 'spam'
187187
sw_intp_dt = intp_dt.newbyteorder(swapped_code)
188188
assert d[sw_intp_dt] == 'spam'

nibabel/tests/test_scaling.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,35 +159,33 @@ def test_array_file_scales(in_type, out_type, err):
159159
# Max rounding error for integer type
160160
max_miss = slope / 2.
161161
assert np.all(np.abs(arr - arr3) <= max_miss)
162-
bio.truncate(0)
163-
bio.seek(0)
164162

165163

166-
@pytest.mark.parametrize("category0, category1",[
164+
@pytest.mark.parametrize("in_type, out_type",[
167165
('int', 'int'),
168166
('uint', 'int'),
169167
])
170-
def test_scaling_in_abstract(category0, category1):
168+
def test_scaling_in_abstract(in_type, out_type):
171169
# Confirm that, for all ints and uints as input, and all possible outputs,
172170
# for any simple way of doing the calculation, the result is near enough
173-
for in_type in np.sctypes[category0]:
174-
for out_type in np.sctypes[category1]:
175-
check_int_a2f(in_type, out_type)
171+
for in_tp in np.sctypes[in_type]:
172+
for out_tp in np.sctypes[out_type]:
173+
check_int_a2f(in_tp, out_tp)
176174

177175

178-
@pytest.mark.parametrize("category0, category1", [
176+
@pytest.mark.parametrize("in_type, out_type", [
179177
('float', 'int'),
180178
('float', 'uint'),
181179
('complex', 'int'),
182180
('complex', 'uint'),
183181
])
184-
def test_scaling_in_abstract_warn(category0, category1):
182+
def test_scaling_in_abstract_warn(in_type, out_type):
185183

186184
# Converting floats to integer
187-
for in_type in np.sctypes[category0]:
188-
for out_type in np.sctypes[category1]:
185+
for in_tp in np.sctypes[in_type]:
186+
for out_tp in np.sctypes[out_type]:
189187
with suppress_warnings(): # overflow
190-
check_int_a2f(in_type, out_type)
188+
check_int_a2f(in_tp, out_tp)
191189

192190

193191
def check_int_a2f(in_type, out_type):

0 commit comments

Comments
 (0)