Skip to content

Commit 811dd15

Browse files
authored
Merge pull request #7769 from radarhere/type_hints
2 parents 3e04ca5 + 65cb0b0 commit 811dd15

18 files changed

Lines changed: 235 additions & 162 deletions

Tests/test_bmp_reference.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
base = os.path.join("Tests", "images", "bmp")
1111

1212

13-
def get_files(d, ext: str = ".bmp"):
13+
def get_files(d: str, ext: str = ".bmp") -> list[str]:
1414
return [
1515
os.path.join(base, d, f) for f in os.listdir(os.path.join(base, d)) if ext in f
1616
]
@@ -29,7 +29,7 @@ def test_bad() -> None:
2929
pass
3030

3131

32-
def test_questionable():
32+
def test_questionable() -> None:
3333
"""These shouldn't crash/dos, but it's not well defined that these
3434
are in spec"""
3535
supported = [
@@ -80,7 +80,7 @@ def test_good() -> None:
8080
"rgb32bf.bmp": "rgb24.png",
8181
}
8282

83-
def get_compare(f):
83+
def get_compare(f: str) -> str:
8484
name = os.path.split(f)[1]
8585
if name in file_map:
8686
return os.path.join(base, "html", file_map[name])

Tests/test_box_blur.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ def test_imageops_box_blur() -> None:
2323
assert isinstance(i, Image.Image)
2424

2525

26-
def box_blur(image, radius: int = 1, n: int = 1):
26+
def box_blur(image: Image.Image, radius: float = 1, n: int = 1) -> Image.Image:
2727
return image._new(image.im.box_blur((radius, radius), n))
2828

2929

30-
def assert_image(im, data, delta: int = 0) -> None:
30+
def assert_image(im: Image.Image, data: list[list[int]], delta: int = 0) -> None:
3131
it = iter(im.getdata())
3232
for data_row in data:
3333
im_row = [next(it) for _ in range(im.size[0])]
@@ -37,7 +37,13 @@ def assert_image(im, data, delta: int = 0) -> None:
3737
next(it)
3838

3939

40-
def assert_blur(im, radius, data, passes: int = 1, delta: int = 0) -> None:
40+
def assert_blur(
41+
im: Image.Image,
42+
radius: float,
43+
data: list[list[int]],
44+
passes: int = 1,
45+
delta: int = 0,
46+
) -> None:
4147
# check grayscale image
4248
assert_image(box_blur(im, radius, passes), data, delta)
4349
rgba = Image.merge("RGBA", (im, im, im, im))

Tests/test_file_apng.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_apng_basic() -> None:
4747
"filename",
4848
("Tests/images/apng/split_fdat.png", "Tests/images/apng/split_fdat_zero_chunk.png"),
4949
)
50-
def test_apng_fdat(filename) -> None:
50+
def test_apng_fdat(filename: str) -> None:
5151
with Image.open(filename) as im:
5252
im.seek(im.n_frames - 1)
5353
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
@@ -338,7 +338,7 @@ def test_apng_syntax_errors() -> None:
338338
"sequence_fdat_fctl.png",
339339
),
340340
)
341-
def test_apng_sequence_errors(test_file) -> None:
341+
def test_apng_sequence_errors(test_file: str) -> None:
342342
with pytest.raises(SyntaxError):
343343
with Image.open(f"Tests/images/apng/{test_file}") as im:
344344
im.seek(im.n_frames - 1)
@@ -681,7 +681,7 @@ def test_seek_after_close() -> None:
681681
@pytest.mark.parametrize("default_image", (True, False))
682682
@pytest.mark.parametrize("duplicate", (True, False))
683683
def test_different_modes_in_later_frames(
684-
mode, default_image, duplicate, tmp_path: Path
684+
mode: str, default_image: bool, duplicate: bool, tmp_path: Path
685685
) -> None:
686686
test_file = str(tmp_path / "temp.png")
687687

Tests/test_file_container.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_seek_mode_2() -> None:
6464

6565

6666
@pytest.mark.parametrize("bytesmode", (True, False))
67-
def test_read_n0(bytesmode) -> None:
67+
def test_read_n0(bytesmode: bool) -> None:
6868
# Arrange
6969
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
7070
container = ContainerIO.ContainerIO(fh, 22, 100)
@@ -80,7 +80,7 @@ def test_read_n0(bytesmode) -> None:
8080

8181

8282
@pytest.mark.parametrize("bytesmode", (True, False))
83-
def test_read_n(bytesmode) -> None:
83+
def test_read_n(bytesmode: bool) -> None:
8484
# Arrange
8585
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
8686
container = ContainerIO.ContainerIO(fh, 22, 100)
@@ -96,7 +96,7 @@ def test_read_n(bytesmode) -> None:
9696

9797

