Skip to content

Commit 5ff7d92

Browse files
committed
Added type hints
1 parent 912a33f commit 5ff7d92

16 files changed

Lines changed: 170 additions & 120 deletions

Tests/test_features.py

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

33
import io
44
import re
5+
from typing import Callable
56

67
import pytest
78

@@ -29,7 +30,7 @@ def test_version() -> None:
2930
# Check the correctness of the convenience function
3031
# and the format of version numbers
3132

32-
def test(name, function) -> None:
33+
def test(name: str, function: Callable[[str], bool]) -> None:
3334
version = features.version(name)
3435
if not features.check(name):
3536
assert version is None
@@ -73,12 +74,12 @@ def test_libimagequant_version() -> None:
7374

7475

7576
@pytest.mark.parametrize("feature", features.modules)
76-
def test_check_modules(feature) -> None:
77+
def test_check_modules(feature: str) -> None:
7778
assert features.check_module(feature) in [True, False]
7879

7980

8081
@pytest.mark.parametrize("feature", features.codecs)
81-
def test_check_codecs(feature) -> None:
82+
def test_check_codecs(feature: str) -> None:
8283
assert features.check_codec(feature) in [True, False]
8384

8485

Tests/test_file_blp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_save(tmp_path: Path) -> None:
7171
"Tests/images/timeout-ef9112a065e7183fa7faa2e18929b03e44ee16bf.blp",
7272
],
7373
)
74-
def test_crashes(test_file) -> None:
74+
def test_crashes(test_file: str) -> None:
7575
with open(test_file, "rb") as f:
7676
with Image.open(f) as im:
7777
with pytest.raises(OSError):

Tests/test_file_bmp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
def test_sanity(tmp_path: Path) -> None:
19-
def roundtrip(im) -> None:
19+
def roundtrip(im: Image.Image) -> None:
2020
outfile = str(tmp_path / "temp.bmp")
2121

2222
im.save(outfile, "BMP")
@@ -194,7 +194,7 @@ def test_rle4() -> None:
194194
("Tests/images/bmp/g/pal8rle.bmp", 1064),
195195
),
196196
)
197-
def test_rle8_eof(file_name, length) -> None:
197+
def test_rle8_eof(file_name: str, length: int) -> None:
198198
with open(file_name, "rb") as fp:
199199
data = fp.read(length)
200200
with Image.open(io.BytesIO(data)) as im:

Tests/test_file_im.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_eoferror() -> None:
8282

8383

8484
@pytest.mark.parametrize("mode", ("RGB", "P", "PA"))
85-
def test_roundtrip(mode, tmp_path: Path) -> None:
85+
def test_roundtrip(mode: str, tmp_path: Path) -> None:
8686
out = str(tmp_path / "temp.im")
8787
im = hopper(mode)
8888
im.save(out)

Tests/test_file_pcx.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .helper import assert_image_equal, hopper
1010

1111

12-
def _roundtrip(tmp_path: Path, im) -> None:
12+
def _roundtrip(tmp_path: Path, im: Image.Image) -> None:
1313
f = str(tmp_path / "temp.pcx")
1414
im.save(f)
1515
with Image.open(f) as im2:
@@ -44,7 +44,7 @@ def test_invalid_file() -> None:
4444

4545

4646
@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB"))
47-
def test_odd(tmp_path: Path, mode) -> None:
47+
def test_odd(tmp_path: Path, mode: str) -> None:
4848
# See issue #523, odd sized images should have a stride that's even.
4949
# Not that ImageMagick or GIMP write PCX that way.
5050
# We were not handling properly.
@@ -89,7 +89,7 @@ def test_large_count(tmp_path: Path) -> None:
8989
_roundtrip(tmp_path, im)
9090

9191

92-
def _test_buffer_overflow(tmp_path: Path, im, size: int = 1024) -> None:
92+
def _test_buffer_overflow(tmp_path: Path, im: Image.Image, size: int = 1024) -> None:
9393
_last = ImageFile.MAXBLOCK
9494
ImageFile.MAXBLOCK = size
9595
try:

