Skip to content

Commit 4f4b0bc

Browse files
authored
Merge pull request #8095 from radarhere/type_hints_tests
2 parents 33c31cb + 66ab7e0 commit 4f4b0bc

10 files changed

+34
-26
lines changed

Tests/test_file_gif.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,10 +1252,11 @@ def test_palette_save_L(tmp_path: Path) -> None:
12521252

12531253
im = hopper("P")
12541254
im_l = Image.frombytes("L", im.size, im.tobytes())
1255-
palette = bytes(im.getpalette())
1255+
palette = im.getpalette()
1256+
assert palette is not None
12561257

12571258
out = str(tmp_path / "temp.gif")
1258-
im_l.save(out, palette=palette)
1259+
im_l.save(out, palette=bytes(palette))
12591260

12601261
with Image.open(out) as reloaded:
12611262
assert_image_equal(reloaded.convert("RGB"), im.convert("RGB"))

Tests/test_file_jpeg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_cmyk(self) -> None:
154154
assert k > 0.9
155155

156156
def test_rgb(self) -> None:
157-
def getchannels(im: Image.Image) -> tuple[int, int, int]:
157+
def getchannels(im: JpegImagePlugin.JpegImageFile) -> tuple[int, int, int]:
158158
return tuple(v[0] for v in im.layer)
159159

160160
im = hopper()
@@ -443,7 +443,7 @@ def test_smooth(self) -> None:
443443
assert_image(im1, im2.mode, im2.size)
444444

445445
def test_subsampling(self) -> None:
446-
def getsampling(im: Image.Image):
446+
def getsampling(im: JpegImagePlugin.JpegImageFile):
447447
layer = im.layer
448448
return layer[0][1:3] + layer[1][1:3] + layer[2][1:3]
449449

Tests/test_file_libtiff.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,8 @@ def save_bytesio(compression: str | None = None) -> None:
668668
pilim.save(buffer_io, format="tiff", compression=compression)
669669
buffer_io.seek(0)
670670

671-
assert_image_similar_tofile(pilim, buffer_io, 0)
671+
with Image.open(buffer_io) as saved_im:
672+
assert_image_similar(pilim, saved_im, 0)
672673

673674
save_bytesio()
674675
save_bytesio("raw")

