Skip to content

Commit 14fb62e

Browse files
authored
Assert image type (#8619)
1 parent bce83ac commit 14fb62e

26 files changed

+236
-9
lines changed

Tests/test_file_apng.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# (referenced from https://wiki.mozilla.org/APNG_Specification)
1313
def test_apng_basic() -> None:
1414
with Image.open("Tests/images/apng/single_frame.png") as im:
15+
assert isinstance(im, PngImagePlugin.PngImageFile)
1516
assert not im.is_animated
1617
assert im.n_frames == 1
1718
assert im.get_format_mimetype() == "image/apng"
@@ -20,6 +21,7 @@ def test_apng_basic() -> None:
2021
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
2122

2223
with Image.open("Tests/images/apng/single_frame_default.png") as im:
24+
assert isinstance(im, PngImagePlugin.PngImageFile)
2325
assert im.is_animated
2426
assert im.n_frames == 2
2527
assert im.get_format_mimetype() == "image/apng"
@@ -52,60 +54,71 @@ def test_apng_basic() -> None:
5254
)
5355
def test_apng_fdat(filename: str) -> None:
5456
with Image.open(filename) as im:
57+
assert isinstance(im, PngImagePlugin.PngImageFile)
5558
im.seek(im.n_frames - 1)
5659
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
5760
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
5861

5962

6063
def test_apng_dispose() -> None:
6164
with Image.open("Tests/images/apng/dispose_op_none.png") as im:
65+
assert isinstance(im, PngImagePlugin.PngImageFile)
6266
im.seek(im.n_frames - 1)
6367
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
6468
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
6569

6670
with Image.open("Tests/images/apng/dispose_op_background.png") as im:
71+
assert isinstance(im, PngImagePlugin.PngImageFile)
6772
im.seek(im.n_frames - 1)
6873
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
6974
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
7075

7176
with Image.open("Tests/images/apng/dispose_op_background_final.png") as im:
77+
assert isinstance(im, PngImagePlugin.PngImageFile)
7278
im.seek(im.n_frames - 1)
7379
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
7480
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
7581

7682
with Image.open("Tests/images/apng/dispose_op_previous.png") as im:
83+
assert isinstance(im, PngImagePlugin.PngImageFile)
7784
im.seek(im.n_frames - 1)
7885
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
7986
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
8087

8188
with Image.open("Tests/images/apng/dispose_op_previous_final.png") as im:
89+
assert isinstance(im, PngImagePlugin.PngImageFile)
8290
im.seek(im.n_frames - 1)
8391
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
8492
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
8593

8694
with Image.open("Tests/images/apng/dispose_op_previous_first.png") as im:
95+
assert isinstance(im, PngImagePlugin.PngImageFile)
8796
im.seek(im.n_frames - 1)
8897
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
8998
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
9099

91100

92101
def test_apng_dispose_region() -> None:
93102
with Image.open("Tests/images/apng/dispose_op_none_region.png") as im:
103+
assert isinstance(im, PngImagePlugin.PngImageFile)
94104
im.seek(im.n_frames - 1)
95105
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
96106
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
97107

98108
with Image.open("Tests/images/apng/dispose_op_background_before_region.png") as im:
109+
assert isinstance(im, PngImagePlugin.PngImageFile)
99110
im.seek(im.n_frames - 1)
100111
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
101112
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
102113

103114
with Image.open("Tests/images/apng/dispose_op_background_region.png") as im:
115+
assert isinstance(im, PngImagePlugin.PngImageFile)
104116
im.seek(im.n_frames - 1)
105117
assert im.getpixel((0, 0)) == (0, 0, 255, 255)
106118
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
107119

108120
with Image.open("Tests/images/apng/dispose_op_previous_region.png") as im:
121+
assert isinstance(im, PngImagePlugin.PngImageFile)
109122
im.seek(im.n_frames - 1)
110123
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
111124
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
@@ -132,6 +145,7 @@ def test_apng_dispose_op_previous_frame() -> None:
132145
# ],
133146
# )
134147
with Image.open("Tests/images/apng/dispose_op_previous_frame.png") as im:
148+
assert isinstance(im, PngImagePlugin.PngImageFile)
135149
im.seek(im.n_frames - 1)
136150
assert im.getpixel((0, 0)) == (255, 0, 0, 255)
137151

@@ -145,26 +159,31 @@ def test_apng_dispose_op_background_p_mode() -> None:
145159

146160
def test_apng_blend() -> None:
147161
with Image.open("Tests/images/apng/blend_op_source_solid.png") as im:
162+
assert isinstance(im, PngImagePlugin.PngImageFile)
148163
im.seek(im.n_frames - 1)
149164
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
150165
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
151166

