diff --git a/nibabel/data.py b/nibabel/data.py index c49580d09..8ea056d8e 100644 --- a/nibabel/data.py +++ b/nibabel/data.py @@ -87,8 +87,7 @@ def list_files(self, relative=True): for base, dirs, files in os.walk(self.base_path): if relative: base = base[len(self.base_path) + 1 :] - for filename in files: - out_list.append(pjoin(base, filename)) + out_list.extend(pjoin(base, filename) for filename in files) return out_list diff --git a/nibabel/nicom/tests/test_dicomwrappers.py b/nibabel/nicom/tests/test_dicomwrappers.py index 55c27df50..db3f66751 100755 --- a/nibabel/nicom/tests/test_dicomwrappers.py +++ b/nibabel/nicom/tests/test_dicomwrappers.py @@ -429,10 +429,9 @@ def fake_shape_dependents( class PrintBase: def __repr__(self): - attr_strs = [] - for attr in dir(self): - if attr[0].isupper(): - attr_strs.append(f'{attr}={getattr(self, attr)}') + attr_strs = [ + f'{attr}={getattr(self, attr)}' for attr in dir(self) if attr[0].isupper() + ] return f"{self.__class__.__name__}({', '.join(attr_strs)})" class DimIdxSeqElem(pydicom.Dataset): diff --git a/nibabel/streamlines/tests/test_streamlines.py b/nibabel/streamlines/tests/test_streamlines.py index 359cbc5e1..8811ddcfa 100644 --- a/nibabel/streamlines/tests/test_streamlines.py +++ b/nibabel/streamlines/tests/test_streamlines.py @@ -207,7 +207,7 @@ def test_save_tractogram_file(self): def test_save_empty_file(self): tractogram = Tractogram(affine_to_rasmm=np.eye(4)) - for ext, cls in FORMATS.items(): + for ext in FORMATS: with InTemporaryDirectory(): filename = 'streamlines' + ext nib.streamlines.save(tractogram, filename) @@ -216,7 +216,7 @@ def test_save_empty_file(self): def test_save_simple_file(self): tractogram = Tractogram(DATA['streamlines'], affine_to_rasmm=np.eye(4)) - for ext, cls in FORMATS.items(): + for ext in FORMATS: with InTemporaryDirectory(): filename = 'streamlines' + ext nib.streamlines.save(tractogram, filename) @@ -262,7 +262,7 @@ def test_save_complex_file(self): def test_save_sliced_tractogram(self): tractogram = Tractogram(DATA['streamlines'], affine_to_rasmm=np.eye(4)) original_tractogram = tractogram.copy() - for ext, cls in FORMATS.items(): + for ext in FORMATS: with InTemporaryDirectory(): filename = 'streamlines' + ext nib.streamlines.save(tractogram[::2], filename) @@ -283,7 +283,7 @@ def test_save_from_generator(self): tractogram = Tractogram(DATA['streamlines'], affine_to_rasmm=np.eye(4)) # Just to create a generator - for ext, _ in FORMATS.items(): + for ext in FORMATS: filtered = (s for s in tractogram.streamlines if True) lazy_tractogram = LazyTractogram(lambda: filtered, affine_to_rasmm=np.eye(4)) diff --git a/nibabel/testing/helpers.py b/nibabel/testing/helpers.py index ae859d657..ad4bf258c 100644 --- a/nibabel/testing/helpers.py +++ b/nibabel/testing/helpers.py @@ -14,7 +14,7 @@ def bytesio_filemap(klass): """Return bytes io filemap for this image class `klass`""" file_map = klass.make_file_map() - for name, fileholder in file_map.items(): + for fileholder in file_map.values(): fileholder.fileobj = BytesIO() fileholder.pos = 0 return file_map diff --git a/nibabel/tests/test_analyze.py b/nibabel/tests/test_analyze.py index cb7b8d686..befc920f1 100644 --- a/nibabel/tests/test_analyze.py +++ b/nibabel/tests/test_analyze.py @@ -728,8 +728,8 @@ def test_data_hdr_cache(self): IC = self.image_class # save an image to a file map fm = IC.make_file_map() - for key, value in fm.items(): - fm[key].fileobj = BytesIO() + for value in fm.values(): + value.fileobj = BytesIO() shape = (2, 3, 4) data = np.arange(24, dtype=np.int8).reshape(shape) affine = np.eye(4) @@ -831,7 +831,7 @@ def test_header_updating(self): hdr = img.header hdr.set_zooms((4, 5, 6)) # Save / reload using bytes IO objects - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() hdr_back = img.from_file_map(img.file_map).header @@ -842,7 +842,7 @@ def test_header_updating(self): assert_array_equal(hdr.get_zooms(), (2, 3, 4)) # Modify affine in-place? Update on save. img.affine[0, 0] = 9 - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() hdr_back = img.from_file_map(img.file_map).header @@ -864,7 +864,7 @@ def test_pickle(self): assert_array_equal(img.get_fdata(), img2.get_fdata()) assert img.header == img2.header # Save / reload using bytes IO objects - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() img_prox = img.from_file_map(img.file_map) diff --git a/nibabel/tests/test_euler.py b/nibabel/tests/test_euler.py index 1a781b8f1..4d251a16e 100644 --- a/nibabel/tests/test_euler.py +++ b/nibabel/tests/test_euler.py @@ -21,12 +21,8 @@ FLOAT_EPS = np.finfo(np.float64).eps # Example rotations """ -eg_rots = [] params = np.arange(-pi * 2, pi * 2.5, pi / 2) -for x in params: - for y in params: - for z in params: - eg_rots.append((x, y, z)) +eg_rots = [(x, y, z) for x in params for y in params for z in params] def x_only(x): diff --git a/nibabel/tests/test_filehandles.py b/nibabel/tests/test_filehandles.py index 506a62375..c985d3544 100644 --- a/nibabel/tests/test_filehandles.py +++ b/nibabel/tests/test_filehandles.py @@ -33,8 +33,7 @@ def test_multiload(): tmpdir = mkdtemp() fname = pjoin(tmpdir, 'test.img') save(img, fname) - for i in range(N): - imgs.append(load(fname)) + imgs.extend(load(fname) for _ in range(N)) finally: del img, imgs shutil.rmtree(tmpdir) diff --git a/nibabel/tests/test_files_interface.py b/nibabel/tests/test_files_interface.py index 07e394eca..b3562b608 100644 --- a/nibabel/tests/test_files_interface.py +++ b/nibabel/tests/test_files_interface.py @@ -28,7 +28,7 @@ def test_files_spatialimages(): ] for klass in klasses: file_map = klass.make_file_map() - for key, value in file_map.items(): + for value in file_map.values(): assert value.filename is None assert value.fileobj is None assert value.pos == 0 @@ -41,7 +41,7 @@ def test_files_spatialimages(): img = klass(arr.astype(np.float32), aff) else: img = klass(arr, aff) - for key, value in img.file_map.items(): + for value in img.file_map.values(): assert value.filename is None assert value.fileobj is None assert value.pos == 0 diff --git a/nibabel/tests/test_nifti1.py b/nibabel/tests/test_nifti1.py index a3626f568..8eae0410e 100644 --- a/nibabel/tests/test_nifti1.py +++ b/nibabel/tests/test_nifti1.py @@ -820,7 +820,7 @@ def _qform_rt(self, img): hdr['qform_code'] = 3 hdr['sform_code'] = 4 # Save / reload using bytes IO objects - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() return img.from_file_map(img.file_map) diff --git a/nibabel/tests/test_spm99analyze.py b/nibabel/tests/test_spm99analyze.py index ada92d3b0..26098d8ed 100644 --- a/nibabel/tests/test_spm99analyze.py +++ b/nibabel/tests/test_spm99analyze.py @@ -423,7 +423,7 @@ def test_mat_read(self): aff = np.diag([2, 3, 4, 1]) # no LR flip in affine img = img_klass(arr, aff) fm = img.file_map - for key, value in fm.items(): + for value in fm.values(): value.fileobj = BytesIO() # Test round trip img.to_file_map() @@ -475,7 +475,7 @@ def test_none_affine(self): img = img_klass(np.zeros((2, 3, 4)), None) aff = img.header.get_best_affine() # Save / reload using bytes IO objects - for key, value in img.file_map.items(): + for value in img.file_map.values(): value.fileobj = BytesIO() img.to_file_map() img_back = img.from_file_map(img.file_map) diff --git a/pyproject.toml b/pyproject.toml index 22be5f917..748dc12ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -123,6 +123,7 @@ select = [ "FLY", "FURB", "I", + "PERF", "PIE", "PLE", "PT", @@ -146,6 +147,7 @@ ignore = [ "C401", "C408", "C416", + "PERF203", "PIE790", "PT004", "PT007",