Skip to content

Commit f55c9c6

Browse files
authored
Merge pull request #7754 from radarhere/type_hints
2 parents b82e601 + ddb7df0 commit f55c9c6

20 files changed

+149
-115
lines changed

Tests/test_image_convert.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
from __future__ import annotations
22

3+
from pathlib import Path
4+
35
import pytest
46

57
from PIL import Image
68

79
from .helper import assert_image, assert_image_equal, assert_image_similar, hopper
810

911

10-
def test_sanity():
11-
def convert(im, mode):
12+
def test_sanity() -> None:
13+
def convert(im: Image.Image, mode: str) -> None:
1214
out = im.convert(mode)
1315
assert out.mode == mode
1416
assert out.size == im.size
@@ -40,13 +42,13 @@ def convert(im, mode):
4042
convert(im, output_mode)
4143

4244

43-
def test_unsupported_conversion():
45+
def test_unsupported_conversion() -> None:
4446
im = hopper()
4547
with pytest.raises(ValueError):
4648
im.convert("INVALID")
4749

4850

49-
def test_default():
51+
def test_default() -> None:
5052
im = hopper("P")
5153
assert im.mode == "P"
5254
converted_im = im.convert()
@@ -62,18 +64,18 @@ def test_default():
6264
# ref https://github.com/python-pillow/Pillow/issues/274
6365

6466

65-
def _test_float_conversion(im):
67+
def _test_float_conversion(im: Image.Image) -> None:
6668
orig = im.getpixel((5, 5))
6769
converted = im.convert("F").getpixel((5, 5))
6870
assert orig == converted
6971

7072

71-
def test_8bit():
73+
def test_8bit() -> None:
7274
with Image.open("Tests/images/hopper.jpg") as im:
7375
_test_float_conversion(im.convert("L"))
7476

7577

76-
def test_16bit():
78+
def test_16bit() -> None:
7779
with Image.open("Tests/images/16bit.cropped.tif") as im:
7880
_test_float_conversion(im)
7981

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

8587

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

9092

91-
def test_opaque():
93+
def test_opaque() -> None:
9294
alpha = hopper("P").convert("PA").getchannel("A")
9395

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

9799

98-
def test_rgba_p():
100+
def test_rgba_p() -> None:
99101
im = hopper("RGBA")
100102
im.putalpha(hopper("L"))
101103

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

107109

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

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

114116

115-
def test_trns_p(tmp_path):
117+
def test_trns_p(tmp_path: Path) -> None:
116118
im = hopper("P")
117119
im.info["transparency"] = 0
118120

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

132134

133135
@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
136138
im = hopper("P")
137139
im.info["transparency"] = 128
@@ -148,7 +150,7 @@ def test_trns_p_transparency(mode):
148150
assert converted_im.palette is None
149151

150152

151-
def test_trns_l(tmp_path):
153+
def test_trns_l(tmp_path: Path) -> None:
152154
im = hopper("L")
153155
im.info["transparency"] = 128
154156

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

173175

174-
def test_trns_RGB(tmp_path):
176+
def test_trns_RGB(tmp_path: Path) -> None:
175177
im = hopper("RGB")
176178
im.info["transparency"] = im.getpixel((0, 0))
177179

@@ -201,7 +203,7 @@ def test_trns_RGB(tmp_path):
201203

202204

203205
@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:
205207
for mode in ("P", "PA"):
206208
im = Image.new(mode, (1, 1))
207209
im.palette.getcolor((0, 1, 2))
@@ -214,7 +216,7 @@ def test_l_macro_rounding(convert_mode):
214216
assert converted_color == 1
215217

216218

217-
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
219221
with Image.open("Tests/images/hopper.gif") as im:
220222
im.info["transparency"] = 255
@@ -226,7 +228,7 @@ def test_gif_with_rgba_palette_to_p():
226228
im_p.load()
227229

228230

229-
def test_p_la():
231+
def test_p_la() -> None:
230232
im = hopper("RGBA")
231233
alpha = hopper("L")
232234
im.putalpha(alpha)
@@ -236,7 +238,7 @@ def test_p_la():
236238
assert_image_similar(alpha, comparable, 5)
237239

238240

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

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

252254

253-
def test_p2pa_palette():
255+
def test_p2pa_palette() -> None:
254256
with Image.open("Tests/images/tiny.png") as im:
255257
im_pa = im.convert("PA")
256258
assert im_pa.getpalette() == im.getpalette()
257259

258260

259-
def test_matrix_illegal_conversion():
261+
def test_matrix_illegal_conversion() -> None:
260262
# Arrange
261263
im = hopper("CMYK")
262264
# fmt: off
@@ -272,7 +274,7 @@ def test_matrix_illegal_conversion():
272274
im.convert(mode="CMYK", matrix=matrix)
273275

274276

275-
def test_matrix_wrong_mode():
277+
def test_matrix_wrong_mode() -> None:
276278
# Arrange
277279
im = hopper("L")
278280
# fmt: off
@@ -289,7 +291,7 @@ def test_matrix_wrong_mode():
289291

