From 4024f0287d4e8a1467da8ce4ac86ea087e53af1c Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 1 Dec 2025 19:25:47 +1100 Subject: [PATCH] Assert image type --- Tests/test_file_iptc.py | 1 + Tests/test_file_libtiff.py | 1 + Tests/test_file_png.py | 2 ++ src/PIL/Image.py | 3 +++ 4 files changed, 7 insertions(+) diff --git a/Tests/test_file_iptc.py b/Tests/test_file_iptc.py index 0376b9997..9e2d8c06d 100644 --- a/Tests/test_file_iptc.py +++ b/Tests/test_file_iptc.py @@ -143,6 +143,7 @@ def test_getiptcinfo_tiff() -> None: # Test with LONG tag type with Image.open("Tests/images/hopper.Lab.tif") as im: + assert isinstance(im, TiffImagePlugin.TiffImageFile) im.tag_v2.tagtype[TiffImagePlugin.IPTC_NAA_CHUNK] = TiffTags.LONG iptc = IptcImagePlugin.getiptcinfo(im) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index cf7676ab1..77e0b4bc9 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -378,6 +378,7 @@ class TestFileLibTiff(LibTiffTestCase): im.save(out, tiffinfo=ifd) with Image.open(out) as reloaded: + assert isinstance(reloaded, TiffImagePlugin.TiffImageFile) assert reloaded.tag_v2[37000] == 100 def test_inknames_tag( diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 9875fe096..7f163a4d6 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -787,7 +787,9 @@ class TestFilePng: im.save(test_file, exif=im.getexif()) with Image.open(test_file) as reloaded: + assert isinstance(reloaded, PngImagePlugin.PngImageFile) exif = reloaded._getexif() + assert exif is not None assert exif[305] == "Adobe Photoshop CS Macintosh" def test_exif_argument(self, tmp_path: Path) -> None: diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 9d50812eb..b71395c62 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1519,6 +1519,9 @@ class Image: "".join(self.info["Raw profile type exif"].split("\n")[3:]) ) elif hasattr(self, "tag_v2"): + from . import TiffImagePlugin + + assert isinstance(self, TiffImagePlugin.TiffImageFile) self._exif.bigtiff = self.tag_v2._bigtiff self._exif.endian = self.tag_v2._endian self._exif.load_from_fp(self.fp, self.tag_v2._offset)