Added type hints

This commit is contained in:
Andrew Murray 2024-06-11 23:26:00 +10:00
parent 780d85b667
commit 1eb960f7e3
16 changed files with 78 additions and 85 deletions

View File

@ -61,7 +61,9 @@ def unpack_565(i: int) -> tuple[int, int, int]:
return ((i >> 11) & 0x1F) << 3, ((i >> 5) & 0x3F) << 2, (i & 0x1F) << 3 return ((i >> 11) & 0x1F) << 3, ((i >> 5) & 0x3F) << 2, (i & 0x1F) << 3
def decode_dxt1(data, alpha=False): def decode_dxt1(
data: bytes, alpha: bool = False
) -> tuple[bytearray, bytearray, bytearray, bytearray]:
""" """
input: one "row" of data (i.e. will produce 4*width pixels) input: one "row" of data (i.e. will produce 4*width pixels)
""" """
@ -69,9 +71,9 @@ def decode_dxt1(data, alpha=False):
blocks = len(data) // 8 # number of blocks in row blocks = len(data) // 8 # number of blocks in row
ret = (bytearray(), bytearray(), bytearray(), bytearray()) ret = (bytearray(), bytearray(), bytearray(), bytearray())
for block in range(blocks): for block_index in range(blocks):
# Decode next 8-byte block. # Decode next 8-byte block.
idx = block * 8 idx = block_index * 8
color0, color1, bits = struct.unpack_from("<HHI", data, idx) color0, color1, bits = struct.unpack_from("<HHI", data, idx)
r0, g0, b0 = unpack_565(color0) r0, g0, b0 = unpack_565(color0)
@ -116,7 +118,7 @@ def decode_dxt1(data, alpha=False):
return ret return ret
def decode_dxt3(data): def decode_dxt3(data: bytes) -> tuple[bytearray, bytearray, bytearray, bytearray]:
""" """
input: one "row" of data (i.e. will produce 4*width pixels) input: one "row" of data (i.e. will produce 4*width pixels)
""" """
@ -124,8 +126,8 @@ def decode_dxt3(data):
blocks = len(data) // 16 # number of blocks in row blocks = len(data) // 16 # number of blocks in row
ret = (bytearray(), bytearray(), bytearray(), bytearray()) ret = (bytearray(), bytearray(), bytearray(), bytearray())
for block in range(blocks): for block_index in range(blocks):
idx = block * 16 idx = block_index * 16
block = data[idx : idx + 16] block = data[idx : idx + 16]
# Decode next 16-byte block. # Decode next 16-byte block.
bits = struct.unpack_from("<8B", block) bits = struct.unpack_from("<8B", block)
@ -169,7 +171,7 @@ def decode_dxt3(data):
return ret return ret
def decode_dxt5(data): def decode_dxt5(data: bytes) -> tuple[bytearray, bytearray, bytearray, bytearray]:
""" """
input: one "row" of data (i.e. will produce 4 * width pixels) input: one "row" of data (i.e. will produce 4 * width pixels)
""" """
@ -177,8 +179,8 @@ def decode_dxt5(data):
blocks = len(data) // 16 # number of blocks in row blocks = len(data) // 16 # number of blocks in row
ret = (bytearray(), bytearray(), bytearray(), bytearray()) ret = (bytearray(), bytearray(), bytearray(), bytearray())
for block in range(blocks): for block_index in range(blocks):
idx = block * 16 idx = block_index * 16
block = data[idx : idx + 16] block = data[idx : idx + 16]
# Decode next 16-byte block. # Decode next 16-byte block.
a0, a1 = struct.unpack_from("<BB", block) a0, a1 = struct.unpack_from("<BB", block)

View File

@ -301,7 +301,8 @@ class BmpImageFile(ImageFile.ImageFile):
class BmpRleDecoder(ImageFile.PyDecoder): class BmpRleDecoder(ImageFile.PyDecoder):
_pulls_fd = True _pulls_fd = True
def decode(self, buffer): def decode(self, buffer: bytes) -> tuple[int, int]:
assert self.fd is not None
rle4 = self.args[1] rle4 = self.args[1]
data = bytearray() data = bytearray()
x = 0 x = 0

View File

