From f4646182fe16e6af965b575a90499e28d6840f9a Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:30:48 +0200 Subject: [PATCH 1/9] STY: Apply ruff/flake8-pytest-style rule PT006 PT006 Wrong type passed to first argument of `@pytest.mark.parametrize`; expected `tuple` --- nibabel/cmdline/tests/test_convert.py | 6 +++--- nibabel/cmdline/tests/test_roi.py | 2 +- nibabel/tests/test_euler.py | 4 ++-- nibabel/tests/test_init.py | 2 +- nibabel/tests/test_pkg_info.py | 2 +- nibabel/tests/test_quaternions.py | 10 +++++----- nibabel/tests/test_scaling.py | 6 +++--- nibabel/tests/test_spaces.py | 2 +- nibabel/tests/test_testing.py | 2 +- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/nibabel/cmdline/tests/test_convert.py b/nibabel/cmdline/tests/test_convert.py index 021e6ea8ef..d500a717a3 100644 --- a/nibabel/cmdline/tests/test_convert.py +++ b/nibabel/cmdline/tests/test_convert.py @@ -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), @@ -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), @@ -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'), diff --git a/nibabel/cmdline/tests/test_roi.py b/nibabel/cmdline/tests/test_roi.py index d2baa80eeb..19bdf29011 100644 --- a/nibabel/cmdline/tests/test_roi.py +++ b/nibabel/cmdline/tests/test_roi.py @@ -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'), diff --git a/nibabel/tests/test_euler.py b/nibabel/tests/test_euler.py index b0c965c399..1a781b8f14 100644 --- a/nibabel/tests/test_euler.py +++ b/nibabel/tests/test_euler.py @@ -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) @@ -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) diff --git a/nibabel/tests/test_init.py b/nibabel/tests/test_init.py index 969b80b6fc..d54f55053b 100644 --- a/nibabel/tests/test_init.py +++ b/nibabel/tests/test_init.py @@ -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)) diff --git a/nibabel/tests/test_pkg_info.py b/nibabel/tests/test_pkg_info.py index a39eac65b1..94ee903494 100644 --- a/nibabel/tests/test_pkg_info.py +++ b/nibabel/tests/test_pkg_info.py @@ -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), diff --git a/nibabel/tests/test_quaternions.py b/nibabel/tests/test_quaternions.py index ec882dd0b3..a5ec89d948 100644 --- a/nibabel/tests/test_quaternions.py +++ b/nibabel/tests/test_quaternions.py @@ -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) @@ -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) @@ -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 diff --git a/nibabel/tests/test_scaling.py b/nibabel/tests/test_scaling.py index f667b4164d..eae0b1702c 100644 --- a/nibabel/tests/test_scaling.py +++ b/nibabel/tests/test_scaling.py @@ -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)), @@ -134,7 +134,7 @@ def test_a2f_nan2zero(): @pytest.mark.parametrize( - 'in_type, out_type', + ('in_type', 'out_type'), [ (np.int16, np.int16), (np.int16, np.int8), @@ -163,7 +163,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 diff --git a/nibabel/tests/test_spaces.py b/nibabel/tests/test_spaces.py index f5e467b2cc..4722228a5b 100644 --- a/nibabel/tests/test_spaces.py +++ b/nibabel/tests/test_spaces.py @@ -125,7 +125,7 @@ def test_slice2volume(): @pytest.mark.parametrize( - 'index, axis', + ('index', 'axis'), [ [-1, 0], [0, -1], diff --git a/nibabel/tests/test_testing.py b/nibabel/tests/test_testing.py index 04ba813d8b..ec147baa95 100644 --- a/nibabel/tests/test_testing.py +++ b/nibabel/tests/test_testing.py @@ -148,7 +148,7 @@ def f(): @pytest.mark.parametrize( - 'regex, entries', + ('regex', 'entries'), [ ['.*', ''], ['.*', ['any']], From bb1c08b44ceb923a850beb86f25576a1e4866c5b Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:35:25 +0200 Subject: [PATCH 2/9] STY: Apply ruff/flake8-pytest-style rule PT014 PT014 Duplicate of test case --- nibabel/tests/test_pkg_info.py | 2 -- nibabel/tests/test_scaling.py | 1 - 2 files changed, 3 deletions(-) diff --git a/nibabel/tests/test_pkg_info.py b/nibabel/tests/test_pkg_info.py index 94ee903494..1a9a06dc93 100644 --- a/nibabel/tests/test_pkg_info.py +++ b/nibabel/tests/test_pkg_info.py @@ -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), diff --git a/nibabel/tests/test_scaling.py b/nibabel/tests/test_scaling.py index eae0b1702c..ccc379c256 100644 --- a/nibabel/tests/test_scaling.py +++ b/nibabel/tests/test_scaling.py @@ -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)), From 30cba2ca39bc02a2da7f7411a178354046fd6cd2 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:37:46 +0200 Subject: [PATCH 3/9] STY: Apply ruff/flake8-pytest-style rule PT015 PT015 Assertion always fails, replace with `pytest.fail()` --- nibabel/cmdline/tests/test_roi.py | 2 +- nibabel/tests/test_removalschedule.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nibabel/cmdline/tests/test_roi.py b/nibabel/cmdline/tests/test_roi.py index 19bdf29011..5f538d53f4 100644 --- a/nibabel/cmdline/tests/test_roi.py +++ b/nibabel/cmdline/tests/test_roi.py @@ -143,7 +143,7 @@ def test_entrypoint(capsys): except SystemExit: pass else: - assert False, 'argparse exits on --help. If changing to another parser, update test.' + pytest.fail('argparse exits on --help. If changing to another parser, update test.') captured = capsys.readouterr() assert captured.out.startswith('usage: nib-roi') diff --git a/nibabel/tests/test_removalschedule.py b/nibabel/tests/test_removalschedule.py index 7a56f3fb8b..d2bc7da2fc 100644 --- a/nibabel/tests/test_removalschedule.py +++ b/nibabel/tests/test_removalschedule.py @@ -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(): From e4a8d1c9f8e79dbd43b62cbc24dfeeb98abf27b3 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:43:42 +0200 Subject: [PATCH 4/9] STY: Apply ruff/flake8-pytest-style rule PT017 PT017 Found assertion on exception `err` in `except` block, use `pytest.raises()` instead --- nibabel/tests/test_tripwire.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/nibabel/tests/test_tripwire.py b/nibabel/tests/test_tripwire.py index bcc81b5f5f..6bc4e8533e 100644 --- a/nibabel/tests/test_tripwire.py +++ b/nibabel/tests/test_tripwire.py @@ -16,9 +16,6 @@ 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') + assert isinstance(err.value, AttributeError) From 341d6d79e35f328b4a6ab2ddd3aa2dc8b5416c2e Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:45:31 +0200 Subject: [PATCH 5/9] STY: Apply ruff/flake8-pytest-style rule PT022 PT022 No teardown in fixture `db`, use `return` instead of `yield` --- nibabel/tests/test_dft.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nibabel/tests/test_dft.py b/nibabel/tests/test_dft.py index 6c6695b16e..6155dda83c 100644 --- a/nibabel/tests/test_dft.py +++ b/nibabel/tests/test_dft.py @@ -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): From bb549fbc84643020b4159d07cf6abcc0fbc34a45 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:46:22 +0200 Subject: [PATCH 6/9] STY: Apply ruff/flake8-pytest-style rule PT027 PT027 Use `pytest.raises` instead of unittest-style `assertRaises` --- nibabel/streamlines/tests/test_streamlines.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nibabel/streamlines/tests/test_streamlines.py b/nibabel/streamlines/tests/test_streamlines.py index 857e64fec9..359cbc5e1c 100644 --- a/nibabel/streamlines/tests/test_streamlines.py +++ b/nibabel/streamlines/tests/test_streamlines.py @@ -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(): @@ -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): From a7e1afdb0b292ae7de45bfadb3d9313b9341df70 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:44:19 +0200 Subject: [PATCH 7/9] STY: Enforce ruff/flake8-pytest-style rules (PT) --- pyproject.toml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index c973d3e0c2..22be5f917f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -125,6 +125,7 @@ select = [ "I", "PIE", "PLE", + "PT", "PYI", "Q", "RSE", @@ -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", From 0e1dee31894e3034031ce0a251c5cfe73da5cdfc Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos Orfanos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Tue, 24 Sep 2024 22:32:58 +0200 Subject: [PATCH 8/9] MNT: Drop test which verifies that TripWireError is an AttributeError Co-authored-by: Chris Markiewicz --- nibabel/tests/test_tripwire.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nibabel/tests/test_tripwire.py b/nibabel/tests/test_tripwire.py index 6bc4e8533e..d7daefe0b1 100644 --- a/nibabel/tests/test_tripwire.py +++ b/nibabel/tests/test_tripwire.py @@ -18,4 +18,3 @@ def test_tripwire(): # Check AttributeError can be checked too with pytest.raises(AttributeError) as err: silly_module_name.__wrapped__ - assert isinstance(err.value, AttributeError) From e58e2ea40ed5c9d0d5bf613e86c789ea0689eedb Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Tue, 24 Sep 2024 22:34:04 +0200 Subject: [PATCH 9/9] MNT: Simplify try/except/else block Co-authored-by: Chris Markiewicz --- nibabel/cmdline/tests/test_roi.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/nibabel/cmdline/tests/test_roi.py b/nibabel/cmdline/tests/test_roi.py index 5f538d53f4..4692bbb038 100644 --- a/nibabel/cmdline/tests/test_roi.py +++ b/nibabel/cmdline/tests/test_roi.py @@ -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: - pytest.fail('argparse exits on --help. If changing to another parser, update test.') captured = capsys.readouterr() assert captured.out.startswith('usage: nib-roi')