Skip to content

Commit f4cd5e7

Browse files
committed
Assert image type
1 parent 7e15c54 commit f4cd5e7

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-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:
@@ -595,10 +597,12 @@ def test_n_frames(self) -> None:
595597
"""
596598

597599
with Image.open(TEST_AVIF_FILE) as im:
600+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
598601
assert im.n_frames == 1
599602
assert not im.is_animated
600603

601604
with Image.open("Tests/images/avif/star.avifs") as im:
605+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
602606
assert im.n_frames == 5
603607
assert im.is_animated
604608

@@ -609,11 +613,13 @@ def test_write_animation_P(self, tmp_path: Path) -> None:
609613
"""
610614

611615
with Image.open("Tests/images/avif/star.gif") as original:
616+
assert isinstance(original, GifImagePlugin.GifImageFile)
612617
assert original.n_frames > 1
613618

614619
temp_file = tmp_path / "temp.avif"
615620
original.save(temp_file, save_all=True)
616621
with Image.open(temp_file) as im:
622+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
617623
assert im.n_frames == original.n_frames
618624

619625
# Compare first frame in P mode to frame from original GIF
@@ -633,6 +639,7 @@ def test_write_animation_RGBA(self, tmp_path: Path) -> None:
633639

634640
def check(temp_file: Path) -> None:
635641
with Image.open(temp_file) as im:
642+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
636643
assert im.n_frames == 4
637644

638645
# Compare first frame to original
@@ -705,6 +712,7 @@ def test_timestamp_and_duration(self, tmp_path: Path) -> None:
705712
)
706713

707714
with Image.open(temp_file) as im:
715+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
708716
assert im.n_frames == 5
709717
assert im.is_animated
710718

@@ -734,6 +742,7 @@ def test_seeking(self, tmp_path: Path) -> None:
734742
)
735743

736744
with Image.open(temp_file) as im:
745+
assert isinstance(im, AvifImagePlugin.AvifImageFile)
737746
assert im.n_frames == 5
738747
assert im.is_animated
739748

Tests/test_file_fli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ def test_sanity() -> None:
4343
def test_prefix_chunk(monkeypatch: pytest.MonkeyPatch) -> None:
4444
monkeypatch.setattr(ImageFile, "LOAD_TRUNCATED_IMAGES", True)
4545
with Image.open(animated_test_file_with_prefix_chunk) as im:
46+
assert isinstance(im, FliImagePlugin.FliImageFile)
4647
assert im.mode == "P"
4748
assert im.size == (320, 200)
4849
assert im.format == "FLI"
4950
assert im.info["duration"] == 171
5051
assert im.is_animated
5152

5253
palette = im.getpalette()
54+
assert palette is not None
5355
assert palette[3:6] == [255, 255, 255]
5456
assert palette[381:384] == [204, 204, 12]
5557
assert palette[765:] == [252, 0, 0]

Tests/test_file_gif.py

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

282282
with Image.open(out) as reread:
283+
assert isinstance(reread, GifImagePlugin.GifImageFile)
283284
assert reread.n_frames == 5
284285

285286

@@ -1357,8 +1358,10 @@ def test_palette_save_all_P(tmp_path: Path) -> None:
13571358

13581359
with Image.open(out) as im:
13591360
# Assert that the frames are correct, and each frame has the same palette
1361+
assert isinstance(im, GifImagePlugin.GifImageFile)
13601362
assert_image_equal(im.convert("RGB"), frames[0].convert("RGB"))
13611363
assert im.palette is not None
1364+
assert im.global_palette is not None
13621365
assert im.palette.palette == im.global_palette.palette
13631366

13641367
im.seek(1)

Tests/test_file_jpeg.py

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

335335
# Reading
336336
with Image.open("Tests/images/exif_gps.jpg") as im:
337-
exif = im._getexif()
338-
assert exif[gps_index] == expected_exif_gps
337+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
338+
exif_data = im._getexif()
339+
assert exif_data is not None
340+
assert exif_data[gps_index] == expected_exif_gps
339341

340342
# Writing
341343
f = tmp_path / "temp.jpg"
@@ -344,8 +346,10 @@ def test_exif_gps(self, tmp_path: Path) -> None:
344346
hopper().save(f, exif=exif)
345347

