Skip to content

Commit 50e9a92

Browse files
authored
Merge pull request #7794 from radarhere/type_hints
Added type hints to additional tests
2 parents 3374e91 + 4ce06aa commit 50e9a92

20 files changed

+108
-75
lines changed

Tests/test_color_lut.py

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

33
from array import array
4+
from types import ModuleType
45

56
import pytest
67

78
from PIL import Image, ImageFilter
89

910
from .helper import assert_image_equal
1011

12+
numpy: ModuleType | None
1113
try:
1214
import numpy
1315
except ImportError:
@@ -397,6 +399,7 @@ def test_convert_table(self) -> None:
397399

398400
@pytest.mark.skipif(numpy is None, reason="NumPy not installed")
399401
def test_numpy_sources(self) -> None:
402+
assert numpy is not None
400403
table = numpy.ones((5, 6, 7, 3), dtype=numpy.float16)
401404
with pytest.raises(ValueError, match="should have either channels"):
402405
lut = ImageFilter.Color3DLUT((5, 6, 7), table)
@@ -430,6 +433,7 @@ def test_numpy_sources(self) -> None:
430433

431434
@pytest.mark.skipif(numpy is None, reason="NumPy not installed")
432435
def test_numpy_formats(self) -> None:
436+
assert numpy is not None
433437
g = Image.linear_gradient("L")
434438
im = Image.merge(
435439
"RGB",

Tests/test_core_resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,6 @@ def test_units(self) -> None:
187187
{"PILLOW_BLOCKS_MAX": "wat"},
188188
),
189189
)
190-
def test_warnings(self, var) -> None:
190+
def test_warnings(self, var: dict[str, str]) -> None:
191191
with pytest.warns(UserWarning):
192192
Image._apply_env_variables(var)

Tests/test_file_dds.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
TEST_FILE_DX10_BC1_TYPELESS,
4949
),
5050
)
51-
def test_sanity_dxt1_bc1(image_path) -> None:
51+
def test_sanity_dxt1_bc1(image_path: str) -> None:
5252
"""Check DXT1 and BC1 images can be opened"""
5353
with Image.open(TEST_FILE_DXT1.replace(".dds", ".png")) as target:
5454
target = target.convert("RGBA")
@@ -96,7 +96,7 @@ def test_sanity_dxt5() -> None:
9696
TEST_FILE_BC4U,
9797
),
9898
)
99-
def test_sanity_ati1_bc4u(image_path) -> None:
99+
def test_sanity_ati1_bc4u(image_path: str) -> None:
100100
"""Check ATI1 and BC4U images can be opened"""
101101

102102
with Image.open(image_path) as im:
@@ -117,7 +117,7 @@ def test_sanity_ati1_bc4u(image_path) -> None:
117117
TEST_FILE_DX10_BC4_TYPELESS,
118118
),
119119
)
120-
def test_dx10_bc4(image_path) -> None:
120+
def test_dx10_bc4(image_path: str) -> None:
121121
"""Check DX10 BC4 images can be opened"""
122122

123123
with Image.open(image_path) as im:
@@ -138,7 +138,7 @@ def test_dx10_bc4(image_path) -> None:
138138
TEST_FILE_BC5U,
139139
),
140140
)
141-
def test_sanity_ati2_bc5u(image_path) -> None:
141+
def test_sanity_ati2_bc5u(image_path: str) -> None:
142142
"""Check ATI2 and BC5U images can be opened"""
143143

144144
with Image.open(image_path) as im:
@@ -162,7 +162,7 @@ def test_sanity_ati2_bc5u(image_path) -> None:
162162
(TEST_FILE_BC5S, TEST_FILE_BC5S),
163163
),
164164
)
165-
def test_dx10_bc5(image_path, expected_path) -> None:
165+
def test_dx10_bc5(image_path: str, expected_path: str) -> None:
166166
"""Check DX10 BC5 images can be opened"""
167167

168168
with Image.open(image_path) as im:
@@ -176,7 +176,7 @@ def test_dx10_bc5(image_path, expected_path) -> None:
176176

177177

178178
@pytest.mark.parametrize("image_path", (TEST_FILE_BC6H, TEST_FILE_BC6HS))
179-
def test_dx10_bc6h(image_path) -> None:
179+
def test_dx10_bc6h(image_path: str) -> None:
180180
"""Check DX10 BC6H/BC6HS images can be opened"""
181181