@ -480,7 +480,8 @@ class DdsImageFile(ImageFile.ImageFile):
class DdsRgbDecoder(ImageFile.PyDecoder): class DdsRgbDecoder(ImageFile.PyDecoder):
_pulls_fd = True _pulls_fd = True
def decode(self, buffer): def decode(self, buffer: bytes) -> tuple[int, int]:
assert self.fd is not None
bitcount, masks = self.args bitcount, masks = self.args
# Some masks will be padded with zeros, e.g. R 0b11 G 0b1100 # Some masks will be padded with zeros, e.g. R 0b11 G 0b1100

View File

@ -27,6 +27,7 @@ import re
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
from typing import IO
from . import Image, ImageFile from . import Image, ImageFile
from ._binary import i32le as i32 from ._binary import i32le as i32
@ -236,7 +237,7 @@ class EpsImageFile(ImageFile.ImageFile):
msg = 'EPS header missing "%%BoundingBox" comment' msg = 'EPS header missing "%%BoundingBox" comment'
raise SyntaxError(msg) raise SyntaxError(msg)
def _read_comment(s): def _read_comment(s: str) -> bool:
nonlocal reading_trailer_comments nonlocal reading_trailer_comments
try: try:
m = split.match(s) m = split.match(s)
@ -244,27 +245,25 @@ class EpsImageFile(ImageFile.ImageFile):
msg = "not an EPS file" msg = "not an EPS file"
raise SyntaxError(msg) from e raise SyntaxError(msg) from e
if m: if not m:
k, v = m.group(1, 2) return False
self.info[k] = v
if k == "BoundingBox": k, v = m.group(1, 2)
if v == "(atend)": self.info[k] = v
reading_trailer_comments = True if k == "BoundingBox":
elif not self._size or ( if v == "(atend)":
trailer_reached and reading_trailer_comments reading_trailer_comments = True
): elif not self._size or (trailer_reached and reading_trailer_comments):
try: try:
# Note: The DSC spec says that BoundingBox # Note: The DSC spec says that BoundingBox
# fields should be integers, but some drivers # fields should be integers, but some drivers
# put floating point values there anyway. # put floating point values there anyway.
box = [int(float(i)) for i in v.split()] box = [int(float(i)) for i in v.split()]
self._size = box[2] - box[0], box[3] - box[1] self._size = box[2] - box[0], box[3] - box[1]
self.tile = [ self.tile = [("eps", (0, 0) + self.size, offset, (length, box))]
("eps", (0, 0) + self.size, offset, (length, box)) except Exception:
] pass
except Exception: return True
pass
return True
while True: while True:
byte = self.fp.read(1) byte = self.fp.read(1)
@ -413,7 +412,7 @@ class EpsImageFile(ImageFile.ImageFile):
# -------------------------------------------------------------------- # --------------------------------------------------------------------
def _save(im, fp, filename, eps=1): def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes, eps: int = 1) -> None:
"""EPS Writer for the Python Imaging Library.""" """EPS Writer for the Python Imaging Library."""
# make sure image data is available # make sure image data is available

View File

@ -122,7 +122,7 @@ class FitsImageFile(ImageFile.ImageFile):
class FitsGzipDecoder(ImageFile.PyDecoder): class FitsGzipDecoder(ImageFile.PyDecoder):
_pulls_fd = True _pulls_fd = True
def decode(self, buffer): def decode(self, buffer: bytes) -> tuple[int, int]:
assert self.fd is not None assert self.fd is not None
value = gzip.decompress(self.fd.read()) value = gzip.decompress(self.fd.read())

View File

@ -241,7 +241,7 @@ class FpxImageFile(ImageFile.ImageFile):
self.ole.close() self.ole.close()
super().close() super().close()
def __exit__(self, *args): def __exit__(self, *args: object) -> None:
self.ole.close() self.ole.close()
super().__exit__() super().__exit__()

View File

@ -487,7 +487,7 @@ class Parser:
def __enter__(self): def __enter__(self):
return self return self
def __exit__(self, *args): def __exit__(self, *args: object) -> None:
self.close() self.close()
def close(self): def close(self):

View File

