Skip to content

Commit 33c31cb

Browse files
authored
Merge pull request #8093 from radarhere/type_hints_tests
2 parents c757439 + afc7d8d commit 33c31cb

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

Tests/test_font_leaks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TestTTypeFontLeak(PillowLeakTestCase):
1212
iterations = 10
1313
mem_limit = 4096 # k
1414

15-
def _test_font(self, font: ImageFont.FreeTypeFont) -> None:
15+
def _test_font(self, font: ImageFont.FreeTypeFont | ImageFont.ImageFont) -> None:
1616
im = Image.new("RGB", (255, 255), "white")
1717
draw = ImageDraw.ImageDraw(im)
1818
self._test_leak(

Tests/test_image.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,18 @@ def test_open_formats(self) -> None:
9999
JPGFILE = "Tests/images/hopper.jpg"
100100

101101
with pytest.raises(TypeError):
102-
with Image.open(PNGFILE, formats=123):
102+
with Image.open(PNGFILE, formats=123): # type: ignore[arg-type]
103103
pass
104104

105-
for formats in [["JPEG"], ("JPEG",), ["jpeg"], ["Jpeg"], ["jPeG"], ["JpEg"]]:
105+
format_list: list[list[str] | tuple[str, ...]] = [
106+
["JPEG"],
107+
("JPEG",),
108+
["jpeg"],
109+
["Jpeg"],
110+
["jPeG"],
111+
["JpEg"],
112+
]
113+
for formats in format_list:
106114
with pytest.raises(UnidentifiedImageError):
107115
with Image.open(PNGFILE, formats=formats):
108116
pass
@@ -138,7 +146,7 @@ def test_invalid_image(self) -> None:
138146

139147
def test_bad_mode(self) -> None:
140148
with pytest.raises(ValueError):
141-
with Image.open("filename", "bad mode"):
149+
with Image.open("filename", "bad mode"): # type: ignore[arg-type]
142150
pass
143151

144152
def test_stringio(self) -> None:
@@ -497,9 +505,11 @@ def test_effect_spread_zero(self) -> None:
497505
def test_check_size(self) -> None:
498506
# Checking that the _check_size function throws value errors when we want it to
499507
with pytest.raises(ValueError):
500-
Image.new("RGB", 0) # not a tuple
508+
# not a tuple
509+
Image.new("RGB", 0) # type: ignore[arg-type]
501510
with pytest.raises(ValueError):
502-
Image.new("RGB", (0,)) # Tuple too short
511+
# tuple too short
512+
Image.new("RGB", (0,)) # type: ignore[arg-type]
503513
with pytest.raises(ValueError):
504514
Image.new("RGB", (-1, -1)) # w,h < 0
505515

Tests/test_imagecms.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
from io import BytesIO
99
from pathlib import Path
10-
from typing import Any
10+
from typing import Any, Literal, cast
1111

1212
import pytest
1313

@@ -209,13 +209,13 @@ def test_exceptions() -> None:
209209
ImageCms.buildTransform("foo", "bar", "RGB", "RGB")
210210

211211
with pytest.raises(ImageCms.PyCMSError, match="Invalid type for Profile"):
212-
ImageCms.getProfileName(None)
212+
ImageCms.getProfileName(None) # type: ignore[arg-type]
213213
skip_missing()
214214

215215
# Python <= 3.9: "an integer is required (got type NoneType)"
216216
# Python > 3.9: "'NoneType' object cannot be interpreted as an integer"
217217
with pytest.raises(ImageCms.PyCMSError, match="integer"):
218-
ImageCms.isIntentSupported(SRGB, None, None)
218+
ImageCms.isIntentSupported(SRGB, None, None) # type: ignore[arg-type]
219219

220220

221221
def test_display_profile() -> None:
@@ -239,7 +239,7 @@ def test_unsupported_color_space() -> None:
239239
"Color space not supported for on-the-fly profile creation (unsupported)"
240240
),
241241
):
242-
ImageCms.createProfile("unsupported")
242+
ImageCms.createProfile("unsupported") # type: ignore[arg-type]
243243

