Skip to content

Commit

Permalink
Merge pull request #1351 from DimitriPapadopoulos/UP
Browse files Browse the repository at this point in the history
STY: Enforce ruff/pyupgrade rules (UP)
  • Loading branch information
effigies authored Sep 22, 2024
2 parents 83eaf0b + bf3e23e commit d1930e9
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 16 deletions.
4 changes: 3 additions & 1 deletion nibabel/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,9 @@ def data_to_fileobj(self, data, fileobj, rescale=True):
data = np.asanyarray(data)
shape = self.get_data_shape()
if data.shape != shape:
raise HeaderDataError('Data should be shape (%s)' % ', '.join(str(s) for s in shape))
raise HeaderDataError(
'Data should be shape ({})'.format(', '.join(str(s) for s in shape))
)
out_dtype = self.get_data_dtype()
if rescale:
try:
Expand Down
4 changes: 1 addition & 3 deletions nibabel/cmdline/dicomfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ def release(self, path, flags, fh):
def get_opt_parser():
# use module docstring for help output
p = OptionParser(
usage='{} [OPTIONS] <DIRECTORY CONTAINING DICOMSs> <mount point>'.format(
os.path.basename(sys.argv[0])
),
usage=f'{os.path.basename(sys.argv[0])} [OPTIONS] <DIRECTORY CONTAINING DICOMSs> <mount point>',
version='%prog ' + nib.__version__,
)

Expand Down
2 changes: 1 addition & 1 deletion nibabel/cmdline/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def display_diff(files, diff):

for item in value:
if isinstance(item, dict):
item_str = ', '.join('%s: %s' % i for i in item.items())
item_str = ', '.join('{}: {}'.format(*i) for i in item.items())
elif item is None:
item_str = '-'
else:
Expand Down
2 changes: 1 addition & 1 deletion nibabel/cmdline/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def proc_file(f, opts):
and (h.has_data_slope or h.has_data_intercept)
and not h.get_slope_inter() in ((1.0, 0.0), (None, None))
):
row += ['@l*%.3g+%.3g' % h.get_slope_inter()]
row += ['@l*{:.3g}+{:.3g}'.format(*h.get_slope_inter())]
else:
row += ['']

Expand Down
2 changes: 1 addition & 1 deletion nibabel/dft.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def __getattribute__(self, name):
WHERE storage_instance = ?
ORDER BY directory, name"""
c.execute(query, (self.uid,))
val = ['%s/%s' % tuple(row) for row in c]
val = ['{}/{}'.format(*tuple(row)) for row in c]
self.files = val
return val

Expand Down
4 changes: 3 additions & 1 deletion nibabel/freesurfer/mghformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,9 @@ def _write_data(self, mghfile, data, header):
"""
shape = header.get_data_shape()
if data.shape != shape:
raise HeaderDataError('Data should be shape (%s)' % ', '.join(str(s) for s in shape))
raise HeaderDataError(
'Data should be shape ({})'.format(', '.join(str(s) for s in shape))
)
offset = header.get_data_offset()
out_dtype = header.get_data_dtype()
array_to_file(data, mghfile, out_dtype, offset)
Expand Down
4 changes: 2 additions & 2 deletions nibabel/gifti/gifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import sys
import warnings
from copy import copy
from typing import Type, cast
from typing import cast

import numpy as np

Expand Down Expand Up @@ -598,7 +598,7 @@ class GiftiImage(xml.XmlSerializable, SerializableImage):
# The parser will in due course be a GiftiImageParser, but we can't set
# that now, because it would result in a circular import. We set it after
# the class has been defined, at the end of the class definition.
parser: Type[xml.XmlParser]
parser: type[xml.XmlParser]

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion nibabel/nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def get_sizeondisk(self):
return np.sum([e.get_sizeondisk() for e in self])

def __repr__(self):
return 'Nifti1Extensions(%s)' % ', '.join(str(e) for e in self)
return 'Nifti1Extensions({})'.format(', '.join(str(e) for e in self))

def write_to(self, fileobj, byteswap):
"""Write header extensions to fileobj
Expand Down
4 changes: 2 additions & 2 deletions nibabel/spatialimages.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ def set_data_dtype(self, dtype: npt.DTypeLike) -> None: ...
@ty.runtime_checkable
class SpatialProtocol(ty.Protocol):
def get_data_dtype(self) -> np.dtype: ...
def get_data_shape(self) -> ty.Tuple[int, ...]: ...
def get_zooms(self) -> ty.Tuple[float, ...]: ...
def get_data_shape(self) -> tuple[int, ...]: ...
def get_zooms(self) -> tuple[float, ...]: ...


class HeaderDataError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_data_path(with_nimd_env):
tmpfile = pjoin(tmpdir, 'another_example.ini')
with open(tmpfile, 'w') as fobj:
fobj.write('[DATA]\n')
fobj.write('path = %s\n' % '/path/two')
fobj.write('path = {}\n'.format('/path/two'))
assert get_data_path() == tst_list + ['/path/two'] + old_pth


Expand Down
2 changes: 1 addition & 1 deletion nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '%2.1f' % val 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']
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ line-length = 99
exclude = ["doc", "nibabel/externals", "tools", "version.py", "versioneer.py"]

[tool.ruff.lint]
select = ["F", "I", "Q"]
select = [
"F",
"I",
"Q",
"UP",
]
ignore = [
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"W191",
Expand Down

0 comments on commit d1930e9

Please sign in to comment.