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
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)
"""
@ -69,9 +71,9 @@ def decode_dxt1(data, alpha=False):
blocks = len(data) // 8 # number of blocks in row
ret = (bytearray(), bytearray(), bytearray(), bytearray())
for block in range(blocks):
for block_index in range(blocks):
# Decode next 8-byte block.
idx = block * 8
idx = block_index * 8
color0, color1, bits = struct.unpack_from("<HHI", data, idx)
r0, g0, b0 = unpack_565(color0)
@ -116,7 +118,7 @@ def decode_dxt1(data, alpha=False):
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)
"""
@ -124,8 +126,8 @@ def decode_dxt3(data):
blocks = len(data) // 16 # number of blocks in row
ret = (bytearray(), bytearray(), bytearray(), bytearray())
for block in range(blocks):
idx = block * 16
for block_index in range(blocks):
idx = block_index * 16
block = data[idx : idx + 16]
# Decode next 16-byte block.
bits = struct.unpack_from("<8B", block)
@ -169,7 +171,7 @@ def decode_dxt3(data):
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)
"""
@ -177,8 +179,8 @@ def decode_dxt5(data):
blocks = len(data) // 16 # number of blocks in row
ret = (bytearray(), bytearray(), bytearray(), bytearray())
for block in range(blocks):
idx = block * 16
for block_index in range(blocks):
idx = block_index * 16
block = data[idx : idx + 16]
# Decode next 16-byte block.
a0, a1 = struct.unpack_from("<BB", block)

View File

@ -301,7 +301,8 @@ class BmpImageFile(ImageFile.ImageFile):
class BmpRleDecoder(ImageFile.PyDecoder):
_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]
data = bytearray()
x = 0

View File

@ -480,7 +480,8 @@ class DdsImageFile(ImageFile.ImageFile):
class DdsRgbDecoder(ImageFile.PyDecoder):
_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
# Some masks will be padded with zeros, e.g. R 0b11 G 0b1100

View File

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

View File

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

View File

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

View File

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

View File

@ -18,7 +18,7 @@ from __future__ import annotations
import io
import os
import struct
from typing import IO
from typing import IO, Tuple, cast
from . import Image, ImageFile, ImagePalette, _binary
@ -59,7 +59,7 @@ class BoxReader:
self.remaining_in_box -= num_bytes
return data
def read_fields(self, field_format):
def read_fields(self, field_format: str) -> tuple[int | bytes, ...]:
size = struct.calcsize(field_format)
data = self._read_bytes(size)
return struct.unpack(field_format, data)
@ -82,9 +82,9 @@ class BoxReader:
self.remaining_in_box = -1
# 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:
lbox = self.read_fields(">Q")[0]
lbox = cast(int, self.read_fields(">Q")[0])
hlen = 16
else:
hlen = 8
@ -127,11 +127,12 @@ def _parse_codestream(fp):
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,
calculated as (num / denom) * 10^exp and stored in dots per meter,
to floating-point dots per inch."""
if denom != 0:
if denom == 0:
return None
return (254 * num * (10**exp)) / (10000 * denom)

View File

@ -93,7 +93,7 @@ class MicImageFile(TiffImagePlugin.TiffImageFile):
self.ole.close()
super().close()
def __exit__(self, *args):
def __exit__(self, *args: object) -> None:
self.__fp.close()
self.ole.close()
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)
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", [])
if not append_images:
try:
animated = im.is_animated
except AttributeError:
animated = False
if not animated:
if not append_images and not getattr(im, "is_animated", False):
_save(im, fp, filename)
return
mpf_offset = 28
offsets = []
offsets: list[int] = []
for imSequence in itertools.chain([im], append_images):
for im_frame in ImageSequence.Iterator(imSequence):
if not offsets:

View File

@ -138,7 +138,7 @@ class PSDraw:
sx = x / im.size[0]
sy = y / im.size[1]
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")

View File

@ -8,6 +8,8 @@
##
from __future__ import annotations
from typing import IO
from . import Image, ImageFile
from ._binary import o8
from ._binary import o16be as o16b
@ -82,10 +84,10 @@ _Palm8BitColormapValues = (
# 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.putdata(list(range(len(_Palm8BitColormapValues))))
palettedata = ()
palettedata: tuple[int, ...] = ()
for colormapValue in _Palm8BitColormapValues:
palettedata += colormapValue
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.
def _save(im, fp, filename):
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
if im.mode == "P":
# we assume this is a color Palm image with the standard colormap,
# unless the "info" dict has a "custom-colormap" field
@ -141,7 +143,7 @@ def _save(im, fp, filename):
raise OSError(msg)
# we ignore the palette here
im.mode = "P"
im._mode = "P"
rawmode = f"P;{bpp}"
version = 1

View File

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

View File

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

View File

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

View File

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