1111import os
1212from typing import IO , Tuple
1313
14- from typing_extensions import Self
15-
1614from docx .image .exceptions import UnrecognizedImageError
1715from docx .shared import Emu , Inches , Length , lazyproperty
1816
@@ -28,14 +26,14 @@ def __init__(self, blob: bytes, filename: str, image_header: BaseImageHeader):
2826 self ._image_header = image_header
2927
3028 @classmethod
31- def from_blob (cls , blob : bytes ) -> Self :
29+ def from_blob (cls , blob : bytes ) -> Image :
3230 """Return a new |Image| subclass instance parsed from the image binary contained
3331 in `blob`."""
3432 stream = io .BytesIO (blob )
3533 return cls ._from_stream (stream , blob )
3634
3735 @classmethod
38- def from_file (cls , image_descriptor ):
36+ def from_file (cls , image_descriptor : str | IO [ bytes ] ):
3937 """Return a new |Image| subclass instance loaded from the image file identified
4038 by `image_descriptor`, a path or file-like object."""
4139 if isinstance (image_descriptor , str ):
@@ -57,7 +55,7 @@ def blob(self):
5755 return self ._blob
5856
5957 @property
60- def content_type (self ):
58+ def content_type (self ) -> str :
6159 """MIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image."""
6260 return self ._image_header .content_type
6361
@@ -167,12 +165,11 @@ def _from_stream(
167165 return cls (blob , filename , image_header )
168166
169167
170- def _ImageHeaderFactory (stream ):
171- """Return a |BaseImageHeader| subclass instance that knows how to parse the headers
172- of the image in `stream`."""
168+ def _ImageHeaderFactory (stream : IO [bytes ]):
169+ """A |BaseImageHeader| subclass instance that can parse headers of image in `stream`."""
173170 from docx .image import SIGNATURES
174171
175- def read_32 (stream ):
172+ def read_32 (stream : IO [ bytes ] ):
176173 stream .seek (0 )
177174 return stream .read (32 )
178175
@@ -188,32 +185,27 @@ def read_32(stream):
188185class BaseImageHeader :
189186 """Base class for image header subclasses like |Jpeg| and |Tiff|."""
190187
191- def __init__ (self , px_width , px_height , horz_dpi , vert_dpi ):
188+ def __init__ (self , px_width : int , px_height : int , horz_dpi : int , vert_dpi : int ):
192189 self ._px_width = px_width
193190 self ._px_height = px_height
194191 self ._horz_dpi = horz_dpi
195192 self ._vert_dpi = vert_dpi
196193
197194 @property
198- def content_type (self ):
195+ def content_type (self ) -> str :
199196 """Abstract property definition, must be implemented by all subclasses."""
200- msg = (
201- "content_type property must be implemented by all subclasses of "
202- "BaseImageHeader"
203- )
197+ msg = "content_type property must be implemented by all subclasses of " "BaseImageHeader"
204198 raise NotImplementedError (msg )
205199
206200 @property
207- def default_ext (self ):
201+ def default_ext (self ) -> str :
208202 """Default filename extension for images of this type.
209203
210204 An abstract property definition, must be implemented by all subclasses.
211205 """
212- msg = (
213- "default_ext property must be implemented by all subclasses of "
214- "BaseImageHeader"
206+ raise NotImplementedError (
207+ "default_ext property must be implemented by all subclasses of " "BaseImageHeader"
215208 )
216- raise NotImplementedError (msg )
217209
218210 @property
219211 def px_width (self ):
0 commit comments