290292

291293
@pytest.mark.parametrize("mode", ("RGB", "L"))
292-
def test_matrix_xyz(mode):
294+
def test_matrix_xyz(mode: str) -> None:
293295
# Arrange
294296
im = hopper("RGB")
295297
im.info["transparency"] = (255, 0, 0)
@@ -317,7 +319,7 @@ def test_matrix_xyz(mode):
317319
assert converted_im.info["transparency"] == 105
318320

319321

320-
def test_matrix_identity():
322+
def test_matrix_identity() -> None:
321323
# Arrange
322324
im = hopper("RGB")
323325
# fmt: off

Tests/test_image_copy.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

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

@@ -39,15 +39,15 @@ def test_copy(mode):
3939
assert out.size == cropped_size
4040

4141

42-
def test_copy_zero():
42+
def test_copy_zero() -> None:
4343
im = Image.new("RGB", (0, 0))
4444
out = im.copy()
4545
assert out.mode == im.mode
4646
assert out.size == im.size
4747

4848

4949
@skip_unless_feature("libtiff")
50-
def test_deepcopy():
50+
def test_deepcopy() -> None:
5151
with Image.open("Tests/images/g4_orientation_5.tif") as im:
5252
out = copy.deepcopy(im)
5353
assert out.size == (590, 88)

Tests/test_image_crop.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

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

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

1919

20-
def test_wide_crop():
21-
def crop(*bbox):
20+
def test_wide_crop() -> None:
21+
def crop(*bbox: int) -> tuple[int, ...]:
2222
i = im.crop(bbox)
2323
h = i.histogram()
2424
while h and not h[-1]:
@@ -47,14 +47,14 @@ def crop(*bbox):
4747

4848

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

5353
with pytest.raises(ValueError):
5454
im.crop(box)
5555

5656

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

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

7171

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

8989

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

9393
cropped = im.crop((0, 0, 0, 0))

Tests/test_image_frombytes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

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

1414
data = im1.tobytes()

Tests/test_image_fromqimage.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import warnings
4+
from typing import Generator
45

56
import pytest
67

@@ -18,7 +19,7 @@
1819

1920

2021
@pytest.fixture
21-
def test_images():
22+
def test_images() -> Generator[Image.Image, None, None]:
2223
ims = [
2324
hopper(),
2425
Image.open("Tests/images/transparent.png"),
@@ -31,7 +32,7 @@ def test_images():
3132
im.close()
3233

3334

34-
def roundtrip(expected):
35+
def roundtrip(expected: Image.Image) -> None:
3536
# PIL -> Qt
3637
intermediate = expected.toqimage()
3738
# Qt -> PIL
@@ -43,26 +44,26 @@ def roundtrip(expected):
4344
assert_image_equal(result, expected.convert("RGB"))
4445

4546

46-
def test_sanity_1(test_images):
47+
def test_sanity_1(test_images: Generator[Image.Image, None, None]) -> None:
4748
for im in test_images:
4849
roundtrip(im.convert("1"))
4950

5051

51-
def test_sanity_rgb(test_images):
52+
def test_sanity_rgb(test_images: Generator[Image.Image, None, None]) -> None:
5253
for im in test_images:
5354
roundtrip(im.convert("RGB"))
5455

5556

56-
def test_sanity_rgba(test_images):
57+
def test_sanity_rgba(test_images: Generator[Image.Image, None, None]) -> None:
5758
for im in test_images:
5859
roundtrip(im.convert("RGBA"))
5960

6061

61-
def test_sanity_l(test_images):
62+
def test_sanity_l(test_images: Generator[Image.Image, None, None]) -> None:
6263
for im in test_images:
6364
roundtrip(im.convert("L"))
6465

6566

66-
def test_sanity_p(test_images):
67+
def test_sanity_p(test_images: Generator[Image.Image, None, None]) -> None:
6768
for im in test_images:
6869
roundtrip(im.convert("P"))

Tests/test_image_getbands.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from PIL import Image
44

55

6-
def test_getbands():
6+
def test_getbands() -> None:
77
assert Image.new("1", (1, 1)).getbands() == ("1",)
88
assert Image.new("L", (1, 1)).getbands() == ("L",)
99
assert Image.new("I", (1, 1)).getbands() == ("I",)

Tests/test_image_getbbox.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
from .helper import hopper
88

99

10-
def test_sanity():
10+
def test_sanity() -> None:
1111
bbox = hopper().getbbox()
1212
assert isinstance(bbox, tuple)
1313

1414

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

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

3636
for mode in ("RGBA", "RGBa"):
37-
for color in ((0, 0, 0, 0), (127, 127, 127, 0), (255, 255, 255, 0)):
38-
im = Image.new(mode, (100, 100), color)
37+
for rgba_color in ((0, 0, 0, 0), (127, 127, 127, 0), (255, 255, 255, 0)):
38+
im = Image.new(mode, (100, 100), rgba_color)
3939
check(im, (255, 255, 255, 255))
4040

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

4646

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

0 commit comments

Comments
 (0)