Tests/test_file_pdf.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import tempfile
77
import time
88
from pathlib import Path
9+
from typing import Any, Generator
910

1011
import pytest
1112

@@ -14,7 +15,7 @@
1415
from .helper import hopper, mark_if_feature_version, skip_unless_feature
1516

1617

17-
def helper_save_as_pdf(tmp_path: Path, mode, **kwargs):
18+
def helper_save_as_pdf(tmp_path: Path, mode: str, **kwargs: Any) -> str:
1819
# Arrange
1920
im = hopper(mode)
2021
outfile = str(tmp_path / ("temp_" + mode + ".pdf"))
@@ -41,13 +42,13 @@ def helper_save_as_pdf(tmp_path: Path, mode, **kwargs):
4142

4243

4344
@pytest.mark.parametrize("mode", ("L", "P", "RGB", "CMYK"))
44-
def test_save(tmp_path: Path, mode) -> None:
45+
def test_save(tmp_path: Path, mode: str) -> None:
4546
helper_save_as_pdf(tmp_path, mode)
4647

4748

4849
@skip_unless_feature("jpg_2000")
4950
@pytest.mark.parametrize("mode", ("LA", "RGBA"))
50-
def test_save_alpha(tmp_path: Path, mode) -> None:
51+
def test_save_alpha(tmp_path: Path, mode: str) -> None:
5152
helper_save_as_pdf(tmp_path, mode)
5253

5354

@@ -112,7 +113,7 @@ def test_resolution(tmp_path: Path) -> None:
112113
{"dpi": (75, 150), "resolution": 200},
113114
),
114115
)
115-
def test_dpi(params, tmp_path: Path) -> None:
116+
def test_dpi(params: dict[str, int | tuple[int, int]], tmp_path: Path) -> None:
116117
im = hopper()
117118

118119
outfile = str(tmp_path / "temp.pdf")
@@ -156,7 +157,7 @@ def test_save_all(tmp_path: Path) -> None:
156157
assert os.path.getsize(outfile) > 0
157158

158159
# Test appending using a generator
159-
def im_generator(ims):
160+
def im_generator(ims: list[Image.Image]) -> Generator[Image.Image, None, None]:
160161
yield from ims
161162

162163
im.save(outfile, save_all=True, append_images=im_generator(ims))
@@ -226,7 +227,7 @@ def test_pdf_append_fails_on_nonexistent_file() -> None:
226227
im.save(os.path.join(temp_dir, "nonexistent.pdf"), append=True)
227228

228229

229-
def check_pdf_pages_consistency(pdf) -> None:
230+
def check_pdf_pages_consistency(pdf: PdfParser.PdfParser) -> None:
230231
pages_info = pdf.read_indirect(pdf.pages_ref)
231232
assert b"Parent" not in pages_info
232233
assert b"Kids" in pages_info
@@ -339,7 +340,7 @@ def test_pdf_append_to_bytesio() -> None:
339340
@pytest.mark.timeout(1)
340341
@pytest.mark.skipif("PILLOW_VALGRIND_TEST" in os.environ, reason="Valgrind is slower")
341342
@pytest.mark.parametrize("newline", (b"\r", b"\n"))
342-
def test_redos(newline) -> None:
343+
def test_redos(newline: bytes) -> None:
343344
malicious = b" trailer<<>>" + newline * 3456
344345

345346
# This particular exception isn't relevant here.

Tests/test_file_tiff.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import warnings
55
from io import BytesIO
66
from pathlib import Path
7+
from types import ModuleType
8+
from typing import Generator
79

810
import pytest
911

@@ -20,6 +22,7 @@
2022
is_win32,
2123
)
2224

