Skip to content

Commit

Permalink
Added type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 5, 2024
1 parent 9023b0f commit 4869c34
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,11 @@ def __setstate__(self, state: list[float | Fraction]) -> None:
__int__ = _delegate("__int__")


def _register_loader(idx: int, size: int):
def decorator(func):
_LoaderFunc = Callable[["ImageFileDirectory_v2", bytes, bool], Any]


def _register_loader(idx: int, size: int) -> Callable[[_LoaderFunc], _LoaderFunc]:
def decorator(func: _LoaderFunc) -> _LoaderFunc:
from .TiffTags import TYPES

if func.__name__.startswith("load_"):
Expand All @@ -481,12 +484,13 @@ def _register_basic(idx_fmt_name: tuple[int, str, str]) -> None:
idx, fmt, name = idx_fmt_name
TYPES[idx] = name
size = struct.calcsize(f"={fmt}")
_load_dispatch[idx] = ( # noqa: F821
size,
lambda self, data, legacy_api=True: (
self._unpack(f"{len(data) // size}{fmt}", data)
),
)

def basic_handler(
self: ImageFileDirectory_v2, data: bytes, legacy_api: bool = True
) -> tuple[Any, ...]:
return self._unpack(f"{len(data) // size}{fmt}", data)

_load_dispatch[idx] = size, basic_handler # noqa: F821
_write_dispatch[idx] = lambda self, *values: ( # noqa: F821
b"".join(self._pack(fmt, value) for value in values)
)
Expand Down Expand Up @@ -559,7 +563,7 @@ class ImageFileDirectory_v2(_IFDv2Base):
"""

_load_dispatch: dict[int, Callable[[ImageFileDirectory_v2, bytes, bool], Any]] = {}
_load_dispatch: dict[int, tuple[int, _LoaderFunc]] = {}
_write_dispatch: dict[int, Callable[..., Any]] = {}

def __init__(
Expand Down Expand Up @@ -652,10 +656,10 @@ def __getitem__(self, tag):
def __contains__(self, tag: object) -> bool:
return tag in self._tags_v2 or tag in self._tagdata

def __setitem__(self, tag: int, value) -> None:
def __setitem__(self, tag: int, value: Any) -> None:
self._setitem(tag, value, self.legacy_api)

def _setitem(self, tag: int, value, legacy_api: bool) -> None:
def _setitem(self, tag: int, value: Any, legacy_api: bool) -> None:
basetypes = (Number, bytes, str)

info = TiffTags.lookup(tag, self.group)
Expand Down Expand Up @@ -743,10 +747,10 @@ def __delitem__(self, tag: int) -> None:
def __iter__(self) -> Iterator[int]:
return iter(set(self._tagdata) | set(self._tags_v2))

def _unpack(self, fmt: str, data: bytes):
def _unpack(self, fmt: str, data: bytes) -> tuple[Any, ...]:
return struct.unpack(self._endian + fmt, data)

def _pack(self, fmt: str, *values) -> bytes:
def _pack(self, fmt: str, *values: Any) -> bytes:
return struct.pack(self._endian + fmt, *values)

list(
Expand Down Expand Up @@ -823,7 +827,9 @@ def write_undefined(self, value: bytes | int | IFDRational) -> bytes:
return value

@_register_loader(10, 8)
def load_signed_rational(self, data: bytes, legacy_api: bool = True):
def load_signed_rational(
self, data: bytes, legacy_api: bool = True
) -> tuple[tuple[int, int] | IFDRational, ...]:
vals = self._unpack(f"{len(data) // 4}l", data)

def combine(a: int, b: int) -> tuple[int, int] | IFDRational:
Expand Down Expand Up @@ -1087,7 +1093,7 @@ def __len__(self) -> int:
def __iter__(self) -> Iterator[int]:
return iter(set(self._tagdata) | set(self._tags_v1))

def __setitem__(self, tag: int, value) -> None:
def __setitem__(self, tag: int, value: Any) -> None:
for legacy_api in (False, True):
self._setitem(tag, value, legacy_api)

Expand Down

0 comments on commit 4869c34

Please sign in to comment.