346348
with Image.open(f) as reloaded:
347-
exif = reloaded._getexif()
348-
assert exif[gps_index] == expected_exif_gps
349+
assert isinstance(reloaded, JpegImagePlugin.JpegImageFile)
350+
exif_data = reloaded._getexif()
351+
assert exif_data is not None
352+
assert exif_data[gps_index] == expected_exif_gps
349353

350354
def test_empty_exif_gps(self) -> None:
351355
with Image.open("Tests/images/empty_gps_ifd.jpg") as im:
@@ -372,6 +376,7 @@ def test_exif_equality(self) -> None:
372376
exifs = []
373377
for i in range(2):
374378
with Image.open("Tests/images/exif-200dpcm.jpg") as im:
379+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
375380
exifs.append(im._getexif())
376381
assert exifs[0] == exifs[1]
377382

@@ -405,13 +410,17 @@ def test_exif_rollback(self) -> None:
405410
}
406411

407412
with Image.open("Tests/images/exif_gps.jpg") as im:
413+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
408414
exif = im._getexif()
415+
assert exif is not None
409416

410417
for tag, value in expected_exif.items():
411418
assert value == exif[tag]
412419

413420
def test_exif_gps_typeerror(self) -> None:
414421
with Image.open("Tests/images/exif_gps_typeerror.jpg") as im:
422+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
423+
415424
# Should not raise a TypeError
416425
im._getexif()
417426

@@ -491,7 +500,9 @@ def getsampling(
491500

492501
def test_exif(self) -> None:
493502
with Image.open("Tests/images/pil_sample_rgb.jpg") as im:
503+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
494504
info = im._getexif()
505+
assert info is not None
495506
assert info[305] == "Adobe Photoshop CS Macintosh"
496507

497508
def test_get_child_images(self) -> None:
@@ -676,11 +687,13 @@ def test_load_16bit_qtables(self) -> None:
676687

677688
def test_save_multiple_16bit_qtables(self) -> None:
678689
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
690+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
679691
im2 = self.roundtrip(im, qtables="keep")
680692
assert im.quantization == im2.quantization
681693

682694
def test_save_single_16bit_qtable(self) -> None:
683695
with Image.open("Tests/images/hopper_16bit_qtables.jpg") as im:
696+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
684697
im2 = self.roundtrip(im, qtables={0: im.quantization[0]})
685698
assert len(im2.quantization) == 1
686699
assert im2.quantization[0] == im.quantization[0]
@@ -889,7 +902,10 @@ def test_ifd_offset_exif(self) -> None:
889902
# in contrast to normal 8
890903
with Image.open("Tests/images/exif-ifd-offset.jpg") as im:
891904
# Act / Assert
892-
assert im._getexif()[306] == "2017:03:13 23:03:09"
905+
assert isinstance(im, JpegImagePlugin.JpegImageFile)
906+
exif = im._getexif()
907+
assert exif is not None
908+
assert exif[306] == "2017:03:13 23:03:09"
893909

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

Tests/test_file_mpo.py

Lines changed: 8 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,7 +157,9 @@ 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()
162+
assert mpinfo is not None
159163
assert mpinfo[45056] == b"0100"
160164
assert mpinfo[45057] == 2
161165

@@ -164,7 +168,9 @@ def test_mp_offset() -> None:
164168
# This image has been manually hexedited to have an IFD offset of 10
165169
# in APP2 data, in contrast to normal 8
166170
with Image.open("Tests/images/sugarshack_ifd_offset.mpo") as im:
171+
assert isinstance(im, MpoImagePlugin.MpoImageFile)
167172
mpinfo = im._getmp()
173+
assert mpinfo is not None
168174
assert mpinfo[45056] == b"0100"
169175
assert mpinfo[45057] == 2
170176

@@ -180,7 +186,9 @@ def test_mp_no_data() -> None:
180186
@pytest.mark.parametrize("test_file", test_files)
181187
def test_mp_attribute(test_file: str) -> None:
182188
with Image.open(test_file) as im:
189+
assert isinstance(im, MpoImagePlugin.MpoImageFile)
183190
mpinfo = im._getmp()
191+
assert mpinfo is not None
184192
for frame_number, mpentry in enumerate(mpinfo[0xB002]):
185193
mpattr = mpentry["Attribute"]
186194
if frame_number:

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)