15
15
from copy import deepcopy
16
16
from urllib import request
17
17
18
+ from typing_extensions import Self
19
+
18
20
from ._compression import COMPRESSION_ERRORS
19
21
from .fileholders import FileHolder , FileMap
20
22
from .filename_parser import TypesFilenamesError , _stringify_path , splitext_addext , types_filenames
@@ -39,7 +41,7 @@ class FileBasedHeader:
39
41
"""Template class to implement header protocol"""
40
42
41
43
@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 :
43
45
if header is None :
44
46
return klass ()
45
47
# 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 =
53
55
)
54
56
55
57
@classmethod
56
- def from_fileobj (klass : type [ HdrT ] , fileobj : io .IOBase ) -> HdrT :
58
+ def from_fileobj (klass , fileobj : io .IOBase ) -> Self :
57
59
raise NotImplementedError
58
60
59
61
def write_to (self , fileobj : io .IOBase ) -> None :
@@ -65,7 +67,7 @@ def __eq__(self, other: object) -> bool:
65
67
def __ne__ (self , other : object ) -> bool :
66
68
return not self == other
67
69
68
- def copy (self : HdrT ) -> HdrT :
70
+ def copy (self ) -> Self :
69
71
"""Copy object to independent representation
70
72
71
73
The copy should not be affected by any changes to the original
@@ -245,12 +247,12 @@ def set_filename(self, filename: str) -> None:
245
247
self .file_map = self .__class__ .filespec_to_file_map (filename )
246
248
247
249
@classmethod
248
- def from_filename (klass : type [ ImgT ] , filename : FileSpec ) -> ImgT :
250
+ def from_filename (klass , filename : FileSpec ) -> Self :
249
251
file_map = klass .filespec_to_file_map (filename )
250
252
return klass .from_file_map (file_map )
251
253
252
254
@classmethod
253
- def from_file_map (klass : type [ ImgT ] , file_map : FileMap ) -> ImgT :
255
+ def from_file_map (klass , file_map : FileMap ) -> Self :
254
256
raise NotImplementedError
255
257
256
258
@classmethod
@@ -360,7 +362,7 @@ def instance_to_filename(klass, img: FileBasedImage, filename: FileSpec) -> None
360
362
img .to_filename (filename )
361
363
362
364
@classmethod
363
- def from_image (klass : type [ ImgT ] , img : FileBasedImage ) -> ImgT :
365
+ def from_image (klass , img : FileBasedImage ) -> Self :
364
366
"""Class method to create new instance of own class from `img`
365
367
366
368
Parameters
@@ -540,7 +542,7 @@ def _filemap_from_iobase(klass, io_obj: io.IOBase) -> FileMap:
540
542
return klass .make_file_map ({klass .files_types [0 ][0 ]: io_obj })
541
543
542
544
@classmethod
543
- def from_stream (klass : type [ StreamImgT ] , io_obj : io .IOBase ) -> StreamImgT :
545
+ def from_stream (klass , io_obj : io .IOBase ) -> Self :
544
546
"""Load image from readable IO stream
545
547
546
548
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:
567
569
self .to_file_map (self ._filemap_from_iobase (io_obj ), ** kwargs )
568
570
569
571
@classmethod
570
- def from_bytes (klass : type [ StreamImgT ] , bytestring : bytes ) -> StreamImgT :
572
+ def from_bytes (klass , bytestring : bytes ) -> Self :
571
573
"""Construct image from a byte string
572
574
573
575
Class method
@@ -598,9 +600,7 @@ def to_bytes(self, **kwargs) -> bytes:
598
600
return bio .getvalue ()
599
601
600
602
@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 :
604
604
"""Retrieve and load an image from a URL
605
605
606
606
Class method
0 commit comments