From d400ef2b46914c693fa94bf5188cb1cee0383735 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 26 Dec 2023 12:18:38 +1100 Subject: [PATCH 1/3] Added type hints --- src/PIL/ContainerIO.py | 2 +- src/PIL/TarIO.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/PIL/ContainerIO.py b/src/PIL/ContainerIO.py index 387a4c182..64d042426 100644 --- a/src/PIL/ContainerIO.py +++ b/src/PIL/ContainerIO.py @@ -24,7 +24,7 @@ class ContainerIO: file (for example a TAR file). """ - def __init__(self, file, offset, length): + def __init__(self, file, offset, length) -> None: """ Create file object. diff --git a/src/PIL/TarIO.py b/src/PIL/TarIO.py index 26522d93f..c9923487d 100644 --- a/src/PIL/TarIO.py +++ b/src/PIL/TarIO.py @@ -16,6 +16,7 @@ from __future__ import annotations import io +from types import TracebackType from . import ContainerIO @@ -23,7 +24,7 @@ from . import ContainerIO class TarIO(ContainerIO.ContainerIO): """A file object that provides read access to a given member of a TAR file.""" - def __init__(self, tarfile, file): + def __init__(self, tarfile: str, file: str) -> None: """ Create file object. @@ -57,11 +58,16 @@ class TarIO(ContainerIO.ContainerIO): super().__init__(self.fh, self.fh.tell(), size) # Context manager support - def __enter__(self): + def __enter__(self) -> TarIO: return self - def __exit__(self, *args): + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> None: self.close() - def close(self): + def close(self) -> None: self.fh.close() From 7b9d101533ea0e94e334427b7dba6a78bf5ab477 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 26 Dec 2023 16:47:04 +1100 Subject: [PATCH 2/3] Moved __future__ import to beginning of file --- Tests/oss-fuzz/fuzz_font.py | 3 ++- Tests/oss-fuzz/fuzz_pillow.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/oss-fuzz/fuzz_font.py b/Tests/oss-fuzz/fuzz_font.py index 4e7c7deec..024117c56 100755 --- a/Tests/oss-fuzz/fuzz_font.py +++ b/Tests/oss-fuzz/fuzz_font.py @@ -1,5 +1,7 @@ #!/usr/bin/python3 +from __future__ import annotations + # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +15,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import annotations import atheris diff --git a/Tests/oss-fuzz/fuzz_pillow.py b/Tests/oss-fuzz/fuzz_pillow.py index e7cd0474a..c1ab42e56 100644 --- a/Tests/oss-fuzz/fuzz_pillow.py +++ b/Tests/oss-fuzz/fuzz_pillow.py @@ -1,5 +1,7 @@ #!/usr/bin/python3 +from __future__ import annotations + # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +15,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from __future__ import annotations import atheris From bc5ec2268a45795c58cd87686986194174b4dcb0 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 26 Dec 2023 22:26:11 +1100 Subject: [PATCH 3/3] Added type hints --- docs/conf.py | 2 +- docs/example/anchors.py | 2 +- selftest.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 9974b0f2a..a70dece74 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -233,7 +233,7 @@ htmlhelp_basename = "PillowPILForkdoc" # -- Options for LaTeX output --------------------------------------------- -latex_elements = { +latex_elements: dict[str, str] = { # The paper size ('letterpaper' or 'a4paper'). # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). diff --git a/docs/example/anchors.py b/docs/example/anchors.py index 3a0e40b84..b5d76b4fe 100644 --- a/docs/example/anchors.py +++ b/docs/example/anchors.py @@ -5,7 +5,7 @@ from PIL import Image, ImageDraw, ImageFont font = ImageFont.truetype("Tests/fonts/NotoSans-Regular.ttf", 16) -def test(anchor): +def test(anchor: str) -> Image.Image: im = Image.new("RGBA", (200, 100), "white") d = ImageDraw.Draw(im) d.line(((100, 0), (100, 100)), "gray") diff --git a/selftest.py b/selftest.py index 600fd6496..ed5252c44 100755 --- a/selftest.py +++ b/selftest.py @@ -15,7 +15,7 @@ except AttributeError: pass -def testimage(): +def testimage() -> None: """ PIL lets you create in-memory images with various pixel types: