Skip to content

Commit 1148b7a

Browse files
authored
Check return types (#9045)
2 parents 7a6664d + a66d0d1 commit 1148b7a

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

Tests/test_file_avif.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def test_file_pointer_could_be_reused(self) -> None:
221221
def test_background_from_gif(self, tmp_path: Path) -> None:
222222
with Image.open("Tests/images/chi.gif") as im:
223223
original_value = im.convert("RGB").getpixel((1, 1))
224+
assert isinstance(original_value, tuple)
224225

225226
# Save as AVIF
226227
out_avif = tmp_path / "temp.avif"
@@ -233,6 +234,7 @@ def test_background_from_gif(self, tmp_path: Path) -> None:
233234

234235
with Image.open(out_gif) as reread:
235236
reread_value = reread.convert("RGB").getpixel((1, 1))
237+
assert isinstance(reread_value, tuple)
236238
difference = sum([abs(original_value[i] - reread_value[i]) for i in range(3)])
237239
assert difference <= 6
238240

Tests/test_file_png.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ def test_load_transparent_p(self) -> None:
229229
assert_image(im, "RGBA", (162, 150))
230230

231231
# image has 124 unique alpha values
232-
assert len(im.getchannel("A").getcolors()) == 124
232+
colors = im.getchannel("A").getcolors()
233+
assert colors is not None
234+
assert len(colors) == 124
233235

234236
def test_load_transparent_rgb(self) -> None:
235237
test_file = "Tests/images/rgb_trns.png"
@@ -241,7 +243,9 @@ def test_load_transparent_rgb(self) -> None:
241243
assert_image(im, "RGBA", (64, 64))
242244

243245
# image has 876 transparent pixels
244-
assert im.getchannel("A").getcolors()[0][0] == 876
246+
colors = im.getchannel("A").getcolors()
247+
assert colors is not None
248+
assert colors[0][0] == 876
245249

246250
def test_save_p_transparent_palette(self, tmp_path: Path) -> None:
247251
in_file = "Tests/images/pil123p.png"
@@ -262,7 +266,9 @@ def test_save_p_transparent_palette(self, tmp_path: Path) -> None:
262266
assert_image(im, "RGBA", (162, 150))
263267

264268
# image has 124 unique alpha values
265-
assert len(im.getchannel("A").getcolors()) == 124
269+
colors = im.getchannel("A").getcolors()
270+
assert colors is not None
271+
assert len(colors) == 124
266272

267273
def test_save_p_single_transparency(self, tmp_path: Path) -> None:
268274
in_file = "Tests/images/p_trns_single.png"
@@ -285,7 +291,9 @@ def test_save_p_single_transparency(self, tmp_path: Path) -> None:
285291
assert im.getpixel((31, 31)) == (0, 255, 52, 0)
286292

287293
# image has 876 transparent pixels
288-
assert im.getchannel("A").getcolors()[0][0] == 876
294+
colors = im.getchannel("A").getcolors()
295+
assert colors is not None
296+
assert colors[0][0] == 876
289297

290298
def test_save_p_transparent_black(self, tmp_path: Path) -> None:
291299
# check if solid black image with full transparency
@@ -313,7 +321,9 @@ def test_save_grayscale_transparency(self, tmp_path: Path) -> None:
313321
assert im.info["transparency"] == 255
314322

315323
im_rgba = im.convert("RGBA")
316-
assert im_rgba.getchannel("A").getcolors()[0][0] == num_transparent
324+
colors = im_rgba.getchannel("A").getcolors()
325+
assert colors is not None
326+
assert colors[0][0] == num_transparent
317327

318328
test_file = tmp_path / "temp.png"
319329
im.save(test_file)
@@ -324,7 +334,9 @@ def test_save_grayscale_transparency(self, tmp_path: Path) -> None:
324334
assert_image_equal(im, test_im)
325335

326336
test_im_rgba = test_im.convert("RGBA")
327-
assert test_im_rgba.getchannel("A").getcolors()[0][0] == num_transparent
337+
colors = test_im_rgba.getchannel("A").getcolors()
338+
assert colors is not None
339+
assert colors[0][0] == num_transparent
328340

329341
def test_save_rgb_single_transparency(self, tmp_path: Path) -> None:
330342
in_file = "Tests/images/caption_6_33_22.png"

Tests/test_file_tga.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,17 @@ def test_save_l_transparency(tmp_path: Path) -> None:
274274
in_file = "Tests/images/la.tga"
275275
with Image.open(in_file) as im:
276276
assert im.mode == "LA"
277-
assert im.getchannel("A").getcolors()[0][0] == num_transparent
277+
colors = im.getchannel("A").getcolors()
278+
assert colors is not None
279+
assert colors[0][0] == num_transparent
278280

279281
out = tmp_path / "temp.tga"
280282
im.save(out)
281283

282284
with Image.open(out) as test_im:
283285
assert test_im.mode == "LA"
284-
assert test_im.getchannel("A").getcolors()[0][0] == num_transparent
286+
colors = test_im.getchannel("A").getcolors()
287+
assert colors is not None
288+
assert colors[0][0] == num_transparent
285289

286290
assert_image_equal(im, test_im)

Tests/test_image_putpalette.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_putpalette_with_alpha_values() -> None:
6262
expected = im.convert("RGBA")
6363

6464
palette = im.getpalette()
65+
assert palette is not None
6566
transparency = im.info.pop("transparency")
6667

6768
palette_with_alpha_values = []

Tests/test_imagesequence.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,14 @@ def test_consecutive() -> None:
7676
def test_palette_mmap() -> None:
7777
# Using mmap in ImageFile can require to reload the palette.
7878
with Image.open("Tests/images/multipage-mmap.tiff") as im:
79-
color1 = im.getpalette()[:3]
79+
palette = im.getpalette()
80+
assert palette is not None
81+
color1 = palette[:3]
8082
im.seek(0)
81-
color2 = im.getpalette()[:3]
83+
84+
palette = im.getpalette()
85+
assert palette is not None
86+
color2 = palette[:3]
8287
assert color1 == color2
8388

8489

0 commit comments

Comments
 (0)