From aa1315277b5f2b8ff9cfda4e16b7ab98a57eecf4 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:05:30 +0200 Subject: [PATCH 1/3] STY: Apply ruff/flake8-bugbear rule B009 B009 Do not call `getattr` with a constant attribute value. It is not any safer than normal property access. --- nibabel/tests/conftest.py | 2 +- nibabel/viewers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nibabel/tests/conftest.py b/nibabel/tests/conftest.py index 3cf54a34c5..fb13708450 100644 --- a/nibabel/tests/conftest.py +++ b/nibabel/tests/conftest.py @@ -6,7 +6,7 @@ # Generate dynamic fixtures def pytest_generate_tests(metafunc): if 'supported_dtype' in metafunc.fixturenames: - if metafunc.cls is None or not getattr(metafunc.cls, 'image_class'): + if metafunc.cls is None or not metafunc.cls.image_class: raise pytest.UsageError( 'Attempting to use supported_dtype fixture outside an image test case' ) diff --git a/nibabel/viewers.py b/nibabel/viewers.py index 0dc2f0dafc..4dd8a1c258 100644 --- a/nibabel/viewers.py +++ b/nibabel/viewers.py @@ -447,7 +447,7 @@ def _set_position(self, x, y, z, notify=True): # Matplotlib handlers #################################################### def _in_axis(self, event): """Return axis index if within one of our axes, else None""" - if getattr(event, 'inaxes') is None: + if event.inaxes is None: return None for ii, ax in enumerate(self._axes): if event.inaxes is ax: From d6ea77beed3db1361c04165a054f4081cf9b8dd8 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:07:35 +0200 Subject: [PATCH 2/3] STY: Apply ruff/flake8-bugbear rule B015 B015 Pointless comparison. Did you mean to assign a value? Otherwise, prepend `assert` or remove it. --- nibabel/gifti/tests/test_parse_gifti_fast.py | 4 ++-- nibabel/tests/test_openers.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/nibabel/gifti/tests/test_parse_gifti_fast.py b/nibabel/gifti/tests/test_parse_gifti_fast.py index 8cb7c96794..6ca54df038 100644 --- a/nibabel/gifti/tests/test_parse_gifti_fast.py +++ b/nibabel/gifti/tests/test_parse_gifti_fast.py @@ -241,7 +241,7 @@ def test_load_dataarray1(): me = img.darrays[0].meta assert 'AnatomicalStructurePrimary' in me assert 'AnatomicalStructureSecondary' in me - me['AnatomicalStructurePrimary'] == 'CortexLeft' + assert me['AnatomicalStructurePrimary'] == 'CortexLeft' assert_array_almost_equal(img.darrays[0].coordsys.xform, np.eye(4, 4)) assert xform_codes.niistring[img.darrays[0].coordsys.dataspace] == 'NIFTI_XFORM_TALAIRACH' assert xform_codes.niistring[img.darrays[0].coordsys.xformspace] == 'NIFTI_XFORM_TALAIRACH' @@ -279,7 +279,7 @@ def test_load_dataarray4(): def test_dataarray5(): img5 = load(DATA_FILE5) for da in img5.darrays: - gifti_endian_codes.byteorder[da.endian] == 'little' + assert gifti_endian_codes.byteorder[da.endian] == 'little' assert_array_almost_equal(img5.darrays[0].data, DATA_FILE5_darr1) assert_array_almost_equal(img5.darrays[1].data, DATA_FILE5_darr2) # Round trip tested below diff --git a/nibabel/tests/test_openers.py b/nibabel/tests/test_openers.py index 15290d5ef9..0b58794331 100644 --- a/nibabel/tests/test_openers.py +++ b/nibabel/tests/test_openers.py @@ -431,17 +431,17 @@ def test_DeterministicGzipFile_fileobj(): with open('test.gz', 'wb') as fobj: with DeterministicGzipFile(filename='', mode='wb', fileobj=fobj) as gzobj: gzobj.write(msg) - md5sum('test.gz') == ref_chksum + assert md5sum('test.gz') == ref_chksum with open('test.gz', 'wb') as fobj: with DeterministicGzipFile(fileobj=fobj, mode='wb') as gzobj: gzobj.write(msg) - md5sum('test.gz') == ref_chksum + assert md5sum('test.gz') == ref_chksum with open('test.gz', 'wb') as fobj: with DeterministicGzipFile(filename='test.gz', mode='wb', fileobj=fobj) as gzobj: gzobj.write(msg) - md5sum('test.gz') == ref_chksum + assert md5sum('test.gz') == ref_chksum def test_bitwise_determinism(): From f064b62e8045a60065b9a6ac48670a4def46af38 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:31:14 +0200 Subject: [PATCH 3/3] STY: Enforce ruff/flake8-bugbear rules (B) --- pyproject.toml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 2840119c4f..ead2782b23 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -116,12 +116,23 @@ exclude = ["doc", "nibabel/externals", "tools", "version.py", "versioneer.py"] [tool.ruff.lint] select = [ + "B", "F", "I", "Q", "UP", ] ignore = [ + "B006", # TODO: enable + "B008", # TODO: enable + "B007", + "B011", + "B017", # TODO: enable + "B018", + "B020", + "B023", # TODO: enable + "B028", + "B904", # https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules "W191", "E111",