Skip to content

Commit d1930e9

Browse files
authored
Merge pull request #1351 from DimitriPapadopoulos/UP
STY: Enforce ruff/pyupgrade rules (UP)
2 parents 83eaf0b + bf3e23e commit d1930e9

File tree

12 files changed

+23
-16
lines changed

12 files changed

+23
-16
lines changed

nibabel/analyze.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,9 @@ def data_to_fileobj(self, data, fileobj, rescale=True):
515515
data = np.asanyarray(data)
516516
shape = self.get_data_shape()
517517
if data.shape != shape:
518-
raise HeaderDataError('Data should be shape (%s)' % ', '.join(str(s) for s in shape))
518+
raise HeaderDataError(
519+
'Data should be shape ({})'.format(', '.join(str(s) for s in shape))
520+
)
519521
out_dtype = self.get_data_dtype()
520522
if rescale:
521523
try:

nibabel/cmdline/dicomfs.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ def release(self, path, flags, fh):
193193
def get_opt_parser():
194194
# use module docstring for help output
195195
p = OptionParser(
196-
usage='{} [OPTIONS] <DIRECTORY CONTAINING DICOMSs> <mount point>'.format(
197-
os.path.basename(sys.argv[0])
198-
),
196+
usage=f'{os.path.basename(sys.argv[0])} [OPTIONS] <DIRECTORY CONTAINING DICOMSs> <mount point>',
199197
version='%prog ' + nib.__version__,
200198
)
201199

nibabel/cmdline/diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def display_diff(files, diff):
302302

303303
for item in value:
304304
if isinstance(item, dict):
305-
item_str = ', '.join('%s: %s' % i for i in item.items())
305+
item_str = ', '.join('{}: {}'.format(*i) for i in item.items())
306306
elif item is None:
307307
item_str = '-'
308308
else:

nibabel/cmdline/ls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def proc_file(f, opts):
112112
and (h.has_data_slope or h.has_data_intercept)
113113
and not h.get_slope_inter() in ((1.0, 0.0), (None, None))
114114
):
115-
row += ['@l*%.3g+%.3g' % h.get_slope_inter()]
115+
row += ['@l*{:.3g}+{:.3g}'.format(*h.get_slope_inter())]
116116
else:
117117
row += ['']
118118

nibabel/dft.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def __getattribute__(self, name):
231231
WHERE storage_instance = ?
232232
ORDER BY directory, name"""
233233
c.execute(query, (self.uid,))
234-
val = ['%s/%s' % tuple(row) for row in c]
234+
val = ['{}/{}'.format(*tuple(row)) for row in c]
235235
self.files = val
236236
return val
237237

nibabel/freesurfer/mghformat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,9 @@ def _write_data(self, mghfile, data, header):
570570
"""
571571
shape = header.get_data_shape()
572572
if data.shape != shape:
573-
raise HeaderDataError('Data should be shape (%s)' % ', '.join(str(s) for s in shape))
573+
raise HeaderDataError(
574+
'Data should be shape ({})'.format(', '.join(str(s) for s in shape))
575+
)
574576
offset = header.get_data_offset()
575577
out_dtype = header.get_data_dtype()
576578
array_to_file(data, mghfile, out_dtype, offset)

nibabel/gifti/gifti.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import sys
1919
import warnings
2020
from copy import copy
21-
from typing import Type, cast
21+
from typing import cast
2222

2323
import numpy as np
2424

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

603603
def __init__(
604604
self,

nibabel/nifti1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def get_sizeondisk(self):
552552
return np.sum([e.get_sizeondisk() for e in self])
553553

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

557557
def write_to(self, fileobj, byteswap):
558558
"""Write header extensions to fileobj

nibabel/spatialimages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ def set_data_dtype(self, dtype: npt.DTypeLike) -> None: ...
169169
@ty.runtime_checkable
170170
class SpatialProtocol(ty.Protocol):
171171
def get_data_dtype(self) -> np.dtype: ...
172-
def get_data_shape(self) -> ty.Tuple[int, ...]: ...
173-
def get_zooms(self) -> ty.Tuple[float, ...]: ...
172+
def get_data_shape(self) -> tuple[int, ...]: ...
173+
def get_zooms(self) -> tuple[float, ...]: ...
174174

175175

176176
class HeaderDataError(Exception):

nibabel/tests/test_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_data_path(with_nimd_env):
160160
tmpfile = pjoin(tmpdir, 'another_example.ini')
161161
with open(tmpfile, 'w') as fobj:
162162
fobj.write('[DATA]\n')
163-
fobj.write('path = %s\n' % '/path/two')
163+
fobj.write('path = {}\n'.format('/path/two'))
164164
assert get_data_path() == tst_list + ['/path/two'] + old_pth
165165

166166

nibabel/tests/test_nifti1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def test_slice_times(self):
538538
hdr.set_slice_duration(0.1)
539539
# We need a function to print out the Nones and floating point
540540
# values in a predictable way, for the tests below.
541-
_stringer = lambda val: val is not None and '%2.1f' % val or None
541+
_stringer = lambda val: val is not None and f'{val:2.1f}' or None
542542
_print_me = lambda s: list(map(_stringer, s))
543543
# The following examples are from the nifti1.h documentation.
544544
hdr['slice_code'] = slice_order_codes['sequential increasing']

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ line-length = 99
115115
exclude = ["doc", "nibabel/externals", "tools", "version.py", "versioneer.py"]
116116

117117
[tool.ruff.lint]
118-
select = ["F", "I", "Q"]
118+
select = [
119+
"F",
120+
"I",
121+
"Q",
122+
"UP",
123+
]
119124
ignore = [
120125
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
121126
"W191",

0 commit comments

Comments
 (0)