Skip to content

Commit efb3294

Browse files
committed
sty: Apply ruff fix for Self-returning methods
1 parent 71a792b commit efb3294

File tree

5 files changed

+25
-23
lines changed

5 files changed

+25
-23
lines changed

nibabel/dataobj_images.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import typing as ty
1414

1515
import numpy as np
16+
from typing_extensions import Self
1617

1718
from .deprecated import deprecate_with_version
1819
from .filebasedimages import FileBasedHeader, FileBasedImage
@@ -427,12 +428,12 @@ def ndim(self) -> int:
427428

428429
@classmethod
429430
def from_file_map(
430-
klass: type[ArrayImgT],
431+
klass,
431432
file_map: FileMap,
432433
*,
433434
mmap: bool | ty.Literal['c', 'r'] = True,
434435
keep_file_open: bool | None = None,
435-
) -> ArrayImgT:
436+
) -> Self:
436437
"""Class method to create image from mapping in ``file_map``
437438
438439
Parameters
@@ -466,12 +467,12 @@ def from_file_map(
466467

467468
@classmethod
468469
def from_filename(
469-
klass: type[ArrayImgT],
470+
klass,
470471
filename: FileSpec,
471472
*,
472473
mmap: bool | ty.Literal['c', 'r'] = True,
473474
keep_file_open: bool | None = None,
474-
) -> ArrayImgT:
475+
) -> Self:
475476
"""Class method to create image from filename `filename`
476477
477478
Parameters

nibabel/filebasedimages.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from copy import deepcopy
1616
from urllib import request
1717

18+
from typing_extensions import Self
19+
1820
from ._compression import COMPRESSION_ERRORS
1921
from .fileholders import FileHolder, FileMap
2022
from .filename_parser import TypesFilenamesError, _stringify_path, splitext_addext, types_filenames
@@ -39,7 +41,7 @@ class FileBasedHeader:
3941
"""Template class to implement header protocol"""
4042

4143
@classmethod
42-
def from_header(klass: type[HdrT], header: FileBasedHeader | ty.Mapping | None = None) -> HdrT:
44+
def from_header(klass, header: FileBasedHeader | ty.Mapping | None = None) -> Self:
4345
if header is None:
4446
return klass()
4547
# I can't do isinstance here because it is not necessarily true
@@ -53,7 +55,7 @@ def from_header(klass: type[HdrT], header: FileBasedHeader | ty.Mapping | None =
5355
)
5456

5557
@classmethod
56-
def from_fileobj(klass: type[HdrT], fileobj: io.IOBase) -> HdrT:
58+
def from_fileobj(klass, fileobj: io.IOBase) -> Self:
5759
raise NotImplementedError
5860

5961
def write_to(self, fileobj: io.IOBase) -> None:
@@ -65,7 +67,7 @@ def __eq__(self, other: object) -> bool:
6567
def __ne__(self, other: object) -> bool:
6668
return not self == other
6769

68-
def copy(self: HdrT) -> HdrT:
70+
def copy(self) -> Self:
6971
"""Copy object to independent representation
7072
7173
The copy should not be affected by any changes to the original
@@ -245,12 +247,12 @@ def set_filename(self, filename: str) -> None:
245247
self.file_map = self.__class__.filespec_to_file_map(filename)
246248

247249
@classmethod
248-
def from_filename(klass: type[ImgT], filename: FileSpec) -> ImgT:
250+
def from_filename(klass, filename: FileSpec) -> Self:
249251
file_map = klass.filespec_to_file_map(filename)
250252
return klass.from_file_map(file_map)
251253

252254
@classmethod
253-
def from_file_map(klass: type[ImgT], file_map: FileMap) -> ImgT:
255+
def from_file_map(klass, file_map: FileMap) -> Self:
254256
raise NotImplementedError
255257

256258
@classmethod
@@ -360,7 +362,7 @@ def instance_to_filename(klass, img: FileBasedImage, filename: FileSpec) -> None
360362
img.to_filename(filename)
361363

362364
@classmethod
363-
def from_image(klass: type[ImgT], img: FileBasedImage) -> ImgT:
365+
def from_image(klass, img: FileBasedImage) -> Self:
364366
"""Class method to create new instance of own class from `img`
365367
366368
Parameters
@@ -540,7 +542,7 @@ def _filemap_from_iobase(klass, io_obj: io.IOBase) -> FileMap:
540542
return klass.make_file_map({klass.files_types[0][0]: io_obj})
541543