Tests/test_image.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from .helper import (
2626
assert_image_equal,
2727
assert_image_equal_tofile,
28+
assert_image_similar,
2829
assert_image_similar_tofile,
2930
assert_not_all_same,
3031
hopper,
@@ -193,7 +194,8 @@ def test_tempfile(self) -> None:
193194
with tempfile.TemporaryFile() as fp:
194195
im.save(fp, "JPEG")
195196
fp.seek(0)
196-
assert_image_similar_tofile(im, fp, 20)
197+
with Image.open(fp) as reloaded:
198+
assert_image_similar(im, reloaded, 20)
197199

198200
def test_unknown_extension(self, tmp_path: Path) -> None:
199201
im = hopper()

Tests/test_image_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def test(mode: str) -> tuple[str, tuple[int, int], bool]:
8686
assert test("RGBX") == ("RGBA", (128, 100), True)
8787

8888
# Test mode is None with no "typestr" in the array interface
89+
wrapped = Wrapper(hopper("L"), {"shape": (100, 128)})
8990
with pytest.raises(TypeError):
90-
wrapped = Wrapper(test("L"), {"shape": (100, 128)})
9191
Image.fromarray(wrapped)
9292

9393

Tests/test_image_crop.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_crop(mode: str) -> None:
1818

1919

2020
def test_wide_crop() -> None:
21-
def crop(*bbox: int) -> tuple[int, ...]:
21+
def crop(bbox: tuple[int, int, int, int]) -> tuple[int, ...]:
2222
i = im.crop(bbox)
2323
h = i.histogram()
2424
while h and not h[-1]:
@@ -27,23 +27,23 @@ def crop(*bbox: int) -> tuple[int, ...]:
2727

2828
im = Image.new("L", (100, 100), 1)
2929

30-
assert crop(0, 0, 100, 100) == (0, 10000)
31-
assert crop(25, 25, 75, 75) == (0, 2500)
30+
assert crop((0, 0, 100, 100)) == (0, 10000)
31+
assert crop((25, 25, 75, 75)) == (0, 2500)
3232

3333
# sides
34-
assert crop(-25, 0, 25, 50) == (1250, 1250)
35-
assert crop(0, -25, 50, 25) == (1250, 1250)
36-
assert crop(75, 0, 125, 50) == (1250, 1250)
37-
assert crop(0, 75, 50, 125) == (1250, 1250)
34+
assert crop((-25, 0, 25, 50)) == (1250, 1250)
35+
assert crop((0, -25, 50, 25)) == (1250, 1250)
36+
assert crop((75, 0, 125, 50)) == (1250, 1250)
37+
assert crop((0, 75, 50, 125)) == (1250, 1250)
3838

39-
assert crop(-25, 25, 125, 75) == (2500, 5000)
40-
assert crop(25, -25, 75, 125) == (2500, 5000)
39+
assert crop((-25, 25, 125, 75)) == (2500, 5000)
40+
assert crop((25, -25, 75, 125)) == (2500, 5000)
4141

4242
# corners
43-
assert crop(-25, -25, 25, 25) == (1875, 625)
44-
assert crop(75, -25, 125, 25) == (1875, 625)
45-
assert crop(75, 75, 125, 125) == (1875, 625)
46-
assert crop(-25, 75, 25, 125) == (1875, 625)
43+
assert crop((-25, -25, 25, 25)) == (1875, 625)
44+
assert crop((75, -25, 125, 25)) == (1875, 625)
45+
assert crop((75, 75, 125, 125)) == (1875, 625)
46+
assert crop((-25, 75, 25, 125)) == (1875, 625)
4747

4848

4949
@pytest.mark.parametrize("box", ((8, 2, 2, 8), (2, 8, 8, 2), (8, 8, 2, 2)))

Tests/test_image_filter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def test_sanity(filter_to_apply: ImageFilter.Filter, mode: str) -> None:
4646

4747
@pytest.mark.parametrize("mode", ("L", "I", "RGB", "CMYK"))
4848
def test_sanity_error(mode: str) -> None:
49+
im = hopper(mode)
4950
with pytest.raises(TypeError):
50-
im = hopper(mode)
51-
im.filter("hello")
51+
im.filter("hello") # type: ignore[arg-type]
5252

5353

5454
# crashes on small images

Tests/test_image_getextrema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def test_extrema() -> None:
9-
def extrema(mode: str) -> tuple[int, int] | tuple[tuple[int, int], ...]:
9+
def extrema(mode: str) -> tuple[float, float] | tuple[tuple[int, int], ...]:
1010
return hopper(mode).getextrema()
1111

1212
assert extrema("1") == (0, 255)

Tests/test_imagecms.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_invalid_color_temperature() -> None:
247247
ImageCms.PyCMSError,
248248
match='Color temperature must be numeric, "invalid" not valid',
249249
):
250-
ImageCms.createProfile("LAB", "invalid")
250+
ImageCms.createProfile("LAB", "invalid") # type: ignore[arg-type]
251251

252252

253253
@pytest.mark.parametrize("flag", ("my string", -1))
@@ -256,7 +256,7 @@ def test_invalid_flag(flag: str | int) -> None:
256256
with pytest.raises(
257257
ImageCms.PyCMSError, match="flags must be an integer between 0 and "
258258
):
259-
ImageCms.profileToProfile(im, "foo", "bar", flags=flag)
259+
ImageCms.profileToProfile(im, "foo", "bar", flags=flag) # type: ignore[arg-type]
260260

261261

262262
def test_simple_lab() -> None:
@@ -588,11 +588,13 @@ def create_test_image() -> Image.Image:
588588
)
589589

590590
# apply transform
591+
result_image: Image.Image | None
591592
if transform_in_place:
592593
ImageCms.applyTransform(source_image, t, inPlace=True)
593594
result_image = source_image
594595
else:
595596
result_image = ImageCms.applyTransform(source_image, t, inPlace=False)
597+
assert result_image is not None
596598
result_image_aux = result_image.getchannel(preserved_channel)
597599

598600
assert_image_equal(source_image_aux, result_image_aux)
@@ -650,13 +652,15 @@ def test_auxiliary_channels_isolated() -> None:
650652
)
651653

652654
# test conversion from aux-ful source
655+
test_image: Image.Image | None
653656
if transform_in_place:
654657
test_image = source_image.copy()
655658
ImageCms.applyTransform(test_image, test_transform, inPlace=True)
656659
else:
657660
test_image = ImageCms.applyTransform(
658661
source_image, test_transform, inPlace=False
659662
)
663+
assert test_image is not None
660664

661665
# reference conversion from aux-less source
662666
reference_transform = ImageCms.buildTransform(

Tests/test_imagedraw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,8 +1083,8 @@ def test_line_horizontal() -> None:
10831083
)
10841084

10851085

1086+
@pytest.mark.xfail(reason="failing test")
10861087
def test_line_h_s1_w2() -> None:
1087-
pytest.skip("failing")
10881088
img, draw = create_base_image_draw((20, 20))
10891089
draw.line((5, 5, 14, 6), BLACK, 2)
10901090
assert_image_equal_tofile(

0 commit comments

Comments
 (0)