152167
with Image.open("Tests/images/apng/blend_op_source_transparent.png") as im:
168+
assert isinstance(im, PngImagePlugin.PngImageFile)
153169
im.seek(im.n_frames - 1)
154170
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
155171
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
156172

157173
with Image.open("Tests/images/apng/blend_op_source_near_transparent.png") as im:
174+
assert isinstance(im, PngImagePlugin.PngImageFile)
158175
im.seek(im.n_frames - 1)
159176
assert im.getpixel((0, 0)) == (0, 255, 0, 2)
160177
assert im.getpixel((64, 32)) == (0, 255, 0, 2)
161178

162179
with Image.open("Tests/images/apng/blend_op_over.png") as im:
180+
assert isinstance(im, PngImagePlugin.PngImageFile)
163181
im.seek(im.n_frames - 1)
164182
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
165183
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
166184

167185
with Image.open("Tests/images/apng/blend_op_over_near_transparent.png") as im:
186+
assert isinstance(im, PngImagePlugin.PngImageFile)
168187
im.seek(im.n_frames - 1)
169188
assert im.getpixel((0, 0)) == (0, 255, 0, 97)
170189
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
@@ -178,6 +197,7 @@ def test_apng_blend_transparency() -> None:
178197

179198
def test_apng_chunk_order() -> None:
180199
with Image.open("Tests/images/apng/fctl_actl.png") as im:
200+
assert isinstance(im, PngImagePlugin.PngImageFile)
181201
im.seek(im.n_frames - 1)
182202
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
183203
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
@@ -233,38 +253,44 @@ def test_apng_num_plays() -> None:
233253

234254
def test_apng_mode() -> None:
235255
with Image.open("Tests/images/apng/mode_16bit.png") as im:
256+
assert isinstance(im, PngImagePlugin.PngImageFile)
236257
assert im.mode == "RGBA"
237258
im.seek(im.n_frames - 1)
238259
assert im.getpixel((0, 0)) == (0, 0, 128, 191)
239260
assert im.getpixel((64, 32)) == (0, 0, 128, 191)
240261

241262
with Image.open("Tests/images/apng/mode_grayscale.png") as im:
263+
assert isinstance(im, PngImagePlugin.PngImageFile)
242264
assert im.mode == "L"
243265
im.seek(im.n_frames - 1)
244266
assert im.getpixel((0, 0)) == 128
245267
assert im.getpixel((64, 32)) == 255
246268

247269
with Image.open("Tests/images/apng/mode_grayscale_alpha.png") as im:
270+
assert isinstance(im, PngImagePlugin.PngImageFile)
248271
assert im.mode == "LA"
249272
im.seek(im.n_frames - 1)
250273
assert im.getpixel((0, 0)) == (128, 191)
251274
assert im.getpixel((64, 32)) == (128, 191)
252275

253276
with Image.open("Tests/images/apng/mode_palette.png") as im:
277+
assert isinstance(im, PngImagePlugin.PngImageFile)
254278
assert im.mode == "P"
255279
im.seek(im.n_frames - 1)
256280
im = im.convert("RGB")
257281
assert im.getpixel((0, 0)) == (0, 255, 0)
258282
assert im.getpixel((64, 32)) == (0, 255, 0)
259283

260284
with Image.open("Tests/images/apng/mode_palette_alpha.png") as im:
285+
assert isinstance(im, PngImagePlugin.PngImageFile)
261286
assert im.mode == "P"
262287
im.seek(im.n_frames - 1)
263288
im = im.convert("RGBA")
264289
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
265290
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
266291

267292
with Image.open("Tests/images/apng/mode_palette_1bit_alpha.png") as im:
293+
assert isinstance(im, PngImagePlugin.PngImageFile)
268294
assert im.mode == "P"
269295
im.seek(im.n_frames - 1)
270296
im = im.convert("RGBA")
@@ -274,52 +300,63 @@ def test_apng_mode() -> None:
274300

275301
def test_apng_chunk_errors() -> None:
276302
with Image.open("Tests/images/apng/chunk_no_actl.png") as im:
303+
assert isinstance(im, PngImagePlugin.PngImageFile)
277304
assert not im.is_animated
278305

279306
with pytest.warns(UserWarning):
280307
with Image.open("Tests/images/apng/chunk_multi_actl.png") as im:
281308
im.load()
309+
assert isinstance(im, PngImagePlugin.PngImageFile)
282310
assert not im.is_animated
283311

