Skip to content

Commit

Permalink
Merge pull request python-pillow#7754 from radarhere/type_hints
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Jan 26, 2024
2 parents b82e601 + ddb7df0 commit f55c9c6
Show file tree
Hide file tree
Showing 20 changed files with 149 additions and 115 deletions.
50 changes: 26 additions & 24 deletions Tests/test_image_convert.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import annotations

from pathlib import Path

import pytest

from PIL import Image

from .helper import assert_image, assert_image_equal, assert_image_similar, hopper


def test_sanity():
def convert(im, mode):
def test_sanity() -> None:
def convert(im: Image.Image, mode: str) -> None:
out = im.convert(mode)
assert out.mode == mode
assert out.size == im.size
Expand Down Expand Up @@ -40,13 +42,13 @@ def convert(im, mode):
convert(im, output_mode)


def test_unsupported_conversion():
def test_unsupported_conversion() -> None:
im = hopper()
with pytest.raises(ValueError):
im.convert("INVALID")


def test_default():
def test_default() -> None:
im = hopper("P")
assert im.mode == "P"
converted_im = im.convert()
Expand All @@ -62,18 +64,18 @@ def test_default():
# ref https://github.com/python-pillow/Pillow/issues/274


def _test_float_conversion(im):
def _test_float_conversion(im: Image.Image) -> None:
orig = im.getpixel((5, 5))
converted = im.convert("F").getpixel((5, 5))
assert orig == converted


def test_8bit():
def test_8bit() -> None:
with Image.open("Tests/images/hopper.jpg") as im:
_test_float_conversion(im.convert("L"))


def test_16bit():
def test_16bit() -> None:
with Image.open("Tests/images/16bit.cropped.tif") as im:
_test_float_conversion(im)

Expand All @@ -83,19 +85,19 @@ def test_16bit():
assert im_i16.getpixel((0, 0)) == 65535


def test_16bit_workaround():
def test_16bit_workaround() -> None:
with Image.open("Tests/images/16bit.cropped.tif") as im:
_test_float_conversion(im.convert("I"))


def test_opaque():
def test_opaque() -> None:
alpha = hopper("P").convert("PA").getchannel("A")

solid = Image.new("L", (128, 128), 255)
assert_image_equal(alpha, solid)


def test_rgba_p():
def test_rgba_p() -> None:
im = hopper("RGBA")
im.putalpha(hopper("L"))

Expand All @@ -105,14 +107,14 @@ def test_rgba_p():
assert_image_similar(im, comparable, 20)


def test_rgba():
def test_rgba() -> None:
with Image.open("Tests/images/transparent.png") as im:
assert im.mode == "RGBA"

assert_image_similar(im.convert("RGBa").convert("RGB"), im.convert("RGB"), 1.5)


def test_trns_p(tmp_path):
def test_trns_p(tmp_path: Path) -> None:
im = hopper("P")
im.info["transparency"] = 0

Expand All @@ -131,7 +133,7 @@ def test_trns_p(tmp_path):


@pytest.mark.parametrize("mode", ("LA", "PA", "RGBA"))
def test_trns_p_transparency(mode):
def test_trns_p_transparency(mode: str) -> None:
# Arrange
im = hopper("P")
im.info["transparency"] = 128
Expand All @@ -148,7 +150,7 @@ def test_trns_p_transparency(mode):
assert converted_im.palette is None


def test_trns_l(tmp_path):
def test_trns_l(tmp_path: Path) -> None:
im = hopper("L")
im.info["transparency"] = 128

Expand All @@ -171,7 +173,7 @@ def test_trns_l(tmp_path):
im_p.save(f)


def test_trns_RGB(tmp_path):
def test_trns_RGB(tmp_path: Path) -> None:
im = hopper("RGB")
im.info["transparency"] = im.getpixel((0, 0))

Expand Down Expand Up @@ -201,7 +203,7 @@ def test_trns_RGB(tmp_path):


@pytest.mark.parametrize("convert_mode", ("L", "LA", "I"))
def test_l_macro_rounding(convert_mode):
def test_l_macro_rounding(convert_mode: str) -> None:
for mode in ("P", "PA"):
im = Image.new(mode, (1, 1))
im.palette.getcolor((0, 1, 2))
Expand All @@ -214,7 +216,7 @@ def test_l_macro_rounding(convert_mode):
assert converted_color == 1


