From 4da1e490368109658cbd1c367cad016b59a7293c Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 1 Jan 2024 19:22:01 +1100 Subject: [PATCH 1/3] Added type hints --- src/PIL/Image.py | 2 +- src/PIL/ImageTransform.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 055dbda7a03..065b22dc9cd 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2643,7 +2643,7 @@ def transform( resample=Resampling.NEAREST, fill=1, fillcolor=None, - ): + ) -> Image: """ Transforms this image. This method creates a new image with the given size, and the same mode as the original, and copies data diff --git a/src/PIL/ImageTransform.py b/src/PIL/ImageTransform.py index 1fdaa9140ff..1dc3e210e88 100644 --- a/src/PIL/ImageTransform.py +++ b/src/PIL/ImageTransform.py @@ -14,17 +14,26 @@ # from __future__ import annotations +from typing import Sequence + from . import Image class Transform(Image.ImageTransformHandler): - def __init__(self, data): + method: int + + def __init__(self, data: Sequence[int]) -> None: self.data = data - def getdata(self): + def getdata(self) -> tuple[int, Sequence[int]]: return self.method, self.data - def transform(self, size, image, **options): + def transform( + self, + size: tuple[int, int], + image: Image.Image, + **options: dict[str, str | int | tuple[int, ...] | list[int]], + ) -> Image.Image: # can be overridden method, data = self.getdata() return image.transform(size, method, data, **options) From 59100652b6e9d4eeb82c34d446fea0a1736a5942 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 1 Jan 2024 20:33:34 +1100 Subject: [PATCH 2/3] Added type hints to ImageTransform --- docs/releasenotes/10.2.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/releasenotes/10.2.0.rst b/docs/releasenotes/10.2.0.rst index f8ef4b8c85a..d1f549e9c51 100644 --- a/docs/releasenotes/10.2.0.rst +++ b/docs/releasenotes/10.2.0.rst @@ -149,4 +149,5 @@ Work has begun to add type annotations to Pillow, including: * :py:mod:`~PIL.ImageChops` * :py:mod:`~PIL.ImageMode` * :py:mod:`~PIL.ImageSequence` +* :py:mod:`~PIL.ImageTransform` * :py:mod:`~PIL.TarIO` From 09ea12107940d6831c300db3bd6ed29a0888c5b7 Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Mon, 1 Jan 2024 21:09:01 +1100 Subject: [PATCH 3/3] Use enum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondrej Baranovič --- src/PIL/ImageTransform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/ImageTransform.py b/src/PIL/ImageTransform.py index 1dc3e210e88..84c81f1844f 100644 --- a/src/PIL/ImageTransform.py +++ b/src/PIL/ImageTransform.py @@ -20,7 +20,7 @@ class Transform(Image.ImageTransformHandler): - method: int + method: Image.Transform def __init__(self, data: Sequence[int]) -> None: self.data = data