2121from .openers import ImageOpener
2222
2323if ty .TYPE_CHECKING :
24+ from ._typing import Self
2425 from .filename_parser import ExtensionSpec , FileSpec
2526
2627FileSniff = tuple [bytes , str ]
2728
28- ImgT = ty .TypeVar ('ImgT' , bound = 'FileBasedImage' )
29- HdrT = ty .TypeVar ('HdrT' , bound = 'FileBasedHeader' )
30-
31- StreamImgT = ty .TypeVar ('StreamImgT' , bound = 'SerializableImage' )
32-
3329
3430class ImageFileError (Exception ):
3531 pass
@@ -39,7 +35,7 @@ class FileBasedHeader:
3935 """Template class to implement header protocol"""
4036
4137 @classmethod
42- def from_header (klass : type [ HdrT ] , header : FileBasedHeader | ty .Mapping | None = None ) -> HdrT :
38+ def from_header (klass , header : FileBasedHeader | ty .Mapping | None = None ) -> Self :
4339 if header is None :
4440 return klass ()
4541 # I can't do isinstance here because it is not necessarily true
@@ -53,7 +49,7 @@ def from_header(klass: type[HdrT], header: FileBasedHeader | ty.Mapping | None =
5349 )
5450
5551 @classmethod
56- def from_fileobj (klass : type [ HdrT ] , fileobj : io .IOBase ) -> HdrT :
52+ def from_fileobj (klass , fileobj : io .IOBase ) -> Self :
5753 raise NotImplementedError
5854
5955 def write_to (self , fileobj : io .IOBase ) -> None :
@@ -65,7 +61,7 @@ def __eq__(self, other: object) -> bool:
6561 def __ne__ (self , other : object ) -> bool :
6662 return not self == other
6763
68- def copy (self : HdrT ) -> HdrT :
64+ def copy (self ) -> Self :
6965 """Copy object to independent representation
7066
7167 The copy should not be affected by any changes to the original
@@ -245,12 +241,12 @@ def set_filename(self, filename: str) -> None:
245241 self .file_map = self .__class__ .filespec_to_file_map (filename )
246242
247243 @classmethod
248- def from_filename (klass : type [ ImgT ] , filename : FileSpec ) -> ImgT :
244+ def from_filename (klass , filename : FileSpec ) -> Self :
249245 file_map = klass .filespec_to_file_map (filename )
250246 return klass .from_file_map (file_map )
251247
252248 @classmethod
253- def from_file_map (klass : type [ ImgT ] , file_map : FileMap ) -> ImgT :
249+ def from_file_map (klass , file_map : FileMap ) -> Self :
254250 raise NotImplementedError
255251
256252 @classmethod
@@ -360,7 +356,7 @@ def instance_to_filename(klass, img: FileBasedImage, filename: FileSpec) -> None
360356 img .to_filename (filename )
361357
362358 @classmethod
363- def from_image (klass : type [ ImgT ] , img : FileBasedImage ) -> ImgT :
359+ def from_image (klass , img : FileBasedImage ) -> Self :
364360 """Class method to create new instance of own class from `img`
365361
366362 Parameters
@@ -540,7 +536,7 @@ def _filemap_from_iobase(klass, io_obj: io.IOBase) -> FileMap:
540536 return klass .make_file_map ({klass .files_types [0 ][0 ]: io_obj })
541537
542538 @classmethod
543- def from_stream (klass : type [ StreamImgT ] , io_obj : io .IOBase ) -> StreamImgT :
539+ def from_stream (klass , io_obj : io .IOBase ) -> Self :
544540 """Load image from readable IO stream
545541
546542 Convert to BytesIO to enable seeking, if input stream is not seekable
@@ -567,7 +563,7 @@ def to_stream(self, io_obj: io.IOBase, **kwargs) -> None:
567563 self .to_file_map (self ._filemap_from_iobase (io_obj ), ** kwargs )
568564
569565 @classmethod
570- def from_bytes (klass : type [ StreamImgT ] , bytestring : bytes ) -> StreamImgT :
566+ def from_bytes (klass , bytestring : bytes ) -> Self :
571567 """Construct image from a byte string
572568
573569 Class method
@@ -598,9 +594,7 @@ def to_bytes(self, **kwargs) -> bytes:
598594 return bio .getvalue ()
599595
600596 @classmethod
601- def from_url (
602- klass : type [StreamImgT ], url : str | request .Request , timeout : float = 5
603- ) -> StreamImgT :
597+ def from_url (klass , url : str | request .Request , timeout : float = 5 ) -> Self :
604598 """Retrieve and load an image from a URL
605599
606600 Class method
0 commit comments