def test_gif_with_rgba_palette_to_p():
def test_gif_with_rgba_palette_to_p() -> None:
# See https://github.com/python-pillow/Pillow/issues/2433
with Image.open("Tests/images/hopper.gif") as im:
im.info["transparency"] = 255
Expand All @@ -226,7 +228,7 @@ def test_gif_with_rgba_palette_to_p():
im_p.load()


def test_p_la():
def test_p_la() -> None:
im = hopper("RGBA")
alpha = hopper("L")
im.putalpha(alpha)
Expand All @@ -236,7 +238,7 @@ def test_p_la():
assert_image_similar(alpha, comparable, 5)


def test_p2pa_alpha():
def test_p2pa_alpha() -> None:
with Image.open("Tests/images/tiny.png") as im:
assert im.mode == "P"

Expand All @@ -250,13 +252,13 @@ def test_p2pa_alpha():
assert im_a.getpixel((x, y)) == alpha


def test_p2pa_palette():
def test_p2pa_palette() -> None:
with Image.open("Tests/images/tiny.png") as im:
im_pa = im.convert("PA")
assert im_pa.getpalette() == im.getpalette()


def test_matrix_illegal_conversion():
def test_matrix_illegal_conversion() -> None:
# Arrange
im = hopper("CMYK")
# fmt: off
Expand All @@ -272,7 +274,7 @@ def test_matrix_illegal_conversion():
im.convert(mode="CMYK", matrix=matrix)


def test_matrix_wrong_mode():
def test_matrix_wrong_mode() -> None:
# Arrange
im = hopper("L")
# fmt: off
Expand All @@ -289,7 +291,7 @@ def test_matrix_wrong_mode():


@pytest.mark.parametrize("mode", ("RGB", "L"))
def test_matrix_xyz(mode):
def test_matrix_xyz(mode: str) -> None:
# Arrange
im = hopper("RGB")
im.info["transparency"] = (255, 0, 0)
Expand Down Expand Up @@ -317,7 +319,7 @@ def test_matrix_xyz(mode):
assert converted_im.info["transparency"] == 105


def test_matrix_identity():
def test_matrix_identity() -> None:
# Arrange
im = hopper("RGB")
# fmt: off
Expand Down
6 changes: 3 additions & 3 deletions Tests/test_image_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@pytest.mark.parametrize("mode", ("1", "P", "L", "RGB", "I", "F"))
def test_copy(mode):
def test_copy(mode: str) -> None:
cropped_coordinates = (10, 10, 20, 20)
cropped_size = (10, 10)

Expand Down Expand Up @@ -39,15 +39,15 @@ def test_copy(mode):
assert out.size == cropped_size


def test_copy_zero():
def test_copy_zero() -> None:
im = Image.new("RGB", (0, 0))
out = im.copy()
assert out.mode == im.mode
assert out.size == im.size


@skip_unless_feature("libtiff")
def test_deepcopy():
def test_deepcopy() -> None:
with Image.open("Tests/images/g4_orientation_5.tif") as im:
out = copy.deepcopy(im)
assert out.size == (590, 88)
14 changes: 7 additions & 7 deletions Tests/test_image_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@pytest.mark.parametrize("mode", ("1", "P", "L", "RGB", "I", "F"))
def test_crop(mode):
def test_crop(mode: str) -> None:
im = hopper(mode)
assert_image_equal(im.crop(), im)

Expand All @@ -17,8 +17,8 @@ def test_crop(mode):
assert cropped.size == (50, 50)


def test_wide_crop():
def crop(*bbox):
def test_wide_crop() -> None:
def crop(*bbox: int) -> tuple[int, ...]:
i = im.crop(bbox)
h = i.histogram()
while h and not h[-1]:
Expand Down Expand Up @@ -47,14 +47,14 @@ def crop(*bbox):


@pytest.mark.parametrize("box", ((8, 2, 2, 8), (2, 8, 8, 2), (8, 8, 2, 2)))
def test_negative_crop(box):
def test_negative_crop(box: tuple[int, int, int, int]) -> None:
im = Image.new("RGB", (10, 10))

with pytest.raises(ValueError):
im.crop(box)


def test_crop_float():
def test_crop_float() -> None:
# Check cropping floats are rounded to nearest integer
# https://github.com/python-pillow/Pillow/issues/1744