182182
with Image.open(image_path) as im:
@@ -257,7 +257,7 @@ def test_dx10_r8g8b8a8_unorm_srgb() -> None:
257257
("RGBA", (800, 600), TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA),
258258
],
259259
)
260-
def test_uncompressed(mode, size, test_file) -> None:
260+
def test_uncompressed(mode: str, size: tuple[int, int], test_file: str) -> None:
261261
"""Check uncompressed images can be opened"""
262262

263263
with Image.open(test_file) as im:
@@ -359,7 +359,7 @@ def test_unsupported_bitcount() -> None:
359359
"Tests/images/unimplemented_pfflags.dds",
360360
),
361361
)
362-
def test_not_implemented(test_file) -> None:
362+
def test_not_implemented(test_file: str) -> None:
363363
with pytest.raises(NotImplementedError):
364364
with Image.open(test_file):
365365
pass
@@ -381,7 +381,7 @@ def test_save_unsupported_mode(tmp_path: Path) -> None:
381381
("RGBA", "Tests/images/pil123rgba.png"),
382382
],
383383
)
384-
def test_save(mode, test_file, tmp_path: Path) -> None:
384+
def test_save(mode: str, test_file: str, tmp_path: Path) -> None:
385385
out = str(tmp_path / "temp.dds")
386386
with Image.open(test_file) as im:
387387
assert im.mode == mode

Tests/test_file_fli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_seek() -> None:
147147
],
148148
)
149149
@pytest.mark.timeout(timeout=3)
150-
def test_timeouts(test_file) -> None:
150+
def test_timeouts(test_file: str) -> None:
151151
with open(test_file, "rb") as f:
152152
with Image.open(f) as im:
153153
with pytest.raises(OSError):
@@ -160,7 +160,7 @@ def test_timeouts(test_file) -> None:
160160
"Tests/images/crash-5762152299364352.fli",
161161
],
162162
)
163-
def test_crash(test_file) -> None:
163+
def test_crash(test_file: str) -> None:
164164
with open(test_file, "rb") as f:
165165
with Image.open(f) as im:
166166
with pytest.raises(OSError):

Tests/test_file_gribstub.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
from pathlib import Path
4+
from typing import IO
45

56
import pytest
67

@@ -55,15 +56,15 @@ class TestHandler:
5556
loaded = False
5657
saved = False
5758

58-
def open(self, im) -> None:
59+
def open(self, im: Image.Image) -> None:
5960
self.opened = True
6061

61-
def load(self, im):
62+
def load(self, im: Image.Image) -> Image.Image:
6263
self.loaded = True
6364
im.fp.close()
6465
return Image.new("RGB", (1, 1))
6566

66-
def save(self, im, fp, filename) -> None:
67+
def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
6768
self.saved = True
6869

6970
handler = TestHandler()

Tests/test_file_hdf5stub.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
from pathlib import Path
4+
from typing import IO
45

56
import pytest
67

@@ -56,15 +57,15 @@ class TestHandler:
5657
loaded = False
5758
saved = False
5859

59-
def open(self, im) -> None:
60+
def open(self, im: Image.Image) -> None:
6061
self.opened = True
6162

62-
def load(self, im):
63+
def load(self, im: Image.Image) -> Image.Image:
6364
self.loaded = True
6465
im.fp.close()
6566
return Image.new("RGB", (1, 1))
6667

67-
def save(self, im, fp, filename) -> None:
68+
def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
6869
self.saved = True
6970

7071
handler = TestHandler()

Tests/test_file_jpeg.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import warnings
66
from io import BytesIO
77
from pathlib import Path
8+
from types import ModuleType
89
from typing import Any
910

1011
import pytest
@@ -33,6 +34,7 @@
3334
skip_unless_feature,
3435
)
3536