25+
ElementTree: ModuleType | None
2326
try:
2427
from defusedxml import ElementTree
2528
except ImportError:
@@ -156,7 +159,7 @@ def test_int_resolution(self) -> None:
156159
"resolution_unit, dpi",
157160
[(None, 72.8), (2, 72.8), (3, 184.912)],
158161
)
159-
def test_load_float_dpi(self, resolution_unit, dpi) -> None:
162+
def test_load_float_dpi(self, resolution_unit: int | None, dpi: float) -> None:
160163
with Image.open(
161164
"Tests/images/hopper_float_dpi_" + str(resolution_unit) + ".tif"
162165
) as im:
@@ -284,7 +287,7 @@ def test_unknown_pixel_mode(self) -> None:
284287
("Tests/images/multipage.tiff", 3),
285288
),
286289
)
287-
def test_n_frames(self, path, n_frames) -> None:
290+
def test_n_frames(self, path: str, n_frames: int) -> None:
288291
with Image.open(path) as im:
289292
assert im.n_frames == n_frames
290293
assert im.is_animated == (n_frames != 1)
@@ -402,7 +405,7 @@ def test__delitem__(self) -> None:
402405
assert len_before == len_after + 1
403406

404407
@pytest.mark.parametrize("legacy_api", (False, True))
405-
def test_load_byte(self, legacy_api) -> None:
408+
def test_load_byte(self, legacy_api: bool) -> None:
406409
ifd = TiffImagePlugin.ImageFileDirectory_v2()
407410
data = b"abc"
408411
ret = ifd.load_byte(data, legacy_api)
@@ -431,7 +434,7 @@ def test_ifd_tag_type(self) -> None:
431434
assert 0x8825 in im.tag_v2
432435

433436
def test_exif(self, tmp_path: Path) -> None:
434-
def check_exif(exif) -> None:
437+
def check_exif(exif: Image.Exif) -> None:
435438
assert sorted(exif.keys()) == [
436439
256,
437440
257,
@@ -511,7 +514,7 @@ def test_exif_frames(self) -> None:
511514
assert im.getexif()[273] == (1408, 1907)
512515

513516
@pytest.mark.parametrize("mode", ("1", "L"))
514-
def test_photometric(self, mode, tmp_path: Path) -> None:
517+
def test_photometric(self, mode: str, tmp_path: Path) -> None:
515518
filename = str(tmp_path / "temp.tif")
516519
im = hopper(mode)
517520
im.save(filename, tiffinfo={262: 0})
@@ -660,7 +663,7 @@ def test_planar_configuration_save(self, tmp_path: Path) -> None:
660663
assert_image_equal_tofile(reloaded, infile)
661664

662665
@pytest.mark.parametrize("mode", ("P", "PA"))
663-
def test_palette(self, mode, tmp_path: Path) -> None:
666+
def test_palette(self, mode: str, tmp_path: Path) -> None:
664667
outfile = str(tmp_path / "temp.tif")
665668

666669
im = hopper(mode)
@@ -689,7 +692,7 @@ def test_tiff_save_all(self) -> None:
689692
assert reread.n_frames == 3
690693

691694
# Test appending using a generator
692-
def im_generator(ims):
695+
def im_generator(ims: list[Image.Image]) -> Generator[Image.Image, None, None]:
693696
yield from ims
694697

695698
mp = BytesIO()
@@ -860,7 +863,7 @@ def test_timeout(self) -> None:
860863
],
861864
)
862865
@pytest.mark.timeout(2)
863-
def test_oom(self, test_file) -> None:
866+
def test_oom(self, test_file: str) -> None:
864867
with pytest.raises(UnidentifiedImageError):
865868
with pytest.warns(UserWarning):
866869
with Image.open(test_file):

Tests/test_format_hsv.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
import colorsys
44
import itertools
5+
from typing import Callable
56

67
from PIL import Image
78

89
from .helper import assert_image_similar, hopper
910

1011

11-
def int_to_float(i):
12+
def int_to_float(i: int) -> float:
1213
return i / 255
1314

1415

15-
def str_to_float(i):
16+
def str_to_float(i: str) -> float:
1617
return ord(i) / 255
1718

1819

19-
def tuple_to_ints(tp):
20+
def tuple_to_ints(tp: tuple[float, float, float]) -> tuple[int, int, int]:
2021
x, y, z = tp
2122
return int(x * 255.0), int(y * 255.0), int(z * 255.0)
2223