@ -18,7 +18,7 @@ from __future__ import annotations
import io import io
import os import os
import struct import struct
from typing import IO from typing import IO, Tuple, cast
from . import Image, ImageFile, ImagePalette, _binary from . import Image, ImageFile, ImagePalette, _binary
@ -59,7 +59,7 @@ class BoxReader:
self.remaining_in_box -= num_bytes self.remaining_in_box -= num_bytes
return data return data
def read_fields(self, field_format): def read_fields(self, field_format: str) -> tuple[int | bytes, ...]:
size = struct.calcsize(field_format) size = struct.calcsize(field_format)
data = self._read_bytes(size) data = self._read_bytes(size)
return struct.unpack(field_format, data) return struct.unpack(field_format, data)
@ -82,9 +82,9 @@ class BoxReader:
self.remaining_in_box = -1 self.remaining_in_box = -1
# Read the length and type of the next box # Read the length and type of the next box
lbox, tbox = self.read_fields(">I4s") lbox, tbox = cast(Tuple[int, bytes], self.read_fields(">I4s"))
if lbox == 1: if lbox == 1:
lbox = self.read_fields(">Q")[0] lbox = cast(int, self.read_fields(">Q")[0])
hlen = 16 hlen = 16
else: else:
hlen = 8 hlen = 8
@ -127,12 +127,13 @@ def _parse_codestream(fp):
return size, mode return size, mode
def _res_to_dpi(num, denom, exp): def _res_to_dpi(num: int, denom: int, exp: int) -> float | None:
"""Convert JPEG2000's (numerator, denominator, exponent-base-10) resolution, """Convert JPEG2000's (numerator, denominator, exponent-base-10) resolution,
calculated as (num / denom) * 10^exp and stored in dots per meter, calculated as (num / denom) * 10^exp and stored in dots per meter,
to floating-point dots per inch.""" to floating-point dots per inch."""
if denom != 0: if denom == 0:
return (254 * num * (10**exp)) / (10000 * denom) return None
return (254 * num * (10**exp)) / (10000 * denom)
def _parse_jp2_header(fp): def _parse_jp2_header(fp):

View File

@ -93,7 +93,7 @@ class MicImageFile(TiffImagePlugin.TiffImageFile):
self.ole.close() self.ole.close()
super().close() super().close()
def __exit__(self, *args): def __exit__(self, *args: object) -> None:
self.__fp.close() self.__fp.close()
self.ole.close() self.ole.close()
super().__exit__() super().__exit__()

View File

@ -37,19 +37,14 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
JpegImagePlugin._save(im, fp, filename) JpegImagePlugin._save(im, fp, filename)
def _save_all(im, fp, filename): def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
append_images = im.encoderinfo.get("append_images", []) append_images = im.encoderinfo.get("append_images", [])
if not append_images: if not append_images and not getattr(im, "is_animated", False):
try: _save(im, fp, filename)
animated = im.is_animated return
except AttributeError:
animated = False
if not animated:
_save(im, fp, filename)
return
mpf_offset = 28 mpf_offset = 28
offsets = [] offsets: list[int] = []
for imSequence in itertools.chain([im], append_images): for imSequence in itertools.chain([im], append_images):
for im_frame in ImageSequence.Iterator(imSequence): for im_frame in ImageSequence.Iterator(imSequence):
if not offsets: if not offsets:

View File

@ -138,7 +138,7 @@ class PSDraw:
sx = x / im.size[0] sx = x / im.size[0]
sy = y / im.size[1] sy = y / im.size[1]
self.fp.write(b"%f %f scale\n" % (sx, sy)) self.fp.write(b"%f %f scale\n" % (sx, sy))
EpsImagePlugin._save(im, self.fp, None, 0) EpsImagePlugin._save(im, self.fp, "", 0)
self.fp.write(b"\ngrestore\n") self.fp.write(b"\ngrestore\n")

View File