244244

245245
def test_invalid_color_temperature() -> None:
@@ -352,7 +352,7 @@ def test_extended_information() -> None:
352352
p = o.profile
353353

354354
def assert_truncated_tuple_equal(
355-
tup1: tuple[Any, ...], tup2: tuple[Any, ...], digits: int = 10
355+
tup1: tuple[Any, ...] | None, tup2: tuple[Any, ...], digits: int = 10
356356
) -> None:
357357
# Helper function to reduce precision of tuples of floats
358358
# recursively and then check equality.
@@ -368,6 +368,7 @@ def truncate_tuple(tuple_value: tuple[Any, ...]) -> tuple[Any, ...]:
368368
for val in tuple_value
369369
)
370370

371+
assert tup1 is not None
371372
assert truncate_tuple(tup1) == truncate_tuple(tup2)
372373

373374
assert p.attributes == 4294967296
@@ -513,22 +514,22 @@ def test_non_ascii_path(tmp_path: Path) -> None:
513514
def test_profile_typesafety() -> None:
514515
# does not segfault
515516
with pytest.raises(TypeError, match="Invalid type for Profile"):
516-
ImageCms.ImageCmsProfile(0).tobytes()
517+
ImageCms.ImageCmsProfile(0) # type: ignore[arg-type]
517518
with pytest.raises(TypeError, match="Invalid type for Profile"):
518-
ImageCms.ImageCmsProfile(1).tobytes()
519+
ImageCms.ImageCmsProfile(1) # type: ignore[arg-type]
519520

520521
# also check core function
521522
with pytest.raises(TypeError):
522-
ImageCms.core.profile_tobytes(0)
523+
ImageCms.core.profile_tobytes(0) # type: ignore[arg-type]
523524
with pytest.raises(TypeError):
524-
ImageCms.core.profile_tobytes(1)
525+
ImageCms.core.profile_tobytes(1) # type: ignore[arg-type]
525526

526527
if not is_pypy():
527528
# core profile should not be directly instantiable
528529
with pytest.raises(TypeError):
529530
ImageCms.core.CmsProfile()
530531
with pytest.raises(TypeError):
531-
ImageCms.core.CmsProfile(0)
532+
ImageCms.core.CmsProfile(0) # type: ignore[call-arg]
532533

533534

534535
@pytest.mark.skipif(is_pypy(), reason="fails on PyPy")
@@ -537,7 +538,7 @@ def test_transform_typesafety() -> None:
537538
with pytest.raises(TypeError):
538539
ImageCms.core.CmsTransform()
539540
with pytest.raises(TypeError):
540-
ImageCms.core.CmsTransform(0)
541+
ImageCms.core.CmsTransform(0) # type: ignore[call-arg]
541542

542543

543544
def assert_aux_channel_preserved(
@@ -637,7 +638,8 @@ def test_auxiliary_channels_isolated() -> None:
637638
continue
638639

639640
# convert with and without AUX data, test colors are equal
640-
source_profile = ImageCms.createProfile(src_format[1])
641+
src_colorSpace = cast(Literal["LAB", "XYZ", "sRGB"], src_format[1])
642+
source_profile = ImageCms.createProfile(src_colorSpace)
641643
destination_profile = ImageCms.createProfile(dst_format[1])
642644
source_image = src_format[3]
643645
test_transform = ImageCms.buildTransform(

Tests/test_imagestat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def test_sanity() -> None:
2525
st.stddev
2626

2727
with pytest.raises(AttributeError):
28-
st.spam()
28+
st.spam() # type: ignore[attr-defined]
2929

3030
with pytest.raises(TypeError):
31-
ImageStat.Stat(1)
31+
ImageStat.Stat(1) # type: ignore[arg-type]
3232

3333

3434
def test_hopper() -> None:

0 commit comments

Comments
 (0)