284312
with Image.open("Tests/images/apng/chunk_actl_after_idat.png") as im:
313+
assert isinstance(im, PngImagePlugin.PngImageFile)
285314
assert not im.is_animated
286315

287316
with Image.open("Tests/images/apng/chunk_no_fctl.png") as im:
317+
assert isinstance(im, PngImagePlugin.PngImageFile)
288318
with pytest.raises(SyntaxError):
289319
im.seek(im.n_frames - 1)
290320

291321
with Image.open("Tests/images/apng/chunk_repeat_fctl.png") as im:
322+
assert isinstance(im, PngImagePlugin.PngImageFile)
292323
with pytest.raises(SyntaxError):
293324
im.seek(im.n_frames - 1)
294325

295326
with Image.open("Tests/images/apng/chunk_no_fdat.png") as im:
327+
assert isinstance(im, PngImagePlugin.PngImageFile)
296328
with pytest.raises(SyntaxError):
297329
im.seek(im.n_frames - 1)
298330

299331

300332
def test_apng_syntax_errors() -> None:
301333
with pytest.warns(UserWarning):
302334
with Image.open("Tests/images/apng/syntax_num_frames_zero.png") as im:
335+
assert isinstance(im, PngImagePlugin.PngImageFile)
303336
assert not im.is_animated
304337
with pytest.raises(OSError):
305338
im.load()
306339

307340
with pytest.warns(UserWarning):
308341
with Image.open("Tests/images/apng/syntax_num_frames_zero_default.png") as im:
342+
assert isinstance(im, PngImagePlugin.PngImageFile)
309343
assert not im.is_animated
310344
im.load()
311345

312346
# we can handle this case gracefully
313347
with Image.open("Tests/images/apng/syntax_num_frames_low.png") as im:
348+
assert isinstance(im, PngImagePlugin.PngImageFile)
314349
im.seek(im.n_frames - 1)
315350

316351
with pytest.raises(OSError):
317352
with Image.open("Tests/images/apng/syntax_num_frames_high.png") as im:
353+
assert isinstance(im, PngImagePlugin.PngImageFile)
318354
im.seek(im.n_frames - 1)
319355
im.load()
320356

321357
with pytest.warns(UserWarning):
322358
with Image.open("Tests/images/apng/syntax_num_frames_invalid.png") as im:
359+
assert isinstance(im, PngImagePlugin.PngImageFile)
323360
assert not im.is_animated
324361
im.load()
325362

@@ -339,6 +376,7 @@ def test_apng_syntax_errors() -> None:
339376
def test_apng_sequence_errors(test_file: str) -> None:
340377
with pytest.raises(SyntaxError):
341378
with Image.open(f"Tests/images/apng/{test_file}") as im:
379+
assert isinstance(im, PngImagePlugin.PngImageFile)
342380
im.seek(im.n_frames - 1)
343381
im.load()
344382

@@ -349,6 +387,7 @@ def test_apng_save(tmp_path: Path) -> None:
349387
im.save(test_file, save_all=True)
350388

351389
with Image.open(test_file) as im:
390+
assert isinstance(im, PngImagePlugin.PngImageFile)
352391
im.load()
353392
assert not im.is_animated
354393
assert im.n_frames == 1
@@ -364,6 +403,7 @@ def test_apng_save(tmp_path: Path) -> None:
364403
)
365404

366405
with Image.open(test_file) as im:
406+
assert isinstance(im, PngImagePlugin.PngImageFile)
367407
im.load()
368408
assert im.is_animated
369409
assert im.n_frames == 2
@@ -403,6 +443,7 @@ def test_apng_save_split_fdat(tmp_path: Path) -> None:
403443
append_images=frames,
404444
)
405445
with Image.open(test_file) as im:
446+
assert isinstance(im, PngImagePlugin.PngImageFile)
406447
im.seek(im.n_frames - 1)
407448
im.load()
408449

@@ -445,6 +486,7 @@ def test_apng_save_duration_loop(tmp_path: Path) -> None:
445486
test_file, save_all=True, append_images=[frame, frame], duration=[500, 100, 150]
446487
)
447488
with Image.open(test_file) as im:
489+
assert isinstance(im, PngImagePlugin.PngImageFile)
448490
assert im.n_frames == 1
449491
assert "duration" not in im.info
450492

@@ -456,6 +498,7 @@ def test_apng_save_duration_loop(tmp_path: Path) -> None:
456498
duration=[500, 100, 150],
457499
)
458500
with Image.open(test_file) as im:
501+
assert isinstance(im, PngImagePlugin.PngImageFile)
459502
assert im.n_frames == 2
460503
assert im.info["duration"] == 600
461504

