Skip to content

Commit 2963020

Browse files
committed
Assert image type
1 parent e19a149 commit 2963020

26 files changed

+231
-9
lines changed

Tests/test_file_apng.py

+44
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"
@@ -49,60 +51,71 @@ def test_apng_basic() -> None:
4951
)
5052
def test_apng_fdat(filename: str) -> None:
5153
with Image.open(filename) as im:
54+
assert isinstance(im, PngImagePlugin.PngImageFile)
5255
im.seek(im.n_frames - 1)
5356
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
5457
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
5558

5659

5760
def test_apng_dispose() -> None:
5861
with Image.open("Tests/images/apng/dispose_op_none.png") as im:
62+
assert isinstance(im, PngImagePlugin.PngImageFile)
5963
im.seek(im.n_frames - 1)
6064
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
6165
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
6266

6367
with Image.open("Tests/images/apng/dispose_op_background.png") as im:
68+
assert isinstance(im, PngImagePlugin.PngImageFile)
6469
im.seek(im.n_frames - 1)
6570
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
6671
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
6772

6873
with Image.open("Tests/images/apng/dispose_op_background_final.png") as im:
74+
assert isinstance(im, PngImagePlugin.PngImageFile)
6975
im.seek(im.n_frames - 1)
7076
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
7177
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
7278

7379
with Image.open("Tests/images/apng/dispose_op_previous.png") as im:
80+
assert isinstance(im, PngImagePlugin.PngImageFile)
7481
im.seek(im.n_frames - 1)
7582
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
7683
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
7784

7885
with Image.open("Tests/images/apng/dispose_op_previous_final.png") as im:
86+
assert isinstance(im, PngImagePlugin.PngImageFile)
7987
im.seek(im.n_frames - 1)
8088
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
8189
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
8290

8391
with Image.open("Tests/images/apng/dispose_op_previous_first.png") as im:
92+
assert isinstance(im, PngImagePlugin.PngImageFile)
8493
im.seek(im.n_frames - 1)
8594
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
8695
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
8796

8897

8998
def test_apng_dispose_region() -> None:
9099
with Image.open("Tests/images/apng/dispose_op_none_region.png") as im:
100+
assert isinstance(im, PngImagePlugin.PngImageFile)
91101
im.seek(im.n_frames - 1)
92102
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
93103
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
94104

95105
with Image.open("Tests/images/apng/dispose_op_background_before_region.png") as im:
106+
assert isinstance(im, PngImagePlugin.PngImageFile)
96107
im.seek(im.n_frames - 1)
97108
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
98109
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
99110

100111
with Image.open("Tests/images/apng/dispose_op_background_region.png") as im:
112+
assert isinstance(im, PngImagePlugin.PngImageFile)
101113
im.seek(im.n_frames - 1)
102114
assert im.getpixel((0, 0)) == (0, 0, 255, 255)
103115
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
104116

105117
with Image.open("Tests/images/apng/dispose_op_previous_region.png") as im:
118+
assert isinstance(im, PngImagePlugin.PngImageFile)
106119
im.seek(im.n_frames - 1)
107120
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
108121
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
@@ -129,6 +142,7 @@ def test_apng_dispose_op_previous_frame() -> None:
129142
# ],
130143
# )
131144
with Image.open("Tests/images/apng/dispose_op_previous_frame.png") as im:
145+
assert isinstance(im, PngImagePlugin.PngImageFile)
132146
im.seek(im.n_frames - 1)
133147
assert im.getpixel((0, 0)) == (255, 0, 0, 255)
134148

@@ -142,26 +156,31 @@ def test_apng_dispose_op_background_p_mode() -> None:
142156

143157
def test_apng_blend() -> None:
144158
with Image.open("Tests/images/apng/blend_op_source_solid.png") as im:
159+
assert isinstance(im, PngImagePlugin.PngImageFile)
145160
im.seek(im.n_frames - 1)
146161
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
147162
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
148163

