Added type hints · python-pillow/Pillow@ddb7df0
11from __future__ import annotations
223+from pathlib import Path
4+35import pytest
4657from PIL import Image
6879from .helper import assert_image, assert_image_equal, assert_image_similar, hopper
81091110-def test_sanity():
11-def convert(im, mode):
12+def test_sanity() -> None:
13+def convert(im: Image.Image, mode: str) -> None:
1214out = im.convert(mode)
1315assert out.mode == mode
1416assert out.size == im.size
@@ -40,13 +42,13 @@ def convert(im, mode):
4042convert(im, output_mode)
4143424443-def test_unsupported_conversion():
45+def test_unsupported_conversion() -> None:
4446im = hopper()
4547with pytest.raises(ValueError):
4648im.convert("INVALID")
4749485049-def test_default():
51+def test_default() -> None:
5052im = hopper("P")
5153assert im.mode == "P"
5254converted_im = im.convert()
@@ -62,18 +64,18 @@ def test_default():
6264# ref https://github.com/python-pillow/Pillow/issues/274
6365646665-def _test_float_conversion(im):
67+def _test_float_conversion(im: Image.Image) -> None:
6668orig = im.getpixel((5, 5))
6769converted = im.convert("F").getpixel((5, 5))
6870assert orig == converted
6971707271-def test_8bit():
73+def test_8bit() -> None:
7274with Image.open("Tests/images/hopper.jpg") as im:
7375_test_float_conversion(im.convert("L"))
7476757776-def test_16bit():
78+def test_16bit() -> None:
7779with Image.open("Tests/images/16bit.cropped.tif") as im:
7880_test_float_conversion(im)
7981@@ -83,19 +85,19 @@ def test_16bit():
8385assert im_i16.getpixel((0, 0)) == 65535
8486858786-def test_16bit_workaround():
88+def test_16bit_workaround() -> None:
8789with Image.open("Tests/images/16bit.cropped.tif") as im:
8890_test_float_conversion(im.convert("I"))
8991909291-def test_opaque():
93+def test_opaque() -> None:
9294alpha = hopper("P").convert("PA").getchannel("A")
93959496solid = Image.new("L", (128, 128), 255)
9597assert_image_equal(alpha, solid)
9698979998-def test_rgba_p():
100+def test_rgba_p() -> None:
99101im = hopper("RGBA")
100102im.putalpha(hopper("L"))
101103@@ -105,14 +107,14 @@ def test_rgba_p():
105107assert_image_similar(im, comparable, 20)
106108107109108-def test_rgba():
110+def test_rgba() -> None:
109111with Image.open("Tests/images/transparent.png") as im:
110112assert im.mode == "RGBA"
111113112114assert_image_similar(im.convert("RGBa").convert("RGB"), im.convert("RGB"), 1.5)
113115114116115-def test_trns_p(tmp_path):
117+def test_trns_p(tmp_path: Path) -> None:
116118im = hopper("P")
117119im.info["transparency"] = 0
118120@@ -131,7 +133,7 @@ def test_trns_p(tmp_path):
131133132134133135@pytest.mark.parametrize("mode", ("LA", "PA", "RGBA"))
134-def test_trns_p_transparency(mode):
136+def test_trns_p_transparency(mode: str) -> None:
135137# Arrange
136138im = hopper("P")
137139im.info["transparency"] = 128
@@ -148,7 +150,7 @@ def test_trns_p_transparency(mode):
148150assert converted_im.palette is None
149151150152151-def test_trns_l(tmp_path):
153+def test_trns_l(tmp_path: Path) -> None:
152154im = hopper("L")
153155im.info["transparency"] = 128
154156@@ -171,7 +173,7 @@ def test_trns_l(tmp_path):
171173im_p.save(f)
172174173175174-def test_trns_RGB(tmp_path):
176+def test_trns_RGB(tmp_path: Path) -> None:
175177im = hopper("RGB")
176178im.info["transparency"] = im.getpixel((0, 0))
177179@@ -201,7 +203,7 @@ def test_trns_RGB(tmp_path):
201203202204203205@pytest.mark.parametrize("convert_mode", ("L", "LA", "I"))
204-def test_l_macro_rounding(convert_mode):
206+def test_l_macro_rounding(convert_mode: str) -> None:
205207for mode in ("P", "PA"):
206208im = Image.new(mode, (1, 1))
207209im.palette.getcolor((0, 1, 2))
@@ -214,7 +216,7 @@ def test_l_macro_rounding(convert_mode):
214216assert converted_color == 1
215217216218217-def test_gif_with_rgba_palette_to_p():
219+def test_gif_with_rgba_palette_to_p() -> None:
218220# See https://github.com/python-pillow/Pillow/issues/2433
219221with Image.open("Tests/images/hopper.gif") as im:
220222im.info["transparency"] = 255
@@ -226,7 +228,7 @@ def test_gif_with_rgba_palette_to_p():
226228im_p.load()
227229228230229-def test_p_la():
231+def test_p_la() -> None:
230232im = hopper("RGBA")
231233alpha = hopper("L")
232234im.putalpha(alpha)
@@ -236,7 +238,7 @@ def test_p_la():
236238assert_image_similar(alpha, comparable, 5)
237239238240239-def test_p2pa_alpha():
241+def test_p2pa_alpha() -> None:
240242with Image.open("Tests/images/tiny.png") as im:
241243assert im.mode == "P"
242244@@ -250,13 +252,13 @@ def test_p2pa_alpha():
250252assert im_a.getpixel((x, y)) == alpha
251253252254253-def test_p2pa_palette():
255+def test_p2pa_palette() -> None:
254256with Image.open("Tests/images/tiny.png") as im:
255257im_pa = im.convert("PA")
256258assert im_pa.getpalette() == im.getpalette()
257259258260259-def test_matrix_illegal_conversion():
261+def test_matrix_illegal_conversion() -> None:
260262# Arrange
261263im = hopper("CMYK")
262264# fmt: off
@@ -272,7 +274,7 @@ def test_matrix_illegal_conversion():
272274im.convert(mode="CMYK", matrix=matrix)
273275274276275-def test_matrix_wrong_mode():
277+def test_matrix_wrong_mode() -> None:
276278# Arrange
277279im = hopper("L")
278280# fmt: off
@@ -289,7 +291,7 @@ def test_matrix_wrong_mode():
289291290292291293@pytest.mark.parametrize("mode", ("RGB", "L"))
292-def test_matrix_xyz(mode):
294+def test_matrix_xyz(mode: str) -> None:
293295# Arrange
294296im = hopper("RGB")
295297im.info["transparency"] = (255, 0, 0)
@@ -317,7 +319,7 @@ def test_matrix_xyz(mode):
317319assert converted_im.info["transparency"] == 105
318320319321320-def test_matrix_identity():
322+def test_matrix_identity() -> None:
321323# Arrange
322324im = hopper("RGB")
323325# fmt: off