Skip to content

Commit 9c98162

Browse files
authored
Merge pull request #7732 from radarhere/type_hints_check
Added type hints to Tests/check_*.py
2 parents 1891b61 + 3453a99 commit 9c98162

10 files changed

+39
-27
lines changed

Tests/check_fli_overflow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
TEST_FILE = "Tests/images/fli_overflow.fli"
66

77

8-
def test_fli_overflow():
8+
def test_fli_overflow() -> None:
99
# this should not crash with a malloc error or access violation
1010
with Image.open(TEST_FILE) as im:
1111
im.load()

Tests/check_imaging_leaks.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python3
22
from __future__ import annotations
33

4+
from typing import Any, Callable
5+
46
import pytest
57

68
from PIL import Image
@@ -13,31 +15,34 @@
1315
pytestmark = pytest.mark.skipif(is_win32(), reason="requires Unix or macOS")
1416

1517

16-
def _get_mem_usage():
18+
def _get_mem_usage() -> float:
1719
from resource import RUSAGE_SELF, getpagesize, getrusage
1820

1921
mem = getrusage(RUSAGE_SELF).ru_maxrss
2022
return mem * getpagesize() / 1024 / 1024
2123

2224

23-
def _test_leak(min_iterations, max_iterations, fn, *args, **kwargs):
25+
def _test_leak(
26+
min_iterations: int, max_iterations: int, fn: Callable[..., None], *args: Any
27+
) -> None:
2428
mem_limit = None
2529
for i in range(max_iterations):
26-
fn(*args, **kwargs)
30+
fn(*args)
2731
mem = _get_mem_usage()
2832
if i < min_iterations:
2933
mem_limit = mem + 1
3034
continue
3135
msg = f"memory usage limit exceeded after {i + 1} iterations"
36+
assert mem_limit is not None
3237
assert mem <= mem_limit, msg
3338

3439

35-
def test_leak_putdata():
40+
def test_leak_putdata() -> None:
3641
im = Image.new("RGB", (25, 25))
3742
_test_leak(min_iterations, max_iterations, im.putdata, im.getdata())
3843

3944

40-
def test_leak_getlist():
45+
def test_leak_getlist() -> None:
4146
im = Image.new("P", (25, 25))
4247
_test_leak(
4348
min_iterations,

Tests/check_j2k_leaks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
]
2121

2222

23-
def test_leak_load():
23+
def test_leak_load() -> None:
2424
from resource import RLIMIT_AS, RLIMIT_STACK, setrlimit
2525

2626
setrlimit(RLIMIT_STACK, (stack_size, stack_size))
@@ -30,7 +30,7 @@ def test_leak_load():
3030
im.load()
3131

3232

33-
def test_leak_save():
33+
def test_leak_save() -> None:
3434
from resource import RLIMIT_AS, RLIMIT_STACK, setrlimit
3535

3636
setrlimit(RLIMIT_STACK, (stack_size, stack_size))

Tests/check_j2k_overflow.py

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

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

57
from PIL import Image
68

79

8-
def test_j2k_overflow(tmp_path):
10+
def test_j2k_overflow(tmp_path: PosixPath) -> None:
911
im = Image.new("RGBA", (1024, 131584))
1012
target = str(tmp_path / "temp.jpc")
1113
with pytest.raises(OSError):

Tests/check_jpeg_leaks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@
111111
[standard_l_qtable, standard_chrominance_qtable],
112112
),
113113
)
114-
def test_qtables_leak(qtables):
114+
def test_qtables_leak(qtables: tuple[tuple[int, ...]] | list[tuple[int, ...]]) -> None:
115115
im = hopper("RGB")
116116
for _ in range(iterations):
117117
test_output = BytesIO()
118118
im.save(test_output, "JPEG", qtables=qtables)
119119

120120

121-
def test_exif_leak():
121+
def test_exif_leak() -> None:
122122
"""
123123
pre patch:
124124
@@ -181,7 +181,7 @@ def test_exif_leak():
181181
im.save(test_output, "JPEG", exif=exif)
182182

183183

184-
def test_base_save():
184+
def test_base_save() -> None:
185185
"""
186186
base case:
187187
MB

Tests/check_large_memory.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

33
import sys
4+
from pathlib import PosixPath
5+
from types import ModuleType
46

57
import pytest
68

@@ -16,6 +18,7 @@
1618
# 2.7 and 3.2.
1719

1820

21+
numpy: ModuleType | None
1922
try:
2023
import numpy
2124
except ImportError:
@@ -28,23 +31,24 @@
2831
pytestmark = pytest.mark.skipif(sys.maxsize <= 2**32, reason="requires 64-bit system")
2932

3033