149164
with Image.open("Tests/images/apng/blend_op_source_transparent.png") as im:
165+
assert isinstance(im, PngImagePlugin.PngImageFile)
150166
im.seek(im.n_frames - 1)
151167
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
152168
assert im.getpixel((64, 32)) == (0, 0, 0, 0)
153169

154170
with Image.open("Tests/images/apng/blend_op_source_near_transparent.png") as im:
171+
assert isinstance(im, PngImagePlugin.PngImageFile)
155172
im.seek(im.n_frames - 1)
156173
assert im.getpixel((0, 0)) == (0, 255, 0, 2)
157174
assert im.getpixel((64, 32)) == (0, 255, 0, 2)
158175

159176
with Image.open("Tests/images/apng/blend_op_over.png") as im:
177+
assert isinstance(im, PngImagePlugin.PngImageFile)
160178
im.seek(im.n_frames - 1)
161179
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
162180
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
163181

164182
with Image.open("Tests/images/apng/blend_op_over_near_transparent.png") as im:
183+
assert isinstance(im, PngImagePlugin.PngImageFile)
165184
im.seek(im.n_frames - 1)
166185
assert im.getpixel((0, 0)) == (0, 255, 0, 97)
167186
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
@@ -175,6 +194,7 @@ def test_apng_blend_transparency() -> None:
175194

176195
def test_apng_chunk_order() -> None:
177196
with Image.open("Tests/images/apng/fctl_actl.png") as im:
197+
assert isinstance(im, PngImagePlugin.PngImageFile)
178198
im.seek(im.n_frames - 1)
179199
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
180200
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
@@ -230,38 +250,44 @@ def test_apng_num_plays() -> None:
230250

231251
def test_apng_mode() -> None:
232252
with Image.open("Tests/images/apng/mode_16bit.png") as im:
253+
assert isinstance(im, PngImagePlugin.PngImageFile)
233254
assert im.mode == "RGBA"
234255
im.seek(im.n_frames - 1)
235256
assert im.getpixel((0, 0)) == (0, 0, 128, 191)
236257
assert im.getpixel((64, 32)) == (0, 0, 128, 191)
237258

238259
with Image.open("Tests/images/apng/mode_grayscale.png") as im:
260+
assert isinstance(im, PngImagePlugin.PngImageFile)
239261
assert im.mode == "L"
240262
im.seek(im.n_frames - 1)
241263
assert im.getpixel((0, 0)) == 128
242264
assert im.getpixel((64, 32)) == 255
243265

244266
with Image.open("Tests/images/apng/mode_grayscale_alpha.png") as im:
267+
assert isinstance(im, PngImagePlugin.PngImageFile)
245268
assert im.mode == "LA"
246269
im.seek(im.n_frames - 1)
247270
assert im.getpixel((0, 0)) == (128, 191)
248271
assert im.getpixel((64, 32)) == (128, 191)
249272

250273
with Image.open("Tests/images/apng/mode_palette.png") as im:
274+
assert isinstance(im, PngImagePlugin.PngImageFile)
251275
assert im.mode == "P"
252276
im.seek(im.n_frames - 1)
253277
im = im.convert("RGB")
254278
assert im.getpixel((0, 0)) == (0, 255, 0)
255279
assert im.getpixel((64, 32)) == (0, 255, 0)
256280

257281
with Image.open("Tests/images/apng/mode_palette_alpha.png") as im:
282+
assert isinstance(im, PngImagePlugin.PngImageFile)
258283
assert im.mode == "P"
259284
im.seek(im.n_frames - 1)
260285
im = im.convert("RGBA")
261286
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
262287
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
263288

264289
with Image.open("Tests/images/apng/mode_palette_1bit_alpha.png") as im:
290+
assert isinstance(im, PngImagePlugin.PngImageFile)
265291
assert im.mode == "P"
266292
im.seek(im.n_frames - 1)
267293
im = im.convert("RGBA")
@@ -271,52 +297,63 @@ def test_apng_mode() -> None:
271297