37+
ElementTree: ModuleType | None
3638
try:
3739
from defusedxml import ElementTree
3840
except ImportError:
@@ -440,25 +442,25 @@ def getsampling(im: Image.Image):
440442
for subsampling in (-1, 3): # (default, invalid)
441443
im = self.roundtrip(hopper(), subsampling=subsampling)
442444
assert getsampling(im) == (2, 2, 1, 1, 1, 1)
443-
for subsampling in (0, "4:4:4"):
444-
im = self.roundtrip(hopper(), subsampling=subsampling)
445+
for subsampling1 in (0, "4:4:4"):
446+
im = self.roundtrip(hopper(), subsampling=subsampling1)
445447
assert getsampling(im) == (1, 1, 1, 1, 1, 1)
446-
for subsampling in (1, "4:2:2"):
447-
im = self.roundtrip(hopper(), subsampling=subsampling)
448+
for subsampling1 in (1, "4:2:2"):
449+
im = self.roundtrip(hopper(), subsampling=subsampling1)
448450
assert getsampling(im) == (2, 1, 1, 1, 1, 1)
449-
for subsampling in (2, "4:2:0", "4:1:1"):
450-
im = self.roundtrip(hopper(), subsampling=subsampling)
451+
for subsampling1 in (2, "4:2:0", "4:1:1"):
452+
im = self.roundtrip(hopper(), subsampling=subsampling1)
451453
assert getsampling(im) == (2, 2, 1, 1, 1, 1)
452454

453455
# RGB colorspace
454-
for subsampling in (-1, 0, "4:4:4"):
456+
for subsampling1 in (-1, 0, "4:4:4"):
455457
# "4:4:4" doesn't really make sense for RGB, but the conversion
456458
# to an integer happens at a higher level
457-
im = self.roundtrip(hopper(), keep_rgb=True, subsampling=subsampling)
459+
im = self.roundtrip(hopper(), keep_rgb=True, subsampling=subsampling1)
458460
assert getsampling(im) == (1, 1, 1, 1, 1, 1)
459-
for subsampling in (1, "4:2:2", 2, "4:2:0", 3):
461+
for subsampling1 in (1, "4:2:2", 2, "4:2:0", 3):
460462
with pytest.raises(OSError):
461-
self.roundtrip(hopper(), keep_rgb=True, subsampling=subsampling)
463+
self.roundtrip(hopper(), keep_rgb=True, subsampling=subsampling1)
462464

463465
with pytest.raises(TypeError):
464466
self.roundtrip(hopper(), subsampling="1:1:1")

Tests/test_file_palm.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .helper import assert_image_equal, hopper, magick_command
1212

1313

14-
def helper_save_as_palm(tmp_path: Path, mode) -> None:
14+
def helper_save_as_palm(tmp_path: Path, mode: str) -> None:
1515
# Arrange
1616
im = hopper(mode)
1717
outfile = str(tmp_path / ("temp_" + mode + ".palm"))
@@ -24,7 +24,7 @@ def helper_save_as_palm(tmp_path: Path, mode) -> None:
2424
assert os.path.getsize(outfile) > 0
2525

2626

27-
def open_with_magick(magick, tmp_path: Path, f):
27+
def open_with_magick(magick: list[str], tmp_path: Path, f: str) -> Image.Image:
2828
outfile = str(tmp_path / "temp.png")
2929
rc = subprocess.call(
3030
magick + [f, outfile], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
@@ -33,7 +33,7 @@ def open_with_magick(magick, tmp_path: Path, f):
3333
return Image.open(outfile)
3434

3535

36-
def roundtrip(tmp_path: Path, mode) -> None:
36+
def roundtrip(tmp_path: Path, mode: str) -> None:
3737
magick = magick_command()
3838
if not magick:
3939
return
@@ -66,6 +66,6 @@ def test_p_mode(tmp_path: Path) -> None:
6666

6767

6868
@pytest.mark.parametrize("mode", ("L", "RGB"))
69-
def test_oserror(tmp_path: Path, mode) -> None:
69+
def test_oserror(tmp_path: Path, mode: str) -> None:
7070
with pytest.raises(OSError):
7171
helper_save_as_palm(tmp_path, mode)

Tests/test_file_tar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
("jpg", "hopper.jpg", "JPEG"),
2020
),
2121
)
22-
def test_sanity(codec, test_path, format) -> None:
22+
def test_sanity(codec: str, test_path: str, format: str) -> None:
2323
if features.check(codec):
2424
with TarIO.TarIO(TEST_TAR_FILE, test_path) as tar:
2525
with Image.open(tar) as im:

Tests/test_file_webp_metadata.py

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

33
from io import BytesIO
44
from pathlib import Path
5+
from types import ModuleType
56

67
import pytest
78

@@ -14,6 +15,7 @@
1415
skip_unless_feature("webp_mux"),
1516
]
1617

18+
ElementTree: ModuleType | None
1719
try:
1820
from defusedxml import ElementTree
1921
except ImportError:

0 commit comments

Comments
 (0)