From 867c4772c22455207d6f1982bfbe130b423b53a5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 1 Apr 2025 20:19:40 +1100 Subject: [PATCH 1/8] Do not import type checking --- Tests/test_image_array.py | 3 ++- Tests/test_numpy.py | 2 +- Tests/test_qt_image_qapplication.py | 3 ++- docs/dater.py | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Tests/test_image_array.py b/Tests/test_image_array.py index eb2309e0f..25cb99c43 100644 --- a/Tests/test_image_array.py +++ b/Tests/test_image_array.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any +from typing import Any import pytest from packaging.version import parse as parse_version @@ -13,6 +13,7 @@ numpy = pytest.importorskip("numpy", reason="NumPy not installed") im = hopper().resize((128, 100)) +TYPE_CHECKING = False if TYPE_CHECKING: import numpy.typing as npt diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index c4ad19d23..ef54deeeb 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -1,7 +1,6 @@ from __future__ import annotations import warnings -from typing import TYPE_CHECKING import pytest @@ -9,6 +8,7 @@ from PIL import Image, _typing from .helper import assert_deep_equal, assert_image, hopper, skip_unless_feature +TYPE_CHECKING = False if TYPE_CHECKING: import numpy import numpy.typing as npt diff --git a/Tests/test_qt_image_qapplication.py b/Tests/test_qt_image_qapplication.py index 0ed9fbfa5..82a3e0741 100644 --- a/Tests/test_qt_image_qapplication.py +++ b/Tests/test_qt_image_qapplication.py @@ -1,7 +1,7 @@ from __future__ import annotations from pathlib import Path -from typing import TYPE_CHECKING, Union +from typing import Union import pytest @@ -9,6 +9,7 @@ from PIL import Image, ImageQt from .helper import assert_image_equal_tofile, assert_image_similar, hopper +TYPE_CHECKING = False if TYPE_CHECKING: import PyQt6 import PySide6 diff --git a/docs/dater.py b/docs/dater.py index f9fb0c1da..c0302b55c 100644 --- a/docs/dater.py +++ b/docs/dater.py @@ -8,8 +8,8 @@ from __future__ import annotations import re import subprocess -from typing import TYPE_CHECKING +TYPE_CHECKING = False if TYPE_CHECKING: from sphinx.application import Sphinx From fa7413904b4eee630401c31994eeb5bcda2441a5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 3 Jun 2025 14:13:22 +1000 Subject: [PATCH 2/8] Updated ruff ID --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a1a054e00..1b8fa7199 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.11.12 hooks: - - id: ruff + - id: ruff-check args: [--exit-non-zero-on-fix] - repo: https://github.com/psf/black-pre-commit-mirror From 0d1edba311ffdc9683c6541a5133c60d425debe8 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 12 Apr 2025 14:16:49 +1000 Subject: [PATCH 3/8] Assert tile args is tuple --- Tests/test_file_gif.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 20d58a9dd..2712e683c 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1422,7 +1422,9 @@ def test_getdata(monkeypatch: pytest.MonkeyPatch) -> None: def test_lzw_bits() -> None: # see https://github.com/python-pillow/Pillow/issues/2811 with Image.open("Tests/images/issue_2811.gif") as im: - assert im.tile[0][3][0] == 11 # LZW bits + args = im.tile[0][3] + assert isinstance(args, tuple) + assert args[0] == 11 # LZW bits # codec error prepatch im.load() From 33460d2f82ee148eff2451cfe7e8bc4b6f33b66a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 12 Apr 2025 14:22:02 +1000 Subject: [PATCH 4/8] Assert _getmp() does not return None --- Tests/test_file_mpo.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index 73838ef44..462c95535 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -156,6 +156,7 @@ def test_reload_exif_after_seek() -> None: def test_mp(test_file: str) -> None: with Image.open(test_file) as im: mpinfo = im._getmp() + assert mpinfo is not None assert mpinfo[45056] == b"0100" assert mpinfo[45057] == 2 @@ -165,6 +166,7 @@ def test_mp_offset() -> None: # in APP2 data, in contrast to normal 8 with Image.open("Tests/images/sugarshack_ifd_offset.mpo") as im: mpinfo = im._getmp() + assert mpinfo is not None assert mpinfo[45056] == b"0100" assert mpinfo[45057] == 2 @@ -181,6 +183,7 @@ def test_mp_no_data() -> None: def test_mp_attribute(test_file: str) -> None: with Image.open(test_file) as im: mpinfo = im._getmp() + assert mpinfo is not None for frame_number, mpentry in enumerate(mpinfo[0xB002]): mpattr = mpentry["Attribute"] if frame_number: From cba096b4a99f92eb865c7fb127b9783dbd38643e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Jun 2025 11:13:12 +1000 Subject: [PATCH 5/8] Assert pixel data is tuple --- Tests/test_file_gif.py | 8 ++++++-- Tests/test_file_jpeg.py | 10 ++++++---- Tests/test_file_tga.py | 8 ++++++-- Tests/test_file_webp.py | 2 ++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 2712e683c..f46a28971 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -540,7 +540,9 @@ def test_dispose_background_transparency() -> None: img.seek(2) px = img.load() assert px is not None - assert px[35, 30][3] == 0 + value = px[35, 30] + assert isinstance(value, tuple) + assert value[3] == 0 @pytest.mark.parametrize( @@ -1479,7 +1481,9 @@ def test_saving_rgba(tmp_path: Path) -> None: with Image.open(out) as reloaded: reloaded_rgba = reloaded.convert("RGBA") - assert reloaded_rgba.load()[0, 0][3] == 0 + value = reloaded_rgba.load()[0, 0] + assert isinstance(value, tuple) + assert value[3] == 0 @pytest.mark.parametrize("params", ({}, {"disposal": 2, "optimize": False})) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 2827937cf..50ee04611 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -133,15 +133,17 @@ class TestFileJpeg: f = "Tests/images/pil_sample_cmyk.jpg" with Image.open(f) as im: # the source image has red pixels in the upper left corner. - c, m, y, k = (x / 255.0 for x in im.getpixel((0, 0))) + cmyk = im.getpixel((0, 0)) + assert isinstance(cmyk, tuple) + c, m, y, k = (x / 255.0 for x in cmyk) assert c == 0.0 assert m > 0.8 assert y > 0.8 assert k == 0.0 # the opposite corner is black - c, m, y, k = ( - x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1)) - ) + cmyk = im.getpixel((im.size[0] - 1, im.size[1] - 1)) + assert isinstance(cmyk, tuple) + k = cmyk[3] / 255.0 assert k > 0.9 # roundtrip, and check again im = self.roundtrip(im) diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py index 8b6ed3ed2..d3cceb37f 100644 --- a/Tests/test_file_tga.py +++ b/Tests/test_file_tga.py @@ -220,12 +220,16 @@ def test_horizontal_orientations() -> None: with Image.open("Tests/images/rgb32rle_top_right.tga") as im: px = im.load() assert px is not None - assert px[90, 90][:3] == (0, 0, 0) + value = px[90, 90] + assert isinstance(value, tuple) + assert value[:3] == (0, 0, 0) with Image.open("Tests/images/rgb32rle_bottom_right.tga") as im: px = im.load() assert px is not None - assert px[90, 90][:3] == (0, 255, 0) + value = px[90, 90] + assert isinstance(value, tuple) + assert value[:3] == (0, 255, 0) def test_save_rle(tmp_path: Path) -> None: diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index f61e2c82e..4ea7629d1 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -219,6 +219,7 @@ class TestFileWebp: # Save P mode GIF with background with Image.open("Tests/images/chi.gif") as im: original_value = im.convert("RGB").getpixel((1, 1)) + assert isinstance(original_value, tuple) # Save as WEBP im.save(out_webp, save_all=True) @@ -230,6 +231,7 @@ class TestFileWebp: with Image.open(out_gif) as reread: reread_value = reread.convert("RGB").getpixel((1, 1)) + assert isinstance(reread_value, tuple) difference = sum(abs(original_value[i] - reread_value[i]) for i in range(3)) assert difference < 5 From a3da70e76e14da1bc0e5b1c2331d746e4a095a6b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 12 Apr 2025 14:23:58 +1000 Subject: [PATCH 6/8] Assert load() does not return None --- Tests/test_file_gif.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index f46a28971..e418af45c 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -1481,7 +1481,9 @@ def test_saving_rgba(tmp_path: Path) -> None: with Image.open(out) as reloaded: reloaded_rgba = reloaded.convert("RGBA") - value = reloaded_rgba.load()[0, 0] + px = reloaded_rgba.load() + assert px is not None + value = px[0, 0] assert isinstance(value, tuple) assert value[3] == 0 From 89c38258dc33fd410858c6b760d533789578e15e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 12 Apr 2025 14:24:20 +1000 Subject: [PATCH 7/8] Assert getcolors() does not return None --- Tests/test_file_avif.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_avif.py b/Tests/test_file_avif.py index b2e586637..6d0cc74f9 100644 --- a/Tests/test_file_avif.py +++ b/Tests/test_file_avif.py @@ -254,7 +254,9 @@ class TestFileAvif: assert_image(im, "RGBA", (64, 64)) # image has 876 transparent pixels - assert im.getchannel("A").getcolors()[0] == (876, 0) + colors = im.getchannel("A").getcolors() + assert colors is not None + assert colors[0] == (876, 0) def test_save_transparent(self, tmp_path: Path) -> None: im = Image.new("RGBA", (10, 10), (0, 0, 0, 0)) From 04c984f2f202639479a161dc44ba378b9ebee931 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Jun 2025 11:29:11 +1000 Subject: [PATCH 8/8] Removed duplicate code --- Tests/test_file_jpeg.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 50ee04611..00f2c004d 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -130,9 +130,7 @@ class TestFileJpeg: def test_cmyk(self) -> None: # Test CMYK handling. Thanks to Tim and Charlie for test data, # Michael for getting me to look one more time. - f = "Tests/images/pil_sample_cmyk.jpg" - with Image.open(f) as im: - # the source image has red pixels in the upper left corner. + def check(im: ImageFile.ImageFile) -> None: cmyk = im.getpixel((0, 0)) assert isinstance(cmyk, tuple) c, m, y, k = (x / 255.0 for x in cmyk) @@ -145,19 +143,13 @@ class TestFileJpeg: assert isinstance(cmyk, tuple) k = cmyk[3] / 255.0 assert k > 0.9 + + with Image.open("Tests/images/pil_sample_cmyk.jpg") as im: + # the source image has red pixels in the upper left corner. + check(im) + # roundtrip, and check again - im = self.roundtrip(im) - cmyk = im.getpixel((0, 0)) - assert isinstance(cmyk, tuple) - c, m, y, k = (x / 255.0 for x in cmyk) - assert c == 0.0 - assert m > 0.8 - assert y > 0.8 - assert k == 0.0 - cmyk = im.getpixel((im.size[0] - 1, im.size[1] - 1)) - assert isinstance(cmyk, tuple) - k = cmyk[3] / 255.0 - assert k > 0.9 + check(self.roundtrip(im)) def test_rgb(self) -> None: def getchannels(im: JpegImagePlugin.JpegImageFile) -> tuple[int, ...]: