Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STY: Enforce ruff/pyupgrade rules (UP) #1351

Merged
merged 4 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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())]

Check warning on line 115 in nibabel/cmdline/ls.py

View check run for this annotation

Codecov / codecov/patch

nibabel/cmdline/ls.py#L115

Added line #L115 was not covered by tests
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
Loading