31-
def _write_png(tmp_path, xdim, ydim):
34+
def _write_png(tmp_path: PosixPath, xdim: int, ydim: int) -> None:
3235
f = str(tmp_path / "temp.png")
3336
im = Image.new("L", (xdim, ydim), 0)
3437
im.save(f)
3538

3639

37-
def test_large(tmp_path):
40+
def test_large(tmp_path: PosixPath) -> None:
3841
"""succeeded prepatch"""
3942
_write_png(tmp_path, XDIM, YDIM)
4043

4144

42-
def test_2gpx(tmp_path):
45+
def test_2gpx(tmp_path: PosixPath) -> None:
4346
"""failed prepatch"""
4447
_write_png(tmp_path, XDIM, XDIM)
4548

4649

4750
@pytest.mark.skipif(numpy is None, reason="Numpy is not installed")
48-
def test_size_greater_than_int():
51+
def test_size_greater_than_int() -> None:
52+
assert numpy is not None
4953
arr = numpy.ndarray(shape=(16394, 16394))
5054
Image.fromarray(arr)

Tests/check_large_memory_numpy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4+
from pathlib import PosixPath
45

56
import pytest
67

@@ -24,19 +25,19 @@
2425
pytestmark = pytest.mark.skipif(sys.maxsize <= 2**32, reason="requires 64-bit system")
2526

2627

27-
def _write_png(tmp_path, xdim, ydim):
28+
def _write_png(tmp_path: PosixPath, xdim: int, ydim: int) -> None:
2829
dtype = np.uint8
2930
a = np.zeros((xdim, ydim), dtype=dtype)
3031
f = str(tmp_path / "temp.png")
3132
im = Image.fromarray(a, "L")
3233
im.save(f)
3334

3435

35-
def test_large(tmp_path):
36+
def test_large(tmp_path: PosixPath) -> None:
3637
"""succeeded prepatch"""
3738
_write_png(tmp_path, XDIM, YDIM)
3839

3940

40-
def test_2gpx(tmp_path):
41+
def test_2gpx(tmp_path: PosixPath) -> None:
4142
"""failed prepatch"""
4243
_write_png(tmp_path, XDIM, XDIM)

Tests/check_libtiff_segfault.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
TEST_FILE = "Tests/images/libtiff_segfault.tif"
88

99

10-
def test_libtiff_segfault():
10+
def test_libtiff_segfault() -> None:
1111
"""This test should not segfault. It will on Pillow <= 3.1.0 and
1212
libtiff >= 4.0.0
1313
"""

Tests/check_png_dos.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
TEST_FILE = "Tests/images/png_decompression_dos.png"
99

1010

11-
def test_ignore_dos_text():
11+
def test_ignore_dos_text() -> None:
1212
ImageFile.LOAD_TRUNCATED_IMAGES = True
1313

1414
try:
@@ -24,7 +24,7 @@ def test_ignore_dos_text():
2424
assert len(s) < 1024 * 1024, "Text chunk larger than 1M"
2525

2626

27-
def test_dos_text():
27+
def test_dos_text() -> None:
2828
try:
2929
im = Image.open(TEST_FILE)
3030
im.load()
@@ -36,7 +36,7 @@ def test_dos_text():
3636
assert len(s) < 1024 * 1024, "Text chunk larger than 1M"
3737

3838

39-
def test_dos_total_memory():
39+
def test_dos_total_memory() -> None:
4040
im = Image.new("L", (1, 1))
4141
compressed_data = zlib.compress(b"a" * 1024 * 1023)
4242

@@ -53,7 +53,7 @@ def test_dos_total_memory():
5353
try:
5454
im2 = Image.open(b)
5555
except ValueError as msg:
56-
assert "Too much memory" in msg
56+
assert "Too much memory" in str(msg)
5757
return
5858

5959
total_len = 0

Tests/check_wheel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from PIL import features
66

77

8-
def test_wheel_modules():
8+
def test_wheel_modules() -> None:
99
expected_modules = {"pil", "tkinter", "freetype2", "littlecms2", "webp"}
1010

1111
# tkinter is not available in cibuildwheel installed CPython on Windows
@@ -19,13 +19,13 @@ def test_wheel_modules():
1919
assert set(features.get_supported_modules()) == expected_modules
2020

2121

22-
def test_wheel_codecs():
22+
def test_wheel_codecs() -> None:
2323
expected_codecs = {"jpg", "jpg_2000", "zlib", "libtiff"}
2424

2525
assert set(features.get_supported_codecs()) == expected_codecs
2626

2727

28-
def test_wheel_features():
28+
def test_wheel_features() -> None:
2929
expected_features = {
3030
"webp_anim",
3131
"webp_mux",

0 commit comments

Comments
 (0)