272298
def test_apng_chunk_errors() -> None:
273299
with Image.open("Tests/images/apng/chunk_no_actl.png") as im:
300+
assert isinstance(im, PngImagePlugin.PngImageFile)
274301
assert not im.is_animated
275302

276303
with pytest.warns(UserWarning):
277304
with Image.open("Tests/images/apng/chunk_multi_actl.png") as im:
278305
im.load()
306+
assert isinstance(im, PngImagePlugin.PngImageFile)
279307
assert not im.is_animated
280308

281309
with Image.open("Tests/images/apng/chunk_actl_after_idat.png") as im:
310+
assert isinstance(im, PngImagePlugin.PngImageFile)
282311
assert not im.is_animated
283312

284313
with Image.open("Tests/images/apng/chunk_no_fctl.png") as im:
314+
assert isinstance(im, PngImagePlugin.PngImageFile)
285315
with pytest.raises(SyntaxError):
286316
im.seek(im.n_frames - 1)
287317

288318
with Image.open("Tests/images/apng/chunk_repeat_fctl.png") as im:
319+
assert isinstance(im, PngImagePlugin.PngImageFile)
289320
with pytest.raises(SyntaxError):
290321
im.seek(im.n_frames - 1)
291322

292323
with Image.open("Tests/images/apng/chunk_no_fdat.png") as im:
324+
assert isinstance(im, PngImagePlugin.PngImageFile)
293325
with pytest.raises(SyntaxError):
294326
im.seek(im.n_frames - 1)
295327

296328

297329
def test_apng_syntax_errors() -> None:
298330
with pytest.warns(UserWarning):
299331
with Image.open("Tests/images/apng/syntax_num_frames_zero.png") as im:
332+
assert isinstance(im, PngImagePlugin.PngImageFile)
300333
assert not im.is_animated
301334
with pytest.raises(OSError):
302335
im.load()
303336

304337
with pytest.warns(UserWarning):
305338
with Image.open("Tests/images/apng/syntax_num_frames_zero_default.png") as im:
339+
assert isinstance(im, PngImagePlugin.PngImageFile)
306340
assert not im.is_animated
307341
im.load()
308342

309343
# we can handle this case gracefully
310344
with Image.open("Tests/images/apng/syntax_num_frames_low.png") as im:
345+
assert isinstance(im, PngImagePlugin.PngImageFile)
311346
im.seek(im.n_frames - 1)
312347

313348
with pytest.raises(OSError):
314349
with Image.open("Tests/images/apng/syntax_num_frames_high.png") as im:
350+
assert isinstance(im, PngImagePlugin.PngImageFile)
315351
im.seek(im.n_frames - 1)
316352
im.load()
317353

318354
with pytest.warns(UserWarning):
319355
with Image.open("Tests/images/apng/syntax_num_frames_invalid.png") as im:
356+
assert isinstance(im, PngImagePlugin.PngImageFile)
320357
assert not im.is_animated
321358
im.load()
322359

@@ -336,6 +373,7 @@ def test_apng_syntax_errors() -> None:
336373
def test_apng_sequence_errors(test_file: str) -> None:
337374
with pytest.raises(SyntaxError):
338375
with Image.open(f"Tests/images/apng/{test_file}") as im:
376+
assert isinstance(im, PngImagePlugin.PngImageFile)
339377
im.seek(im.n_frames - 1)
340378
im.load()
341379

@@ -346,6 +384,7 @@ def test_apng_save(tmp_path: Path) -> None:
346384
im.save(test_file, save_all=True)
347385

348386
with Image.open(test_file) as im:
387+
assert isinstance(im, PngImagePlugin.PngImageFile)
349388
im.load()
350389
assert not im.is_animated
351390
assert im.n_frames == 1
@@ -361,6 +400,7 @@ def test_apng_save(tmp_path: Path) -> None:
361400
)
362401

