From f66a57275b48b2eecd1782c7887b3450ef21ae8f Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 24 Nov 2024 19:30:34 +0100 Subject: [PATCH 1/4] MNT: Upgrade ruff to 0.8.2 --- .pre-commit-config.yaml | 2 +- pyproject.toml | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4f49318eb..66d32940c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,7 +13,7 @@ repos: - id: check-merge-conflict - id: check-vcs-permalinks - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.4 + rev: v0.8.2 hooks: - id: ruff args: [ --fix ] diff --git a/pyproject.toml b/pyproject.toml index b62c0048a..720366f53 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -151,8 +151,6 @@ ignore = [ "C416", "PERF203", "PIE790", - "PT004", # deprecated - "PT005", # deprecated "PT007", "PT011", "PT012", @@ -163,7 +161,6 @@ ignore = [ "RUF012", # TODO: enable "RUF015", "RUF017", # TODO: enable - "UP027", # deprecated "UP038", # https://github.com/astral-sh/ruff/issues/7871 # https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules "W191", From 653fd80747764862038cf6ed4a6f306459b4d0d2 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 24 Nov 2024 19:31:10 +0100 Subject: [PATCH 2/4] STY: Apply ruff rule RUF021 RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear --- nibabel/cifti2/cifti2_axes.py | 6 ++++-- nibabel/cmdline/dicomfs.py | 2 +- nibabel/tests/test_nifti1.py | 2 +- nibabel/volumeutils.py | 4 ++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/nibabel/cifti2/cifti2_axes.py b/nibabel/cifti2/cifti2_axes.py index 32914be1b..54dfc7917 100644 --- a/nibabel/cifti2/cifti2_axes.py +++ b/nibabel/cifti2/cifti2_axes.py @@ -634,8 +634,10 @@ def __eq__(self, other): return ( ( self.affine is None - or np.allclose(self.affine, other.affine) - and self.volume_shape == other.volume_shape + or ( + np.allclose(self.affine, other.affine) + and self.volume_shape == other.volume_shape + ) ) and self.nvertices == other.nvertices and np.array_equal(self.name, other.name) diff --git a/nibabel/cmdline/dicomfs.py b/nibabel/cmdline/dicomfs.py index 07aa51e2d..f84cec4a9 100644 --- a/nibabel/cmdline/dicomfs.py +++ b/nibabel/cmdline/dicomfs.py @@ -231,7 +231,7 @@ def main(args=None): if opts.verbose: logger.addHandler(logging.StreamHandler(sys.stdout)) - logger.setLevel(opts.verbose > 1 and logging.DEBUG or logging.INFO) + logger.setLevel((opts.verbose > 1 and logging.DEBUG) or logging.INFO) if len(files) != 2: sys.stderr.write(f'Please provide two arguments:\n{parser.usage}\n') diff --git a/nibabel/tests/test_nifti1.py b/nibabel/tests/test_nifti1.py index 053cad755..e387f93af 100644 --- a/nibabel/tests/test_nifti1.py +++ b/nibabel/tests/test_nifti1.py @@ -538,7 +538,7 @@ def test_slice_times(self): hdr.set_slice_duration(0.1) # We need a function to print out the Nones and floating point # values in a predictable way, for the tests below. - _stringer = lambda val: val is not None and f'{val:2.1f}' or None + _stringer = lambda val: (val is not None and f'{val:2.1f}') or None _print_me = lambda s: list(map(_stringer, s)) # The following examples are from the nifti1.h documentation. hdr['slice_code'] = slice_order_codes['sequential increasing'] diff --git a/nibabel/volumeutils.py b/nibabel/volumeutils.py index d0ebb46a7..67a3f9a58 100644 --- a/nibabel/volumeutils.py +++ b/nibabel/volumeutils.py @@ -35,8 +35,8 @@ DT = ty.TypeVar('DT', bound=np.generic) sys_is_le = sys.byteorder == 'little' -native_code = sys_is_le and '<' or '>' -swapped_code = sys_is_le and '>' or '<' +native_code = (sys_is_le and '<') or '>' +swapped_code = (sys_is_le and '>') or '<' _endian_codes = ( # numpy code, aliases ('<', 'little', 'l', 'le', 'L', 'LE'), From 655df17bca7784cf2121f07b1ee7a36b4cc48665 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 24 Nov 2024 19:31:54 +0100 Subject: [PATCH 3/4] STY: Apply ruff rule RUF023 RUF023 `__slots__` is not sorted --- nibabel/pointset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nibabel/pointset.py b/nibabel/pointset.py index 889a8c70c..759a0b15e 100644 --- a/nibabel/pointset.py +++ b/nibabel/pointset.py @@ -178,7 +178,7 @@ def to_mask(self, shape=None) -> SpatialImage: class GridIndices: """Class for generating indices just-in-time""" - __slots__ = ('gridshape', 'dtype', 'shape') + __slots__ = ('dtype', 'gridshape', 'shape') ndim = 2 def __init__(self, shape, dtype=None): From 386b6eea62fa82c7e1ddfab27bf2b3c4d2196fe2 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 24 Nov 2024 20:20:01 +0100 Subject: [PATCH 4/4] STY: Apply ruff/pyupgrade rule UP031 UP031 Use format specifiers instead of percent format --- nibabel/cmdline/utils.py | 4 ++-- nibabel/volumeutils.py | 3 +-- pyproject.toml | 1 + 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/nibabel/cmdline/utils.py b/nibabel/cmdline/utils.py index d89cc5c96..9f9df13f1 100644 --- a/nibabel/cmdline/utils.py +++ b/nibabel/cmdline/utils.py @@ -65,7 +65,7 @@ def table2string(table, out=None): markup_strip = re.compile('^@([lrc]|w.*)') col_width = [max(len(markup_strip.sub('', x)) for x in column) for column in atable.T] string = '' - for i, table_ in enumerate(table): + for table_ in table: string_ = '' for j, item in enumerate(table_): item = str(item) @@ -89,7 +89,7 @@ def table2string(table, out=None): else: raise RuntimeError(f'Should not get here with align={align}') - string_ += '%%%ds%%s%%%ds ' % (nspacesl, nspacesr) % ('', item, '') + string_ += f'{" " * nspacesl}{item}{" " * nspacesr} ' string += string_.rstrip() + '\n' out.write(string) diff --git a/nibabel/volumeutils.py b/nibabel/volumeutils.py index 67a3f9a58..03aeb440a 100644 --- a/nibabel/volumeutils.py +++ b/nibabel/volumeutils.py @@ -338,11 +338,10 @@ def pretty_mapping( if getterfunc is None: getterfunc = getitem mxlen = max(len(str(name)) for name in mapping) - fmt = '%%-%ds : %%s' % mxlen out = [] for name in mapping: value = getterfunc(mapping, name) - out.append(fmt % (name, value)) + out.append(f'{name:{mxlen}} : {value}') return '\n'.join(out) diff --git a/pyproject.toml b/pyproject.toml index 720366f53..a8ae3129b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -182,6 +182,7 @@ ignore = [ [tool.ruff.lint.per-file-ignores] "__init__.py" = ["F401"] "doc/source/conf.py" = ["F401"] +"nibabel/benchmarks/*.py" = ["UP031"] [tool.ruff.format] quote-style = "single"