542544
@classmethod
543-
def from_stream(klass: type[StreamImgT], io_obj: io.IOBase) -> StreamImgT:
545+
def from_stream(klass, io_obj: io.IOBase) -> Self:
544546
"""Load image from readable IO stream
545547
546548
Convert to BytesIO to enable seeking, if input stream is not seekable
@@ -567,7 +569,7 @@ def to_stream(self, io_obj: io.IOBase, **kwargs) -> None:
567569
self.to_file_map(self._filemap_from_iobase(io_obj), **kwargs)
568570

569571
@classmethod
570-
def from_bytes(klass: type[StreamImgT], bytestring: bytes) -> StreamImgT:
572+
def from_bytes(klass, bytestring: bytes) -> Self:
571573
"""Construct image from a byte string
572574
573575
Class method
@@ -598,9 +600,7 @@ def to_bytes(self, **kwargs) -> bytes:
598600
return bio.getvalue()
599601

600602
@classmethod
601-
def from_url(
602-
klass: type[StreamImgT], url: str | request.Request, timeout: float = 5
603-
) -> StreamImgT:
603+
def from_url(klass, url: str | request.Request, timeout: float = 5) -> Self:
604604
"""Retrieve and load an image from a URL
605605
606606
Class method

nibabel/gifti/gifti.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ def to_xml(self, enc='utf-8', *, mode='strict', **kwargs) -> bytes:
867867
if arr.datatype not in GIFTI_DTYPES:
868868
arr = copy(arr)
869869
# TODO: Better typing for recoders
870-
dtype = cast(np.dtype, data_type_codes.dtype[arr.datatype])
870+
dtype = cast('np.dtype', data_type_codes.dtype[arr.datatype])
871871
if np.issubdtype(dtype, np.floating):
872872
arr.datatype = data_type_codes['float32']
873873
elif np.issubdtype(dtype, np.integer):

nibabel/openers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(
6868
if filename is None:
6969
raise TypeError('Must define either fileobj or filename')
7070
# Cast because GzipFile.myfileobj has type io.FileIO while open returns ty.IO
71-
fileobj = self.myfileobj = ty.cast(io.FileIO, open(filename, modestr))
71+
fileobj = self.myfileobj = ty.cast('io.FileIO', open(filename, modestr))
7272
super().__init__(
7373
filename='',
7474
mode=modestr,

nibabel/spatialimages.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
from typing import Literal
138138

139139
import numpy as np
140+
from typing_extensions import Self
140141

141142
from .casting import sctypes_aliases
142143
from .dataobj_images import DataobjImage
@@ -203,9 +204,9 @@ def __init__(
203204

204205
@classmethod
205206
def from_header(
206-
klass: type[SpatialHdrT],
207+
klass,
207208
header: SpatialProtocol | FileBasedHeader | ty.Mapping | None = None,
208-
) -> SpatialHdrT:
209+
) -> Self:
209210
if header is None:
210211
return klass()
211212
# I can't do isinstance here because it is not necessarily true
@@ -227,7 +228,7 @@ def __eq__(self, other: object) -> bool:
227228
)
228229
return NotImplemented
229230

230-
def copy(self: SpatialHdrT) -> SpatialHdrT:
231+
def copy(self) -> Self:
231232
"""Copy object to independent representation
232233
233234
The copy should not be affected by any changes to the original
@@ -586,7 +587,7 @@ def set_data_dtype(self, dtype: npt.DTypeLike) -> None:
586587
self._header.set_data_dtype(dtype)
587588

588589
@classmethod
589-
def from_image(klass: type[SpatialImgT], img: SpatialImage | FileBasedImage) -> SpatialImgT:
590+
def from_image(klass, img: SpatialImage | FileBasedImage) -> Self:
590591
"""Class method to create new instance of own class from `img`
591592
592593
Parameters
@@ -610,7 +611,7 @@ def from_image(klass: type[SpatialImgT], img: SpatialImage | FileBasedImage) ->
610611
return super().from_image(img)
611612

612613
@property
613-
def slicer(self: SpatialImgT) -> SpatialFirstSlicer[SpatialImgT]:
614+
def slicer(self) -> SpatialFirstSlicer[Self]:
614615
"""Slicer object that returns cropped and subsampled images
615616
616617
The image is resliced in the current orientation; no rotation or
@@ -658,7 +659,7 @@ def orthoview(self) -> OrthoSlicer3D:
658659
"""
659660
return OrthoSlicer3D(self.dataobj, self.affine, title=self.get_filename())
660661

661-
def as_reoriented(self: SpatialImgT, ornt: Sequence[Sequence[int]]) -> SpatialImgT:
662+
def as_reoriented(self, ornt: Sequence[Sequence[int]]) -> Self:
662663
"""Apply an orientation change and return a new image
663664
664665
If ornt is identity transform, return the original image, unchanged

0 commit comments

Comments
 (0)