|
3 | 3 | import warnings |
4 | 4 | from io import BytesIO |
5 | 5 | from pathlib import Path |
| 6 | +from typing import Generator |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 |
|
@@ -144,13 +145,13 @@ def test_strategy() -> None: |
144 | 145 |
|
145 | 146 |
|
146 | 147 | def test_optimize() -> None: |
147 | | - def test_grayscale(optimize): |
| 148 | + def test_grayscale(optimize: int) -> int: |
148 | 149 | im = Image.new("L", (1, 1), 0) |
149 | 150 | filename = BytesIO() |
150 | 151 | im.save(filename, "GIF", optimize=optimize) |
151 | 152 | return len(filename.getvalue()) |
152 | 153 |
|
153 | | - def test_bilevel(optimize): |
| 154 | + def test_bilevel(optimize: int) -> int: |
154 | 155 | im = Image.new("1", (1, 1), 0) |
155 | 156 | test_file = BytesIO() |
156 | 157 | im.save(test_file, "GIF", optimize=optimize) |
@@ -178,7 +179,9 @@ def test_bilevel(optimize): |
178 | 179 | (4, 513, 256), |
179 | 180 | ), |
180 | 181 | ) |
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: |
182 | 185 | # 256 color Palette image, posterize to > 128 and < 128 levels. |
183 | 186 | # Size bigger and smaller than 512x512. |
184 | 187 | # Check the palette for number of colors allocated. |
@@ -297,7 +300,7 @@ def test_roundtrip_save_all_1(tmp_path: Path) -> None: |
297 | 300 | ("Tests/images/dispose_bgnd_rgba.gif", "RGBA"), |
298 | 301 | ), |
299 | 302 | ) |
300 | | -def test_loading_multiple_palettes(path, mode) -> None: |
| 303 | +def test_loading_multiple_palettes(path: str, mode: str) -> None: |
301 | 304 | with Image.open(path) as im: |
302 | 305 | assert im.mode == "P" |
303 | 306 | first_frame_colors = im.palette.colors.keys() |
@@ -347,9 +350,9 @@ def test_palette_handling(tmp_path: Path) -> None: |
347 | 350 | def test_palette_434(tmp_path: Path) -> None: |
348 | 351 | # see https://github.com/python-pillow/Pillow/issues/434 |
349 | 352 |
|
350 | | - def roundtrip(im, *args, **kwargs): |
| 353 | + def roundtrip(im: Image.Image, **kwargs: bool) -> Image.Image: |
351 | 354 | out = str(tmp_path / "temp.gif") |
352 | | - im.copy().save(out, *args, **kwargs) |
| 355 | + im.copy().save(out, **kwargs) |
353 | 356 | reloaded = Image.open(out) |
354 | 357 |
|
355 | 358 | return reloaded |
@@ -429,7 +432,7 @@ def test_seek_rewind() -> None: |
429 | 432 | ("Tests/images/iss634.gif", 42), |
430 | 433 | ), |
431 | 434 | ) |
432 | | -def test_n_frames(path, n_frames) -> None: |
| 435 | +def test_n_frames(path: str, n_frames: int) -> None: |
433 | 436 | # Test is_animated before n_frames |
434 | 437 | with Image.open(path) as im: |
435 | 438 | assert im.is_animated == (n_frames != 1) |
@@ -541,7 +544,10 @@ def test_dispose_background_transparency() -> None: |
541 | 544 | ), |
542 | 545 | ), |
543 | 546 | ) |
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: |
545 | 551 | GifImagePlugin.LOADING_STRATEGY = loading_strategy |
546 | 552 | try: |
547 | 553 | with Image.open("Tests/images/transparent_dispose.gif") as img: |
@@ -889,7 +895,9 @@ def test_identical_frames(tmp_path: Path) -> None: |
889 | 895 | 1500, |
890 | 896 | ), |
891 | 897 | ) |
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: |
893 | 901 | out = str(tmp_path / "temp.gif") |
894 | 902 | im_list = [ |
895 | 903 | Image.new("L", (100, 100), "#000"), |
@@ -1049,7 +1057,7 @@ def test_retain_comment_in_subsequent_frames(tmp_path: Path) -> None: |
1049 | 1057 | def test_version(tmp_path: Path) -> None: |
1050 | 1058 | out = str(tmp_path / "temp.gif") |
1051 | 1059 |
|
1052 | | - def assert_version_after_save(im, version) -> None: |
| 1060 | + def assert_version_after_save(im: Image.Image, version: bytes) -> None: |
1053 | 1061 | im.save(out) |
1054 | 1062 | with Image.open(out) as reread: |
1055 | 1063 | assert reread.info["version"] == version |
@@ -1088,7 +1096,7 @@ def test_append_images(tmp_path: Path) -> None: |
1088 | 1096 | assert reread.n_frames == 3 |
1089 | 1097 |
|
1090 | 1098 | # Tests appending using a generator |
1091 | | - def im_generator(ims): |
| 1099 | + def im_generator(ims: list[Image.Image]) -> Generator[Image.Image, None, None]: |
1092 | 1100 | yield from ims |
1093 | 1101 |
|
1094 | 1102 | im.save(out, save_all=True, append_images=im_generator(ims)) |
|
0 commit comments