Skip to content

Commit c47b8ba

Browse files
authored
Assert image type (#8845)
2 parents ef8d5d3 + f4cd5e7 commit c47b8ba

File tree

7 files changed

+63
-13
lines changed

7 files changed

+63
-13
lines changed

Tests/test_file_avif.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from PIL import (
1616
AvifImagePlugin,
17+
GifImagePlugin,
1718
Image,
1819
ImageDraw,
1920
ImageFile,
@@ -240,6 +241,7 @@ def test_save_single_frame(self, tmp_path: Path) -> None:
240241
with Image.open("Tests/images/chi.gif") as im:
241242
im.save(temp_file)
242243
with Image.open(temp_file) as im:
244+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
243245
assert im.n_frames == 1
244246

245247
def test_invalid_file(self) -> None:
@@ -598,10 +600,12 @@ def test_n_frames(self) -> None:
598600
"""
599601

600602
with Image.open(TEST_AVIF_FILE) as im:
603+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
601604
assert im.n_frames == 1
602605
assert not im.is_animated
603606

604607
with Image.open("Tests/images/avif/star.avifs") as im:
608+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
605609
assert im.n_frames == 5
606610
assert im.is_animated
607611

@@ -612,11 +616,13 @@ def test_write_animation_P(self, tmp_path: Path) -> None:
612616
"""
613617

614618
with Image.open("Tests/images/avif/star.gif") as original:
619+
assert isinstance(original, GifImagePlugin.GifImageFile)
615620
assert original.n_frames > 1
616621

617622
temp_file = tmp_path / "temp.avif"
618623
original.save(temp_file, save_all=True)
619624
with Image.open(temp_file) as im:
625+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
620626
assert im.n_frames == original.n_frames
621627

622628
# Compare first frame in P mode to frame from original GIF
@@ -636,6 +642,7 @@ def test_write_animation_RGBA(self, tmp_path: Path) -> None:
636642

637643
def check(temp_file: Path) -> None:
638644
with Image.open(temp_file) as im:
645+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
639646
assert im.n_frames == 4
640647

641648
# Compare first frame to original
@@ -708,6 +715,7 @@ def test_timestamp_and_duration(self, tmp_path: Path) -> None:
708715
)
709716

710717
with Image.open(temp_file) as im:
718+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
711719
assert im.n_frames == 5
712720
assert im.is_animated
713721

@@ -737,6 +745,7 @@ def test_seeking(self, tmp_path: Path) -> None:
737745
)
738746

739747
with Image.open(temp_file) as im:
748+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
740749
assert im.n_frames == 5
741750
assert im.is_animated
742751

Tests/test_file_fli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ def test_sanity() -> None:
4848
def test_prefix_chunk(monkeypatch: pytest.MonkeyPatch) -> None:
4949
monkeypatch.setattr(ImageFile, "LOAD_TRUNCATED_IMAGES", True)
5050
with Image.open(animated_test_file_with_prefix_chunk) as im:
51+
assert isinstance(im, FliImagePlugin.FliImageFile)
5152
assert im.mode == "P"
5253
assert im.size == (320, 200)
5354
assert im.format == "FLI"
5455
assert im.info["duration"] == 171
5556
assert im.is_animated
5657

5758
palette = im.getpalette()
59+
assert palette is not None
5860
assert palette[3:6] == [255, 255, 255]
5961
assert palette[381:384] == [204, 204, 12]
6062
assert palette[765:] == [252, 0, 0]

Tests/test_file_gif.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ def test_roundtrip_save_all(tmp_path: Path) -> None:
293293
im.save(out, save_all=True)
294294

295295
with Image.open(out) as reread:
296+
assert isinstance(reread, GifImagePlugin.GifImageFile)
296297
assert reread.n_frames == 5
297298

298299

@@ -1374,6 +1375,7 @@ def test_palette_save_all_P(tmp_path: Path) -> None:
13741375

13751376
with Image.open(out) as im:
13761377
# Assert that the frames are correct, and each frame has the same palette
1378+
assert isinstance(im, GifImagePlugin.GifImageFile)
13771379
assert_image_equal(im.convert("RGB"), frames[0].convert("RGB"))
13781380
assert im.palette is not None
13791381
assert im.global_palette is not None

Tests/test_file_jpeg.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,10 @@ def test_exif_gps(self, tmp_path: Path) -> None:
330330

