Skip to content

Commit

Permalink
Merge pull request #1356 from DimitriPapadopoulos/PT
Browse files Browse the repository at this point in the history
STY: Enforce ruff/flake8-pytest-style rules (PT)
  • Loading branch information
effigies authored Sep 25, 2024
2 parents 2532132 + e58e2ea commit b614330
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 37 deletions.
6 changes: 3 additions & 3 deletions nibabel/cmdline/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_convert_dtype(tmp_path, data_dtype):


@pytest.mark.parametrize(
'ext,img_class',
('ext', 'img_class'),
[
('mgh', nib.MGHImage),
('img', nib.Nifti1Pair),
Expand All @@ -94,7 +94,7 @@ def test_convert_by_extension(tmp_path, ext, img_class):


@pytest.mark.parametrize(
'ext,img_class',
('ext', 'img_class'),
[
('mgh', nib.MGHImage),
('img', nib.Nifti1Pair),
Expand Down Expand Up @@ -141,7 +141,7 @@ def test_convert_nifti_int_fail(tmp_path):


@pytest.mark.parametrize(
'orig_dtype,alias,expected_dtype',
('orig_dtype', 'alias', 'expected_dtype'),
[
('int64', 'mask', 'uint8'),
('int64', 'compat', 'int32'),
Expand Down
8 changes: 2 additions & 6 deletions nibabel/cmdline/tests/test_roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_nib_roi(tmp_path, inplace):


@pytest.mark.parametrize(
'args, errmsg',
('args', 'errmsg'),
(
(('-i', '1:1'), 'Cannot take zero-length slice'),
(('-j', '1::2'), 'Downsampling is not supported'),
Expand All @@ -138,12 +138,8 @@ def test_nib_roi_bad_slices(capsys, args, errmsg):
def test_entrypoint(capsys):
# Check that we handle missing args as expected
with mock.patch('sys.argv', ['nib-roi', '--help']):
try:
with pytest.raises(SystemExit):
main()
except SystemExit:
pass
else:
assert False, 'argparse exits on --help. If changing to another parser, update test.'
captured = capsys.readouterr()
assert captured.out.startswith('usage: nib-roi')

Expand Down
8 changes: 4 additions & 4 deletions nibabel/streamlines/tests/test_streamlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ def test_save_tractogram_file(self):
trk_file = trk.TrkFile(tractogram)

# No need for keyword arguments.
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
nib.streamlines.save(trk_file, 'dummy.trk', header={})

# Wrong extension.
with pytest.warns(ExtensionWarning, match='extension'):
trk_file = trk.TrkFile(tractogram)
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
nib.streamlines.save(trk_file, 'dummy.tck', header={})

with InTemporaryDirectory():
Expand Down Expand Up @@ -272,11 +272,11 @@ def test_save_sliced_tractogram(self):
assert_tractogram_equal(tractogram, original_tractogram)

def test_load_unknown_format(self):
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
nib.streamlines.load('')

def test_save_unknown_format(self):
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
nib.streamlines.save(Tractogram(), '')

def test_save_from_generator(self):
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_dft.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def db(monkeypatch):
and not modify the host filesystem."""
database = dft._DB(fname=':memory:')
monkeypatch.setattr(dft, 'DB', database)
yield database
return database


def test_init(db):
Expand Down
4 changes: 2 additions & 2 deletions nibabel/tests/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_euler_mat_1():
assert_array_equal(M, np.eye(3))


@pytest.mark.parametrize('x, y, z', eg_rots)
@pytest.mark.parametrize(('x', 'y', 'z'), eg_rots)
def test_euler_mat_2(x, y, z):
M1 = nea.euler2mat(z, y, x)
M2 = sympy_euler(z, y, x)
Expand Down Expand Up @@ -176,7 +176,7 @@ def test_euler_instability():
assert not np.allclose(M_e, M_e_back)


@pytest.mark.parametrize('x, y, z', eg_rots)
@pytest.mark.parametrize(('x', 'y', 'z'), eg_rots)
def test_quats(x, y, z):
M1 = nea.euler2mat(z, y, x)
quatM = nq.mat2quat(M1)
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


@pytest.mark.parametrize(
'verbose, v_args', [(-2, ['-qq']), (-1, ['-q']), (0, []), (1, ['-v']), (2, ['-vv'])]
('verbose', 'v_args'), [(-2, ['-qq']), (-1, ['-q']), (0, []), (1, ['-v']), (2, ['-vv'])]
)
@pytest.mark.parametrize('doctests', (True, False))
@pytest.mark.parametrize('coverage', (True, False))
Expand Down
4 changes: 1 addition & 3 deletions nibabel/tests/test_pkg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_cmp_pkg_version_0():


@pytest.mark.parametrize(
'test_ver, pkg_ver, exp_out',
('test_ver', 'pkg_ver', 'exp_out'),
[
('1.0', '1.0', 0),
('1.0.0', '1.0', 0),
Expand All @@ -54,8 +54,6 @@ def test_cmp_pkg_version_0():
('1.2.1rc1', '1.2.1', -1),
('1.2.1rc1', '1.2.1rc', 1),
('1.2.1rc', '1.2.1rc1', -1),
('1.2.1rc1', '1.2.1rc', 1),
('1.2.1rc', '1.2.1rc1', -1),
('1.2.1b', '1.2.1a', 1),
('1.2.1a', '1.2.1b', -1),
('1.2.0+1', '1.2', 1),
Expand Down
10 changes: 5 additions & 5 deletions nibabel/tests/test_quaternions.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_inverse_0():
assert iq.dtype.kind == 'f'


@pytest.mark.parametrize('M, q', eg_pairs)
@pytest.mark.parametrize(('M', 'q'), eg_pairs)
def test_inverse_1(M, q):
iq = nq.inverse(q)
iqM = nq.quat2mat(iq)
Expand All @@ -169,15 +169,15 @@ def test_norm():
assert not nq.isunit(qi)


@pytest.mark.parametrize('M1, q1', eg_pairs[0::4])
@pytest.mark.parametrize('M2, q2', eg_pairs[1::4])
@pytest.mark.parametrize(('M1', 'q1'), eg_pairs[0::4])
@pytest.mark.parametrize(('M2', 'q2'), eg_pairs[1::4])
def test_mult(M1, q1, M2, q2):
# Test that quaternion * same as matrix *
q21 = nq.mult(q2, q1)
assert_array_almost_equal, M2 @ M1, nq.quat2mat(q21)


@pytest.mark.parametrize('M, q', eg_pairs)
@pytest.mark.parametrize(('M', 'q'), eg_pairs)
def test_inverse(M, q):
iq = nq.inverse(q)
iqM = nq.quat2mat(iq)
Expand All @@ -186,7 +186,7 @@ def test_inverse(M, q):


@pytest.mark.parametrize('vec', np.eye(3))
@pytest.mark.parametrize('M, q', eg_pairs)
@pytest.mark.parametrize(('M', 'q'), eg_pairs)
def test_qrotate(vec, M, q):
vdash = nq.rotate_vector(vec, q)
vM = M @ vec
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_removalschedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_module_removal():
for module in _filter(MODULE_SCHEDULE):
with pytest.raises(ImportError):
__import__(module)
assert False, f'Time to remove {module}'
raise AssertionError(f'Time to remove {module}')


def test_object_removal():
Expand Down
7 changes: 3 additions & 4 deletions nibabel/tests/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


@pytest.mark.parametrize(
'in_arr, res',
('in_arr', 'res'),
[
([[-1, 0, 1], [np.inf, np.nan, -np.inf]], (-1, 1)),
(np.array([[-1, 0, 1], [np.inf, np.nan, -np.inf]]), (-1, 1)),
Expand All @@ -36,7 +36,6 @@
([[np.nan, -1, 2], [-2, np.nan, 1]], (-2, 2)),
([[np.nan, -np.inf, 2], [-2, np.nan, np.inf]], (-2, 2)),
([[-np.inf, 2], [np.nan, 1]], (1, 2)), # good max case
([[np.nan, -np.inf, 2], [-2, np.nan, np.inf]], (-2, 2)),
([np.nan], (np.inf, -np.inf)),
([np.inf], (np.inf, -np.inf)),
([-np.inf], (np.inf, -np.inf)),
Expand Down Expand Up @@ -134,7 +133,7 @@ def test_a2f_nan2zero():


@pytest.mark.parametrize(
'in_type, out_type',
('in_type', 'out_type'),
[
(np.int16, np.int16),
(np.int16, np.int8),
Expand Down Expand Up @@ -163,7 +162,7 @@ def test_array_file_scales(in_type, out_type):


@pytest.mark.parametrize(
'category0, category1, overflow',
('category0', 'category1', 'overflow'),
[
# Confirm that, for all ints and uints as input, and all possible outputs,
# for any simple way of doing the calculation, the result is near enough
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_slice2volume():


@pytest.mark.parametrize(
'index, axis',
('index', 'axis'),
[
[-1, 0],
[0, -1],
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def f():


@pytest.mark.parametrize(
'regex, entries',
('regex', 'entries'),
[
['.*', ''],
['.*', ['any']],
Expand Down
6 changes: 1 addition & 5 deletions nibabel/tests/test_tripwire.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,5 @@ def test_tripwire():
with pytest.raises(TripWireError):
silly_module_name.do_silly_thing
# Check AttributeError can be checked too
try:
with pytest.raises(AttributeError) as err:
silly_module_name.__wrapped__
except TripWireError as err:
assert isinstance(err, AttributeError)
else:
raise RuntimeError('No error raised, but expected')
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ select = [
"I",
"PIE",
"PLE",
"PT",
"PYI",
"Q",
"RSE",
Expand All @@ -146,6 +147,12 @@ ignore = [
"C408",
"C416",
"PIE790",
"PT004",
"PT007",
"PT011",
"PT012",
"PT017",
"PT018",
"PYI024",
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"W191",
Expand Down

0 comments on commit b614330

Please sign in to comment.