Expand All @@ -69,7 +69,7 @@ def test_crop_float():
assert cropped.size == (3, 5)


def test_crop_crash():
def test_crop_crash() -> None:
# Image.crop crashes prepatch with an access violation
# apparently a use after free on Windows, see
# https://github.com/python-pillow/Pillow/issues/1077
Expand All @@ -87,7 +87,7 @@ def test_crop_crash():
img.load()


def test_crop_zero():
def test_crop_zero() -> None:
im = Image.new("RGB", (0, 0), "white")

cropped = im.crop((0, 0, 0, 0))
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_image_frombytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@pytest.mark.parametrize("data_type", ("bytes", "memoryview"))
def test_sanity(data_type):
def test_sanity(data_type) -> None:
im1 = hopper()

data = im1.tobytes()
Expand Down
15 changes: 8 additions & 7 deletions Tests/test_image_fromqimage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import warnings
from typing import Generator

import pytest

Expand All @@ -18,7 +19,7 @@


@pytest.fixture
def test_images():
def test_images() -> Generator[Image.Image, None, None]:
ims = [
hopper(),
Image.open("Tests/images/transparent.png"),
Expand All @@ -31,7 +32,7 @@ def test_images():
im.close()


def roundtrip(expected):
def roundtrip(expected: Image.Image) -> None:
# PIL -> Qt
intermediate = expected.toqimage()
# Qt -> PIL
Expand All @@ -43,26 +44,26 @@ def roundtrip(expected):
assert_image_equal(result, expected.convert("RGB"))


def test_sanity_1(test_images):
def test_sanity_1(test_images: Generator[Image.Image, None, None]) -> None:
for im in test_images:
roundtrip(im.convert("1"))


def test_sanity_rgb(test_images):
def test_sanity_rgb(test_images: Generator[Image.Image, None, None]) -> None:
for im in test_images:
roundtrip(im.convert("RGB"))


def test_sanity_rgba(test_images):
def test_sanity_rgba(test_images: Generator[Image.Image, None, None]) -> None:
for im in test_images:
roundtrip(im.convert("RGBA"))


def test_sanity_l(test_images):
def test_sanity_l(test_images: Generator[Image.Image, None, None]) -> None:
for im in test_images:
roundtrip(im.convert("L"))


def test_sanity_p(test_images):
def test_sanity_p(test_images: Generator[Image.Image, None, None]) -> None:
for im in test_images:
roundtrip(im.convert("P"))
2 changes: 1 addition & 1 deletion Tests/test_image_getbands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from PIL import Image


def test_getbands():
def test_getbands() -> None:
assert Image.new("1", (1, 1)).getbands() == ("1",)
assert Image.new("L", (1, 1)).getbands() == ("L",)
assert Image.new("I", (1, 1)).getbands() == ("I",)
Expand Down
12 changes: 6 additions & 6 deletions Tests/test_image_getbbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from .helper import hopper


def test_sanity():
def test_sanity() -> None:
bbox = hopper().getbbox()
assert isinstance(bbox, tuple)


def test_bbox():
def check(im, fill_color):
def test_bbox() -> None:
def check(im: Image.Image, fill_color: int | tuple[int, ...]) -> None:
assert im.getbbox() is None

im.paste(fill_color, (10, 25, 90, 75))
Expand All @@ -34,8 +34,8 @@ def check(im, fill_color):
check(im, 255)

for mode in ("RGBA", "RGBa"):
for color in ((0, 0, 0, 0), (127, 127, 127, 0), (255, 255, 255, 0)):
im = Image.new(mode, (100, 100), color)
for rgba_color in ((0, 0, 0, 0), (127, 127, 127, 0), (255, 255, 255, 0)):
im = Image.new(mode, (100, 100), rgba_color)
check(im, (255, 255, 255, 255))

for mode in ("La", "LA", "PA"):
Expand All @@ -45,7 +45,7 @@ def check(im, fill_color):


@pytest.mark.parametrize("mode", ("RGBA", "RGBa", "La", "LA", "PA"))
def test_bbox_alpha_only_false(mode):
def test_bbox_alpha_only_false(mode: str) -> None:
im = Image.new(mode, (100, 100))
assert im.getbbox(alpha_only=False) is None

Expand Down
Loading

0 comments on commit f55c9c6

Please sign in to comment.