331331
# Reading
332332
with Image.open("Tests/images/exif_gps.jpg") as im:
333-
exif = im._getexif()
334-
assert exif[gps_index] == expected_exif_gps
333+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
334+
exif_data = im._getexif()
335+
assert exif_data is not None
336+
assert exif_data[gps_index] == expected_exif_gps
335337

336338
# Writing
337339
f = tmp_path / "temp.jpg"
@@ -340,8 +342,10 @@ def test_exif_gps(self, tmp_path: Path) -> None:
340342
hopper().save(f, exif=exif)
341343

342344
with Image.open(f) as reloaded:
343-
exif = reloaded._getexif()
344-
assert exif[gps_index] == expected_exif_gps
345+
assert isinstance(reloaded, JpegImagePlugin.JpegImageFile)
346+
exif_data = reloaded._getexif()
347+
assert exif_data is not None
348+
assert exif_data[gps_index] == expected_exif_gps
345349

346350
def test_empty_exif_gps(self) -> None:
347351
with Image.open("Tests/images/empty_gps_ifd.jpg") as im:
@@ -368,6 +372,7 @@ def test_exif_equality(self) -> None:
368372
exifs = []
369373
for i in range(2):
370374
with Image.open("Tests/images/exif-200dpcm.jpg") as im:
375+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
371376
exifs.append(im._getexif())
372377
assert exifs[0] == exifs[1]
373378

@@ -401,13 +406,17 @@ def test_exif_rollback(self) -> None:
401406
}
402407

403408
with Image.open("Tests/images/exif_gps.jpg") as im:
409+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
404410
exif = im._getexif()
411+
assert exif is not None
405412

406413
for tag, value in expected_exif.items():
407414
assert value == exif[tag]
408415

409416
def test_exif_gps_typeerror(self) -> None:
410417
with Image.open("Tests/images/exif_gps_typeerror.jpg") as im:
418+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
419+
411420
# Should not raise a TypeError
412421
im._getexif()
413422