9898
@pytest.mark.parametrize("bytesmode", (True, False))
99-
def test_read_eof(bytesmode) -> None:
99+
def test_read_eof(bytesmode: bool) -> None:
100100
# Arrange
101101
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
102102
container = ContainerIO.ContainerIO(fh, 22, 100)
@@ -112,7 +112,7 @@ def test_read_eof(bytesmode) -> None:
112112

113113

114114
@pytest.mark.parametrize("bytesmode", (True, False))
115-
def test_readline(bytesmode) -> None:
115+
def test_readline(bytesmode: bool) -> None:
116116
# Arrange
117117
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
118118
container = ContainerIO.ContainerIO(fh, 0, 120)
@@ -127,7 +127,7 @@ def test_readline(bytesmode) -> None:
127127

128128

129129
@pytest.mark.parametrize("bytesmode", (True, False))
130-
def test_readlines(bytesmode) -> None:
130+
def test_readlines(bytesmode: bool) -> None:
131131
# Arrange
132132
expected = [
133133
"This is line 1\n",

Tests/test_file_gif.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import warnings
44
from io import BytesIO
55
from pathlib import Path
6+
from typing import Generator
67

78
import pytest
89

@@ -144,13 +145,13 @@ def test_strategy() -> None:
144145

145146

146147
def test_optimize() -> None:
147-
def test_grayscale(optimize):
148+
def test_grayscale(optimize: int) -> int:
148149
im = Image.new("L", (1, 1), 0)
149150
filename = BytesIO()
150151
im.save(filename, "GIF", optimize=optimize)
151152
return len(filename.getvalue())
152153

153-
def test_bilevel(optimize):
154+
def test_bilevel(optimize: int) -> int:
154155
im = Image.new("1", (1, 1), 0)
155156
test_file = BytesIO()
156157
im.save(test_file, "GIF", optimize=optimize)
@@ -178,7 +179,9 @@ def test_bilevel(optimize):
178179
(4, 513, 256),
179180
),
180181
)
181-
def test_optimize_correctness(colors, size, expected_palette_length) -> None:
182+
def test_optimize_correctness(
183+
colors: int, size: int, expected_palette_length: int
184+
) -> None:
182185
# 256 color Palette image, posterize to > 128 and < 128 levels.
183186
# Size bigger and smaller than 512x512.
184187
# Check the palette for number of colors allocated.
@@ -297,7 +300,7 @@ def test_roundtrip_save_all_1(tmp_path: Path) -> None:
297300
("Tests/images/dispose_bgnd_rgba.gif", "RGBA"),
298301
),
299302
)
300-
def test_loading_multiple_palettes(path, mode) -> None:
303+
def test_loading_multiple_palettes(path: str, mode: str) -> None:
301304
with Image.open(path) as im:
302305
assert im.mode == "P"
303306
first_frame_colors = im.palette.colors.keys()
@@ -347,9 +350,9 @@ def test_palette_handling(tmp_path: Path) -> None:
347350
def test_palette_434(tmp_path: Path) -> None:
348351
# see https://github.com/python-pillow/Pillow/issues/434
349352

350-
def roundtrip(im, *args, **kwargs):
353+
def roundtrip(im: Image.Image, **kwargs: bool) -> Image.Image:
351354
out = str(tmp_path / "temp.gif")
352-
im.copy().save(out, *args, **kwargs)
355+
im.copy().save(out, **kwargs)
353356
reloaded = Image.open(out)
354357