363402
with Image.open(test_file) as im:
403+
assert isinstance(im, PngImagePlugin.PngImageFile)
364404
im.load()
365405
assert im.is_animated
366406
assert im.n_frames == 2
@@ -400,6 +440,7 @@ def test_apng_save_split_fdat(tmp_path: Path) -> None:
400440
append_images=frames,
401441
)
402442
with Image.open(test_file) as im:
443+
assert isinstance(im, PngImagePlugin.PngImageFile)
403444
im.seek(im.n_frames - 1)
404445
im.load()
405446

@@ -442,6 +483,7 @@ def test_apng_save_duration_loop(tmp_path: Path) -> None:
442483
test_file, save_all=True, append_images=[frame, frame], duration=[500, 100, 150]
443484
)
444485
with Image.open(test_file) as im:
486+
assert isinstance(im, PngImagePlugin.PngImageFile)
445487
assert im.n_frames == 1
446488
assert "duration" not in im.info
447489

@@ -453,6 +495,7 @@ def test_apng_save_duration_loop(tmp_path: Path) -> None:
453495
duration=[500, 100, 150],
454496
)
455497
with Image.open(test_file) as im:
498+
assert isinstance(im, PngImagePlugin.PngImageFile)
456499
assert im.n_frames == 2
457500
assert im.info["duration"] == 600
458501

@@ -463,6 +506,7 @@ def test_apng_save_duration_loop(tmp_path: Path) -> None:
463506
frame.info["duration"] = 300
464507
frame.save(test_file, save_all=True, append_images=[frame, different_frame])
465508
with Image.open(test_file) as im:
509+
assert isinstance(im, PngImagePlugin.PngImageFile)
466510
assert im.n_frames == 2
467511
assert im.info["duration"] == 600
468512

Tests/test_file_dcx.py

+2
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

+6
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
@@ -223,6 +225,8 @@ def test_showpage() -> None:
223225
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
224226
def test_transparency() -> None:
225227
with Image.open("Tests/images/eps/reqd_showpage.eps") as plot_image:
228+
assert isinstance(plot_image, EpsImagePlugin.EpsImageFile)
229+
226230
plot_image.load(transparency=True)
227231
assert plot_image.mode == "RGBA"
228232

@@ -304,6 +308,7 @@ def test_render_scale2() -> None:
304308

305309
# Zero bounding box
306310
with Image.open(FILE1) as image1_scale2:
311+
assert isinstance(image1_scale2, EpsImagePlugin.EpsImageFile)
307312
image1_scale2.load(scale=2)
308313
with Image.open(FILE1_COMPARE_SCALE2) as image1_scale2_compare:
309314
image1_scale2_compare = image1_scale2_compare.convert("RGB")
@@ -312,6 +317,7 @@ def test_render_scale2() -> None:
312317

313318
# Non-zero bounding box
314319
with Image.open(FILE2) as image2_scale2:
320+
assert isinstance(image2_scale2, EpsImagePlugin.EpsImageFile)
315321
image2_scale2.load(scale=2)
316322
with Image.open(FILE2_COMPARE_SCALE2) as image2_scale2_compare:
317323
image2_scale2_compare = image2_scale2_compare.convert("RGB")

Tests/test_file_fli.py

+7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121

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

3032
with Image.open(animated_test_file) as im:
33+
assert isinstance(im, FliImagePlugin.FliImageFile)
34+
3135
assert im.mode == "P"
3236
assert im.size == (320, 200)
3337
assert im.format == "FLI"
@@ -111,16 +115,19 @@ def test_palette_chunk_second() -> None:
111115

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

117122
with Image.open(animated_test_file) as im:
123+
assert isinstance(im, FliImagePlugin.FliImageFile)
118124
assert im.n_frames == 384
119125
assert im.is_animated
120126

121127

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

126133
# Test seeking past the last frame

0 commit comments

Comments
 (0)