@@ -487,7 +496,9 @@ def getsampling(
487496

488497
def test_exif(self) -> None:
489498
with Image.open("Tests/images/pil_sample_rgb.jpg") as im:
499+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
490500
info = im._getexif()
501+
assert info is not None
491502
assert info[305] == "Adobe Photoshop CS Macintosh"
492503

493504
def test_get_child_images(self) -> None:
@@ -690,11 +701,13 @@ def test_load_16bit_qtables(self) -> None:
690701

691702
def test_save_multiple_16bit_qtables(self) -> None:
692703
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
704+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
693705
im2 = self.roundtrip(im, qtables="keep")
694706
assert im.quantization == im2.quantization
695707

696708
def test_save_single_16bit_qtable(self) -> None:
697709
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
710+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
698711
im2 = self.roundtrip(im, qtables={0: im.quantization[0]})
699712
assert len(im2.quantization) == 1
700713
assert im2.quantization[0] == im.quantization[0]
@@ -898,7 +911,10 @@ def test_ifd_offset_exif(self) -> None:
898911
# in contrast to normal 8
899912
with Image.open("Tests/images/exif-ifd-offset.jpg") as im:
900913
# Act / Assert
901-
assert im._getexif()[306] == "2017:03:13 23:03:09"
914+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
915+
exif = im._getexif()
916+
assert exif is not None
917+
assert exif[306] == "2017:03:13 23:03:09"
902918

903919
def test_multiple_exif(self) -> None:
904920
with Image.open("Tests/images/multiple_exif.jpg") as im:

Tests/test_file_mpo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ def test_ignore_frame_size() -> None:
120120
# Ignore the different size of the second frame
121121
# since this is not a "Large Thumbnail" image
122122
with Image.open("Tests/images/ignore_frame_size.mpo") as im:
123+
assert isinstance(im, MpoImagePlugin.MpoImageFile)
123124
assert im.size == (64, 64)
124125

125126
im.seek(1)
127+
assert im.mpinfo is not None
126128
assert (
127129
im.mpinfo[0xB002][1]["Attribute"]["MPType"]
128130
== "Multi-Frame Image: (Disparity)"
@@ -155,6 +157,7 @@ def test_reload_exif_after_seek() -> None:
155157
@pytest.mark.parametrize("test_file", test_files)
156158
def test_mp(test_file: str) -> None:
157159
with Image.open(test_file) as im:
160+
assert isinstance(im, MpoImagePlugin.MpoImageFile)
158161
mpinfo = im._getmp()
159162
assert mpinfo is not None
160163
assert mpinfo[45056] == b"0100"
@@ -165,6 +168,7 @@ def test_mp_offset() -> None:
165168
# This image has been manually hexedited to have an IFD offset of 10
166169
# in APP2 data, in contrast to normal 8
167170
with Image.open("Tests/images/sugarshack_ifd_offset.mpo") as im:
171+
assert isinstance(im, MpoImagePlugin.MpoImageFile)
168172
mpinfo = im._getmp()
169173
assert mpinfo is not None
170174
assert mpinfo[45056] == b"0100"
@@ -182,6 +186,7 @@ def test_mp_no_data() -> None:
182186
@pytest.mark.parametrize("test_file", test_files)
183187
def test_mp_attribute(test_file: str) -> None:
184188
with Image.open(test_file) as im:
189+
assert isinstance(im, MpoImagePlugin.MpoImageFile)
185190
mpinfo = im._getmp()
186191
assert mpinfo is not None
187192
for frame_number, mpentry in enumerate(mpinfo[0xB002]):

Tests/test_file_png.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ def test_specify_bits(self, save_all: bool, tmp_path: Path) -> None:
671671
im.save(out, bits=4, save_all=save_all)
672672

673673
with Image.open(out) as reloaded:
674+
assert isinstance(reloaded, PngImagePlugin.PngImageFile)
675+
assert reloaded.png is not None
676+
assert reloaded.png.im_palette is not None
674677
assert len(reloaded.png.im_palette[1]) == 48
675678

676679
def test_plte_length(self, tmp_path: Path) -> None:
@@ -681,6 +684,9 @@ def test_plte_length(self, tmp_path: Path) -> None:
681684
im.save(out)
682685

683686
with Image.open(out) as reloaded:
687+
assert isinstance(reloaded, PngImagePlugin.PngImageFile)
688+
assert reloaded.png is not None
689+
assert reloaded.png.im_palette is not None
684690
assert len(reloaded.png.im_palette[1]) == 3
685691

686692
def test_getxmp(self) -> None:
@@ -702,13 +708,17 @@ def test_getxmp(self) -> None:
702708
def test_exif(self) -> None:
703709
# With an EXIF chunk
704710
with Image.open("Tests/images/exif.png") as im:
705-
exif = im._getexif()
706-
assert exif[274] == 1
711+
assert isinstance(im, PngImagePlugin.PngImageFile)
712+
exif_data = im._getexif()
713+
assert exif_data is not None
714+
assert exif_data[274] == 1
707715

708716
# With an ImageMagick zTXt chunk
709717
with Image.open("Tests/images/exif_imagemagick.png") as im:
710-
exif = im._getexif()
711-
assert exif[274] == 1
718+
assert isinstance(im, PngImagePlugin.PngImageFile)
719+
exif_data = im._getexif()
720+
assert exif_data is not None
721+
assert exif_data[274] == 1
712722

713723
# Assert that info still can be extracted
714724
# when the image is no longer a PngImageFile instance
@@ -717,8 +727,10 @@ def test_exif(self) -> None:
717727

718728
# With a tEXt chunk
719729
with Image.open("Tests/images/exif_text.png") as im:
720-
exif = im._getexif()
721-
assert exif[274] == 1
730+
assert isinstance(im, PngImagePlugin.PngImageFile)
731+
exif_data = im._getexif()
732+
assert exif_data is not None
733+
assert exif_data[274] == 1
722734

723735
# With XMP tags
724736
with Image.open("Tests/images/xmp_tags_orientation.png") as im:
@@ -740,8 +752,10 @@ def test_exif_save(self, tmp_path: Path) -> None:
740752
im.save(test_file, exif=im.getexif())
741753

742754
with Image.open(test_file) as reloaded:
743-
exif = reloaded._getexif()
744-
assert exif[274] == 1
755+
assert isinstance(reloaded, PngImagePlugin.PngImageFile)
756+
exif_data = reloaded._getexif()
757+
assert exif_data is not None
758+
assert exif_data[274] == 1
745759

746760
@mark_if_feature_version(
747761
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"

Tests/test_file_webp_metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
def test_read_exif_metadata() -> None:
2323
file_path = "Tests/images/flower.webp"
2424
with Image.open(file_path) as image:
25+
assert isinstance(image, WebPImagePlugin.WebPImageFile)
2526
assert image.format == "WEBP"
2627
exif_data = image.info.get("exif", None)
2728
assert exif_data
2829

2930
exif = image._getexif()
31+
assert exif is not None
3032

3133
# Camera make
3234
assert exif[271] == "Canon"

0 commit comments

Comments
 (0)