Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type hints #8108

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Tests/bench_cffi_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def test_direct() -> None:
caccess = im.im.pixel_access(False)
access = PyAccess.new(im, False)

assert access is not None
assert caccess[(0, 0)] == access[(0, 0)]

print(f"Size: {im.width}x{im.height}")
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_unsupported_module() -> None:


@pytest.mark.parametrize("supported_formats", (True, False))
def test_pilinfo(supported_formats) -> None:
def test_pilinfo(supported_formats: bool) -> None:
buf = io.StringIO()
features.pilinfo(buf, supported_formats=supported_formats)
out = buf.getvalue()
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_bmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_load_dib() -> None:
(124, "g/pal8v5.bmp"),
),
)
def test_dib_header_size(header_size, path):
def test_dib_header_size(header_size: int, path: str) -> None:
image_path = "Tests/images/bmp/" + path
with open(image_path, "rb") as fp:
data = fp.read()[14:]
Expand Down
11 changes: 6 additions & 5 deletions Tests/test_file_bufrstub.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from pathlib import Path
from typing import IO

import pytest

from PIL import BufrStubImagePlugin, Image
from PIL import BufrStubImagePlugin, Image, ImageFile

from .helper import hopper

Expand Down Expand Up @@ -50,20 +51,20 @@ def test_save(tmp_path: Path) -> None:


def test_handler(tmp_path: Path) -> None:
class TestHandler:
class TestHandler(ImageFile.StubHandler):
opened = False
loaded = False
saved = False

def open(self, im) -> None:
def open(self, im: ImageFile.StubImageFile) -> None:
self.opened = True

def load(self, im):
def load(self, im: ImageFile.StubImageFile) -> Image.Image:
self.loaded = True
im.fp.close()
return Image.new("RGB", (1, 1))

def save(self, im, fp, filename) -> None:
def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
self.saved = True

handler = TestHandler()
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_gribstub.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from PIL import GribStubImagePlugin, Image
from PIL import GribStubImagePlugin, Image, ImageFile

from .helper import hopper

Expand Down Expand Up @@ -51,7 +51,7 @@ def test_save(tmp_path: Path) -> None:


def test_handler(tmp_path: Path) -> None:
class TestHandler:
class TestHandler(ImageFile.StubHandler):
opened = False
loaded = False
saved = False
Expand Down
7 changes: 4 additions & 3 deletions Tests/test_file_hdf5stub.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

from io import BytesIO
from pathlib import Path
from typing import IO

import pytest

from PIL import Hdf5StubImagePlugin, Image
from PIL import Hdf5StubImagePlugin, Image, ImageFile

TEST_FILE = "Tests/images/hdf5.h5"

Expand Down Expand Up @@ -41,7 +42,7 @@ def test_load() -> None:
def test_save() -> None:
# Arrange
with Image.open(TEST_FILE) as im:
dummy_fp = None
dummy_fp = BytesIO()
dummy_filename = "dummy.filename"

# Act / Assert: stub cannot save without an implemented handler
Expand All @@ -52,7 +53,7 @@ def test_save() -> None:


def test_handler(tmp_path: Path) -> None:
class TestHandler:
class TestHandler(ImageFile.StubHandler):
opened = False
loaded = False
saved = False
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def getchannels(im: JpegImagePlugin.JpegImageFile) -> tuple[int, int, int]:
[TEST_FILE, "Tests/images/pil_sample_cmyk.jpg"],
)
def test_dpi(self, test_image_path: str) -> None:
def test(xdpi: int, ydpi: int | None = None):
def test(xdpi: int, ydpi: int | None = None) -> tuple[int, int] | None:
with Image.open(test_image_path) as im:
im = self.roundtrip(im, dpi=(xdpi, ydpi or xdpi))
return im.info.get("dpi")
Expand Down
4 changes: 3 additions & 1 deletion Tests/test_file_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ def test_file_pointer_could_be_reused(self) -> None:
(0, (0,), (-1, 0, 1, 2), (253, 254, 255, 256)),
)
@skip_unless_feature("webp_anim")
def test_invalid_background(self, background, tmp_path: Path) -> None:
def test_invalid_background(
self, background: int | tuple[int, ...], tmp_path: Path
) -> None:
temp_file = str(tmp_path / "temp.webp")
im = hopper()
with pytest.raises(OSError):
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_webp_animated.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_write_animation_RGB(tmp_path: Path) -> None:
are visually similar to the originals.
"""

def check(temp_file) -> None:
def check(temp_file: str) -> None:
with Image.open(temp_file) as im:
assert im.n_frames == 2

Expand Down
12 changes: 8 additions & 4 deletions Tests/test_file_wmf.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from pathlib import Path
from typing import IO

import pytest

from PIL import Image, WmfImagePlugin
from PIL import Image, ImageFile, WmfImagePlugin

from .helper import assert_image_similar_tofile, hopper

Expand Down Expand Up @@ -34,10 +35,13 @@


def test_register_handler(tmp_path: Path) -> None:
class TestHandler:
class TestHandler(ImageFile.StubHandler):
methodCalled = False

def save(self, im, fp, filename) -> None:
def load(self, im: ImageFile.StubImageFile) -> Image.Image:
return Image.new("RGB", (1, 1))

Check warning on line 42 in Tests/test_file_wmf.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_file_wmf.py#L42

Added line #L42 was not covered by tests

def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
self.methodCalled = True

handler = TestHandler()
Expand Down Expand Up @@ -70,7 +74,7 @@


@pytest.mark.parametrize("ext", (".wmf", ".emf"))
def test_save(ext, tmp_path: Path) -> None:
def test_save(ext: str, tmp_path: Path) -> None:
im = hopper()

tmpfile = str(tmp_path / ("temp" + ext))
Expand Down
6 changes: 6 additions & 0 deletions Tests/test_image_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@
caccess = im.im.pixel_access(False)
with pytest.warns(DeprecationWarning):
access = PyAccess.new(im, False)
assert access is not None

Check warning on line 262 in Tests/test_image_access.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_image_access.py#L262

Added line #L262 was not covered by tests

w, h = im.size
for x in range(0, w, 10):
Expand Down Expand Up @@ -289,6 +290,7 @@
caccess = im.im.pixel_access(False)
with pytest.warns(DeprecationWarning):
access = PyAccess.new(im, False)
assert access is not None

Check warning on line 293 in Tests/test_image_access.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_image_access.py#L293

Added line #L293 was not covered by tests

w, h = im.size
for x in range(0, w, 10):
Expand All @@ -299,6 +301,8 @@
# Attempt to set the value on a read-only image
with pytest.warns(DeprecationWarning):
access = PyAccess.new(im, True)
assert access is not None

Check warning on line 304 in Tests/test_image_access.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_image_access.py#L304

Added line #L304 was not covered by tests

with pytest.raises(ValueError):
access[(0, 0)] = color

Expand Down Expand Up @@ -341,6 +345,8 @@
im = Image.new(mode, (1, 1))
with pytest.warns(DeprecationWarning):
access = PyAccess.new(im, False)
assert access is not None

Check warning on line 348 in Tests/test_image_access.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_image_access.py#L348

Added line #L348 was not covered by tests

access.putpixel((0, 0), color)

if len(color) == 3:
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_image_rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def test_fastpath_translate() -> None:
def test_center() -> None:
im = hopper()
rotate(im, im.mode, 45, center=(0, 0))
rotate(im, im.mode, 45, translate=(im.size[0] / 2, 0))
rotate(im, im.mode, 45, center=(0, 0), translate=(im.size[0] / 2, 0))
rotate(im, im.mode, 45, translate=(im.size[0] // 2, 0))
rotate(im, im.mode, 45, center=(0, 0), translate=(im.size[0] // 2, 0))


def test_rotate_no_fill() -> None:
Expand Down
4 changes: 3 additions & 1 deletion Tests/test_image_thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ def test_load_first_unless_jpeg() -> None:
with Image.open("Tests/images/hopper.jpg") as im:
draft = im.draft

def im_draft(mode: str, size: tuple[int, int]):
def im_draft(
mode: str, size: tuple[int, int]
) -> tuple[str, tuple[int, int, float, float]] | None:
result = draft(mode, size)
assert result is not None

Expand Down
1 change: 0 additions & 1 deletion Tests/test_imageops_usm.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def test_blur_formats(test_images: dict[str, ImageFile.ImageFile]) -> None:
blur = ImageFilter.GaussianBlur
with pytest.raises(ValueError):
im.convert("1").filter(blur)
blur(im.convert("L"))
with pytest.raises(ValueError):
im.convert("I").filter(blur)
with pytest.raises(ValueError):
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_qt_image_qapplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
def test_sanity(tmp_path: Path) -> None:
# Segfault test
app = QApplication([])
app: QApplication | None = QApplication([])

Check warning on line 49 in Tests/test_qt_image_qapplication.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_qt_image_qapplication.py#L49

Added line #L49 was not covered by tests
ex = Example()
assert app # Silence warning
assert ex # Silence warning
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/BufrStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
_handler = None


def register_handler(handler: ImageFile.StubHandler) -> None:
def register_handler(handler: ImageFile.StubHandler | None) -> None:
"""
Install application-specific BUFR image handler.

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GribStubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
_handler = None


def register_handler(handler: ImageFile.StubHandler) -> None:
def register_handler(handler: ImageFile.StubHandler | None) -> None:
"""
Install application-specific GRIB image handler.

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Hdf5StubImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
_handler = None


def register_handler(handler: ImageFile.StubHandler) -> None:
def register_handler(handler: ImageFile.StubHandler | None) -> None:
"""
Install application-specific HDF5 image handler.

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/WmfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
_handler = None


def register_handler(handler: ImageFile.StubHandler) -> None:
def register_handler(handler: ImageFile.StubHandler | None) -> None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the tests, None is passed to this from

WmfImagePlugin.register_handler(original_handler)

"""
Install application-specific WMF image handler.

Expand Down
Loading