355358
return reloaded
@@ -429,7 +432,7 @@ def test_seek_rewind() -> None:
429432
("Tests/images/iss634.gif", 42),
430433
),
431434
)
432-
def test_n_frames(path, n_frames) -> None:
435+
def test_n_frames(path: str, n_frames: int) -> None:
433436
# Test is_animated before n_frames
434437
with Image.open(path) as im:
435438
assert im.is_animated == (n_frames != 1)
@@ -541,7 +544,10 @@ def test_dispose_background_transparency() -> None:
541544
),
542545
),
543546
)
544-
def test_transparent_dispose(loading_strategy, expected_colors) -> None:
547+
def test_transparent_dispose(
548+
loading_strategy: GifImagePlugin.LoadingStrategy,
549+
expected_colors: tuple[tuple[int | tuple[int, int, int, int], ...]],
550+
) -> None:
545551
GifImagePlugin.LOADING_STRATEGY = loading_strategy
546552
try:
547553
with Image.open("Tests/images/transparent_dispose.gif") as img:
@@ -889,7 +895,9 @@ def test_identical_frames(tmp_path: Path) -> None:
889895
1500,
890896
),
891897
)
892-
def test_identical_frames_to_single_frame(duration, tmp_path: Path) -> None:
898+
def test_identical_frames_to_single_frame(
899+
duration: int | list[int], tmp_path: Path
900+
) -> None:
893901
out = str(tmp_path / "temp.gif")
894902
im_list = [
895903
Image.new("L", (100, 100), "#000"),
@@ -1049,7 +1057,7 @@ def test_retain_comment_in_subsequent_frames(tmp_path: Path) -> None:
10491057
def test_version(tmp_path: Path) -> None:
10501058
out = str(tmp_path / "temp.gif")
10511059

1052-
def assert_version_after_save(im, version) -> None:
1060+
def assert_version_after_save(im: Image.Image, version: bytes) -> None:
10531061
im.save(out)
10541062
with Image.open(out) as reread:
10551063
assert reread.info["version"] == version
@@ -1088,7 +1096,7 @@ def test_append_images(tmp_path: Path) -> None:
10881096
assert reread.n_frames == 3
10891097

10901098
# Tests appending using a generator
1091-
def im_generator(ims):
1099+
def im_generator(ims: list[Image.Image]) -> Generator[Image.Image, None, None]:
10921100
yield from ims
10931101

10941102
im.save(out, save_all=True, append_images=im_generator(ims))

Tests/test_file_mpo.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import warnings
44
from io import BytesIO
5+
from typing import Any
56

67
import pytest
78

@@ -19,7 +20,7 @@
1920
pytestmark = skip_unless_feature("jpg")
2021

2122

22-
def roundtrip(im, **options):
23+
def roundtrip(im: Image.Image, **options: Any) -> Image.Image:
2324
out = BytesIO()
2425
im.save(out, "MPO", **options)
2526
test_bytes = out.tell()
@@ -30,7 +31,7 @@ def roundtrip(im, **options):
3031

3132

3233
@pytest.mark.parametrize("test_file", test_files)
33-
def test_sanity(test_file) -> None:
34+
def test_sanity(test_file: str) -> None:
3435
with Image.open(test_file) as im:
3536
im.load()
3637
assert im.mode == "RGB"
@@ -70,7 +71,7 @@ def test_context_manager() -> None:
7071

7172

7273
@pytest.mark.parametrize("test_file", test_files)
73-
def test_app(test_file) -> None:
74+
def test_app(test_file: str) -> None:
7475
# Test APP/COM reader (@PIL135)
7576
with Image.open(test_file) as im:
7677
assert im.applist[0][0] == "APP1"
@@ -82,7 +83,7 @@ def test_app(test_file) -> None:
8283

8384

8485
@pytest.mark.parametrize("test_file", test_files)
85-
def test_exif(test_file) -> None:
86+
def test_exif(test_file: str) -> None:
8687
with Image.open(test_file) as im_original:
8788
im_reloaded = roundtrip(im_original, save_all=True, exif=im_original.getexif())
8889

@@ -143,7 +144,7 @@ def test_reload_exif_after_seek() -> None:
143144

144145

145146
@pytest.mark.parametrize("test_file", test_files)
146-
def test_mp(test_file) -> None:
147+
def test_mp(test_file: str) -> None:
147148
with Image.open(test_file) as im:
148149
mpinfo = im._getmp()
149150
assert mpinfo[45056] == b"0100"
@@ -168,7 +169,7 @@ def test_mp_no_data() -> None:
168169

169170

170171
@pytest.mark.parametrize("test_file", test_files)
171-
def test_mp_attribute(test_file) -> None:
172+
def test_mp_attribute(test_file: str) -> None:
172173
with Image.open(test_file) as im:
173174
mpinfo = im._getmp()
174175
for frame_number, mpentry in enumerate(mpinfo[0xB002]):
@@ -185,7 +186,7 @@ def test_mp_attribute(test_file) -> None:
185186

186187

187188
@pytest.mark.parametrize("test_file", test_files)
188-
def test_seek(test_file) -> None:
189+
def test_seek(test_file: str) -> None:
189190
with Image.open(test_file) as im:
190191
assert im.tell() == 0
191192
# prior to first image raises an error, both blatant and borderline
@@ -229,7 +230,7 @@ def test_eoferror() -> None:
229230

230231

231232
@pytest.mark.parametrize("test_file", test_files)
232-
def test_image_grab(test_file) -> None:
233+
def test_image_grab(test_file: str) -> None:
233234
with Image.open(test_file) as im:
234235
assert im.tell() == 0
235236
im0 = im.tobytes()
@@ -244,7 +245,7 @@ def test_image_grab(test_file) -> None:
244245

245246

246247
@pytest.mark.parametrize("test_file", test_files)
247-
def test_save(test_file) -> None:
248+
def test_save(test_file: str) -> None:
248249
with Image.open(test_file) as im:
249250
assert im.tell() == 0
250251
jpg0 = roundtrip(im)

0 commit comments

Comments
 (0)