@@ -25,7 +26,7 @@ def test_sanity() -> None:
2526
Image.new("HSV", (100, 100))
2627

2728

28-
def wedge():
29+
def wedge() -> Image.Image:
2930
w = Image._wedge()
3031
w90 = w.rotate(90)
3132

@@ -49,7 +50,11 @@ def wedge():
4950
return img
5051

5152

52-
def to_xxx_colorsys(im, func, mode):
53+
def to_xxx_colorsys(
54+
im: Image.Image,
55+
func: Callable[[float, float, float], tuple[float, float, float]],
56+
mode: str,
57+
) -> Image.Image:
5358
# convert the hard way using the library colorsys routines.
5459

5560
(r, g, b) = im.split()
@@ -70,11 +75,11 @@ def to_xxx_colorsys(im, func, mode):
7075
return hsv
7176

7277

73-
def to_hsv_colorsys(im):
78+
def to_hsv_colorsys(im: Image.Image) -> Image.Image:
7479
return to_xxx_colorsys(im, colorsys.rgb_to_hsv, "HSV")
7580

7681

77-
def to_rgb_colorsys(im):
82+
def to_rgb_colorsys(im: Image.Image) -> Image.Image:
7883
return to_xxx_colorsys(im, colorsys.hsv_to_rgb, "RGB")
7984

8085

Tests/test_imagechops.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import Callable
4+
35
from PIL import Image, ImageChops
46

57
from .helper import assert_image_equal, hopper
@@ -387,7 +389,9 @@ def test_overlay() -> None:
387389

388390

389391
def test_logical() -> None:
390-
def table(op, a, b):
392+
def table(
393+
op: Callable[[Image.Image, Image.Image], Image.Image], a: int, b: int
394+
) -> tuple[int, int, int, int]:
391395
out = []
392396
for x in (a, b):
393397
imx = Image.new("1", (1, 1), x)

Tests/test_imagedraw2.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from PIL import Image, ImageDraw, ImageDraw2, features
8+
from PIL._typing import Coords
89

910
from .helper import (
1011
assert_image_equal,
@@ -56,7 +57,7 @@ def test_sanity() -> None:
5657

5758

5859
@pytest.mark.parametrize("bbox", BBOX)
59-
def test_ellipse(bbox) -> None:
60+
def test_ellipse(bbox: Coords) -> None:
6061
# Arrange
6162
im = Image.new("RGB", (W, H))
6263
draw = ImageDraw2.Draw(im)
@@ -84,7 +85,7 @@ def test_ellipse_edge() -> None:
8485

8586

8687
@pytest.mark.parametrize("points", POINTS)
87-
def test_line(points) -> None:
88+
def test_line(points: Coords) -> None:
8889
# Arrange
8990
im = Image.new("RGB", (W, H))
9091
draw = ImageDraw2.Draw(im)
@@ -98,7 +99,7 @@ def test_line(points) -> None:
9899

99100

100101
@pytest.mark.parametrize("points", POINTS)
101-
def test_line_pen_as_brush(points) -> None:
102+
def test_line_pen_as_brush(points: Coords) -> None:
102103
# Arrange
103104
im = Image.new("RGB", (W, H))
104105
draw = ImageDraw2.Draw(im)
@@ -114,7 +115,7 @@ def test_line_pen_as_brush(points) -> None:
114115

115116

116117
@pytest.mark.parametrize("points", POINTS)
117-
def test_polygon(points) -> None:
118+
def test_polygon(points: Coords) -> None:
118119
# Arrange
119120
im = Image.new("RGB", (W, H))
120121
draw = ImageDraw2.Draw(im)
@@ -129,7 +130,7 @@ def test_polygon(points) -> None:
129130

130131

131132
@pytest.mark.parametrize("bbox", BBOX)
132-
def test_rectangle(bbox) -> None:
133+
def test_rectangle(bbox: Coords) -> None:
133134
# Arrange
134135
im = Image.new("RGB", (W, H))
135136
draw = ImageDraw2.Draw(im)

0 commit comments

Comments
 (0)