Added return type to ImageFile.load()

This commit is contained in:
Andrew Murray 2024-08-05 13:32:59 +10:00
parent b14142462e
commit 8737709781
2 changed files with 11 additions and 16 deletions

View File

@ -218,9 +218,10 @@ if hasattr(core, "DEFAULT_STRATEGY"):
# Registries # Registries
if TYPE_CHECKING: if TYPE_CHECKING:
import mmap
from xml.etree.ElementTree import Element from xml.etree.ElementTree import Element
from . import ImageFile, ImagePalette, TiffImagePlugin from . import ImageFile, ImageFilter, ImagePalette, TiffImagePlugin
from ._typing import NumpyArray, StrOrBytesPath, TypeGuard from ._typing import NumpyArray, StrOrBytesPath, TypeGuard
ID: list[str] = [] ID: list[str] = []
OPEN: dict[ OPEN: dict[
@ -612,7 +613,7 @@ class Image:
logger.debug("Error closing: %s", msg) logger.debug("Error closing: %s", msg)
if getattr(self, "map", None): if getattr(self, "map", None):
self.map = None self.map: mmap.mmap | None = None
# Instead of simply setting to None, we're setting up a # Instead of simply setting to None, we're setting up a
# deferred error that will better explain that the core image # deferred error that will better explain that the core image
@ -1336,9 +1337,6 @@ class Image:
self.load() self.load()
return self._new(self.im.expand(xmargin, ymargin)) return self._new(self.im.expand(xmargin, ymargin))
if TYPE_CHECKING:
from . import ImageFilter
def filter(self, filter: ImageFilter.Filter | type[ImageFilter.Filter]) -> Image: def filter(self, filter: ImageFilter.Filter | type[ImageFilter.Filter]) -> Image:
""" """
Filters this image using the given filter. For a list of Filters this image using the given filter. For a list of

View File

@ -174,7 +174,7 @@ class ImageFile(Image.Image):
self.fp.close() self.fp.close()
self.fp = None self.fp = None
def load(self): def load(self) -> Image.core.PixelAccess | None:
"""Load image data based on tile list""" """Load image data based on tile list"""
if self.tile is None: if self.tile is None:
@ -185,7 +185,7 @@ class ImageFile(Image.Image):
if not self.tile: if not self.tile:
return pixel return pixel
self.map = None self.map: mmap.mmap | None = None
use_mmap = self.filename and len(self.tile) == 1 use_mmap = self.filename and len(self.tile) == 1
# As of pypy 2.1.0, memory mapping was failing here. # As of pypy 2.1.0, memory mapping was failing here.
use_mmap = use_mmap and not hasattr(sys, "pypy_version_info") use_mmap = use_mmap and not hasattr(sys, "pypy_version_info")
@ -193,17 +193,17 @@ class ImageFile(Image.Image):
readonly = 0 readonly = 0
# look for read/seek overrides # look for read/seek overrides
try: if hasattr(self, "load_read"):
read = self.load_read read = self.load_read
# don't use mmap if there are custom read/seek functions # don't use mmap if there are custom read/seek functions
use_mmap = False use_mmap = False
except AttributeError: else:
read = self.fp.read read = self.fp.read
try: if hasattr(self, "load_seek"):
seek = self.load_seek seek = self.load_seek
use_mmap = False use_mmap = False
except AttributeError: else:
seek = self.fp.seek seek = self.fp.seek
if use_mmap: if use_mmap:
@ -243,11 +243,8 @@ class ImageFile(Image.Image):
# sort tiles in file order # sort tiles in file order
self.tile.sort(key=_tilesort) self.tile.sort(key=_tilesort)
try:
# FIXME: This is a hack to handle TIFF's JpegTables tag. # FIXME: This is a hack to handle TIFF's JpegTables tag.
prefix = self.tile_prefix prefix = getattr(self, "tile_prefix", b"")
except AttributeError:
prefix = b""
# Remove consecutive duplicates that only differ by their offset # Remove consecutive duplicates that only differ by their offset
self.tile = [ self.tile = [