@ -8,6 +8,8 @@
## ##
from __future__ import annotations from __future__ import annotations
from typing import IO
from . import Image, ImageFile from . import Image, ImageFile
from ._binary import o8 from ._binary import o8
from ._binary import o16be as o16b from ._binary import o16be as o16b
@ -82,10 +84,10 @@ _Palm8BitColormapValues = (
# so build a prototype image to be used for palette resampling # so build a prototype image to be used for palette resampling
def build_prototype_image(): def build_prototype_image() -> Image.Image:
image = Image.new("L", (1, len(_Palm8BitColormapValues))) image = Image.new("L", (1, len(_Palm8BitColormapValues)))
image.putdata(list(range(len(_Palm8BitColormapValues)))) image.putdata(list(range(len(_Palm8BitColormapValues))))
palettedata = () palettedata: tuple[int, ...] = ()
for colormapValue in _Palm8BitColormapValues: for colormapValue in _Palm8BitColormapValues:
palettedata += colormapValue palettedata += colormapValue
palettedata += (0, 0, 0) * (256 - len(_Palm8BitColormapValues)) palettedata += (0, 0, 0) * (256 - len(_Palm8BitColormapValues))
@ -112,7 +114,7 @@ _COMPRESSION_TYPES = {"none": 0xFF, "rle": 0x01, "scanline": 0x00}
# (Internal) Image save plugin for the Palm format. # (Internal) Image save plugin for the Palm format.
def _save(im, fp, filename): def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
if im.mode == "P": if im.mode == "P":
# we assume this is a color Palm image with the standard colormap, # we assume this is a color Palm image with the standard colormap,
# unless the "info" dict has a "custom-colormap" field # unless the "info" dict has a "custom-colormap" field
@ -141,7 +143,7 @@ def _save(im, fp, filename):
raise OSError(msg) raise OSError(msg)
# we ignore the palette here # we ignore the palette here
im.mode = "P" im._mode = "P"
rawmode = f"P;{bpp}" rawmode = f"P;{bpp}"
version = 1 version = 1

View File

@ -404,9 +404,8 @@ class PdfParser:
def __enter__(self) -> PdfParser: def __enter__(self) -> PdfParser:
return self return self
def __exit__(self, exc_type, exc_value, traceback): def __exit__(self, *args: object) -> None:
self.close() self.close()
return False # do not suppress exceptions
def start_writing(self) -> None: def start_writing(self) -> None:
self.close_buf() self.close_buf()

View File

@ -178,7 +178,7 @@ class ChunkStream:
def __enter__(self) -> ChunkStream: def __enter__(self) -> ChunkStream:
return self return self
def __exit__(self, *args): def __exit__(self, *args: object) -> None:
self.close() self.close()
def close(self) -> None: def close(self) -> None:

View File

@ -16,7 +16,6 @@
from __future__ import annotations from __future__ import annotations
import io import io
from types import TracebackType
from . import ContainerIO from . import ContainerIO
@ -61,12 +60,7 @@ class TarIO(ContainerIO.ContainerIO[bytes]):
def __enter__(self) -> TarIO: def __enter__(self) -> TarIO:
return self return self
def __exit__( def __exit__(self, *args: object) -> None:
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
self.close() self.close()
def close(self) -> None: def close(self) -> None:

View File

@ -717,7 +717,7 @@ class ImageFileDirectory_v2(_IFDv2Base):
# Unspec'd, and length > 1 # Unspec'd, and length > 1
dest[tag] = values dest[tag] = values
def __delitem__(self, tag): def __delitem__(self, tag: int) -> None:
self._tags_v2.pop(tag, None) self._tags_v2.pop(tag, None)
self._tags_v1.pop(tag, None) self._tags_v1.pop(tag, None)
self._tagdata.pop(tag, None) self._tagdata.pop(tag, None)
@ -1106,7 +1106,7 @@ class TiffImageFile(ImageFile.ImageFile):
super().__init__(fp, filename) super().__init__(fp, filename)
def _open(self): def _open(self) -> None:
"""Open the first image in a TIFF file""" """Open the first image in a TIFF file"""
# Header # Header
@ -1123,8 +1123,8 @@ class TiffImageFile(ImageFile.ImageFile):
self.__first = self.__next = self.tag_v2.next self.__first = self.__next = self.tag_v2.next
self.__frame = -1 self.__frame = -1
self._fp = self.fp self._fp = self.fp
self._frame_pos = [] self._frame_pos: list[int] = []
self._n_frames = None self._n_frames: int | None = None
logger.debug("*** TiffImageFile._open ***") logger.debug("*** TiffImageFile._open ***")
logger.debug("- __first: %s", self.__first) logger.debug("- __first: %s", self.__first)
@ -1998,10 +1998,9 @@ class AppendingTiffWriter:
def __enter__(self) -> AppendingTiffWriter: def __enter__(self) -> AppendingTiffWriter:
return self return self
def __exit__(self, exc_type, exc_value, traceback): def __exit__(self, *args: object) -> None:
if self.close_fp: if self.close_fp:
self.close() self.close()
return False
def tell(self) -> int: def tell(self) -> int:
return self.f.tell() - self.offsetOfNewPage return self.f.tell() - self.offsetOfNewPage
@ -2043,42 +2042,42 @@ class AppendingTiffWriter:
def write(self, data): def write(self, data):
return self.f.write(data) return self.f.write(data)
def readShort(self): def readShort(self) -> int:
(value,) = struct.unpack(self.shortFmt, self.f.read(2)) (value,) = struct.unpack(self.shortFmt, self.f.read(2))
return value return value
def readLong(self): def readLong(self) -> int:
(value,) = struct.unpack(self.longFmt, self.f.read(4)) (value,) = struct.unpack(self.longFmt, self.f.read(4))
return value return value
def rewriteLastShortToLong(self, value): def rewriteLastShortToLong(self, value: int) -> None:
self.f.seek(-2, os.SEEK_CUR) self.f.seek(-2, os.SEEK_CUR)
bytes_written = self.f.write(struct.pack(self.longFmt, value)) bytes_written = self.f.write(struct.pack(self.longFmt, value))
if bytes_written is not None and bytes_written != 4: if bytes_written is not None and bytes_written != 4:
msg = f"wrote only {bytes_written} bytes but wanted 4" msg = f"wrote only {bytes_written} bytes but wanted 4"
raise RuntimeError(msg) raise RuntimeError(msg)
def rewriteLastShort(self, value): def rewriteLastShort(self, value: int) -> None:
self.f.seek(-2, os.SEEK_CUR) self.f.seek(-2, os.SEEK_CUR)
bytes_written = self.f.write(struct.pack(self.shortFmt, value)) bytes_written = self.f.write(struct.pack(self.shortFmt, value))
if bytes_written is not None and bytes_written != 2: if bytes_written is not None and bytes_written != 2:
msg = f"wrote only {bytes_written} bytes but wanted 2" msg = f"wrote only {bytes_written} bytes but wanted 2"
raise RuntimeError(msg) raise RuntimeError(msg)
def rewriteLastLong(self, value): def rewriteLastLong(self, value: int) -> None:
self.f.seek(-4, os.SEEK_CUR) self.f.seek(-4, os.SEEK_CUR)
bytes_written = self.f.write(struct.pack(self.longFmt, value)) bytes_written = self.f.write(struct.pack(self.longFmt, value))
if bytes_written is not None and bytes_written != 4: if bytes_written is not None and bytes_written != 4:
msg = f"wrote only {bytes_written} bytes but wanted 4" msg = f"wrote only {bytes_written} bytes but wanted 4"
raise RuntimeError(msg) raise RuntimeError(msg)
def writeShort(self, value): def writeShort(self, value: int) -> None:
bytes_written = self.f.write(struct.pack(self.shortFmt, value)) bytes_written = self.f.write(struct.pack(self.shortFmt, value))
if bytes_written is not None and bytes_written != 2: if bytes_written is not None and bytes_written != 2:
msg = f"wrote only {bytes_written} bytes but wanted 2" msg = f"wrote only {bytes_written} bytes but wanted 2"
raise RuntimeError(msg) raise RuntimeError(msg)
def writeLong(self, value): def writeLong(self, value: int) -> None:
bytes_written = self.f.write(struct.pack(self.longFmt, value)) bytes_written = self.f.write(struct.pack(self.longFmt, value))
if bytes_written is not None and bytes_written != 4: if bytes_written is not None and bytes_written != 4:
msg = f"wrote only {bytes_written} bytes but wanted 4" msg = f"wrote only {bytes_written} bytes but wanted 4"
@ -2097,9 +2096,9 @@ class AppendingTiffWriter:
field_size = self.fieldSizes[field_type] field_size = self.fieldSizes[field_type]
total_size = field_size * count total_size = field_size * count
is_local = total_size <= 4 is_local = total_size <= 4
offset: int | None
if not is_local: if not is_local:
offset = self.readLong() offset = self.readLong() + self.offsetOfNewPage
offset += self.offsetOfNewPage
self.rewriteLastLong(offset) self.rewriteLastLong(offset)
if tag in self.Tags: if tag in self.Tags: