1515from copy import deepcopy
1616from urllib import request
1717
18+ from typing_extensions import Self
19+
1820from ._compression import COMPRESSION_ERRORS
1921from .fileholders import FileHolder , FileMap
2022from .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
0 commit comments