@@ -466,6 +509,7 @@ def test_apng_save_duration_loop(tmp_path: Path) -> None:
466509
frame.info["duration"] = 300
467510
frame.save(test_file, save_all=True, append_images=[frame, different_frame])
468511
with Image.open(test_file) as im:
512+
assert isinstance(im, PngImagePlugin.PngImageFile)
469513
assert im.n_frames == 2
470514
assert im.info["duration"] == 600
471515

Tests/test_file_dcx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ def test_tell() -> None:
6969

7070
def test_n_frames() -> None:
7171
with Image.open(TEST_FILE) as im:
72+
assert isinstance(im, DcxImagePlugin.DcxImageFile)
7273
assert im.n_frames == 1
7374
assert not im.is_animated
7475

7576

7677
def test_eoferror() -> None:
7778
with Image.open(TEST_FILE) as im:
79+
assert isinstance(im, DcxImagePlugin.DcxImageFile)
7880
n_frames = im.n_frames
7981

8082
# Test seeking past the last frame

Tests/test_file_eps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
def test_sanity(filename: str, size: tuple[int, int], scale: int) -> None:
8787
expected_size = tuple(s * scale for s in size)
8888
with Image.open(filename) as image:
89+
assert isinstance(image, EpsImagePlugin.EpsImageFile)
90+
8991
image.load(scale=scale)
9092
assert image.mode == "RGB"
9193
assert image.size == expected_size
@@ -227,6 +229,8 @@ def test_showpage() -> None:
227229
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
228230
def test_transparency() -> None:
229231
with Image.open("Tests/images/eps/reqd_showpage.eps") as plot_image:
232+
assert isinstance(plot_image, EpsImagePlugin.EpsImageFile)
233+
230234
plot_image.load(transparency=True)
231235
assert plot_image.mode == "RGBA"
232236

@@ -308,6 +312,7 @@ def test_render_scale2() -> None:
308312

309313
# Zero bounding box
310314
with Image.open(FILE1) as image1_scale2:
315+
assert isinstance(image1_scale2, EpsImagePlugin.EpsImageFile)
311316
image1_scale2.load(scale=2)
312317
with Image.open(FILE1_COMPARE_SCALE2) as image1_scale2_compare:
313318
image1_scale2_compare = image1_scale2_compare.convert("RGB")
@@ -316,6 +321,7 @@ def test_render_scale2() -> None:
316321

317322
# Non-zero bounding box
318323
with Image.open(FILE2) as image2_scale2:
324+
assert isinstance(image2_scale2, EpsImagePlugin.EpsImageFile)
319325
image2_scale2.load(scale=2)
320326
with Image.open(FILE2_COMPARE_SCALE2) as image2_scale2_compare:
321327
image2_scale2_compare = image2_scale2_compare.convert("RGB")

Tests/test_file_fli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@
2222

2323
def test_sanity() -> None:
2424
with Image.open(static_test_file) as im:
25+
assert isinstance(im, FliImagePlugin.FliImageFile)
26+
2527
im.load()
2628
assert im.mode == "P"
2729
assert im.size == (128, 128)
2830
assert im.format == "FLI"
2931
assert not im.is_animated
3032

3133
with Image.open(animated_test_file) as im:
34+
assert isinstance(im, FliImagePlugin.FliImageFile)
35+
3236
assert im.mode == "P"
3337
assert im.size == (320, 200)
3438
assert im.format == "FLI"
@@ -112,16 +116,19 @@ def test_palette_chunk_second() -> None:
112116

113117
def test_n_frames() -> None:
114118
with Image.open(static_test_file) as im:
119+
assert isinstance(im, FliImagePlugin.FliImageFile)
115120
assert im.n_frames == 1
116121
assert not im.is_animated
117122

118123
with Image.open(animated_test_file) as im:
124+
assert isinstance(im, FliImagePlugin.FliImageFile)
119125
assert im.n_frames == 384
120126
assert im.is_animated
121127

122128

123129
def test_eoferror() -> None:
124130
with Image.open(animated_test_file) as im:
131+
assert isinstance(im, FliImagePlugin.FliImageFile)
125132
n_frames = im.n_frames
126133

127134
# Test seeking past the last frame
@@ -166,6 +173,7 @@ def test_seek_tell() -> None:
166173

167174
def test_seek() -> None:
168175
with Image.open(animated_test_file) as im:
176+
assert isinstance(im, FliImagePlugin.FliImageFile)
169177
im.seek(50)
170178

171179
assert_image_equal_tofile(im, "Tests/images/a_fli.png")

0 commit comments

Comments
 (0)