From cafc329f4035af5a9ebebe184d07ef6ea23838dc Mon Sep 17 00:00:00 2001 From: jorenham Date: Tue, 27 Jan 2026 11:29:39 +0100 Subject: [PATCH 1/2] enable the mypy "truthy-bool" error code --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index cc616bc547c..c5211941710 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -218,7 +218,7 @@ python_version = "3.10" pretty = true disallow_any_generics = true disallow_untyped_defs = true -enable_error_code = "ignore-without-code" +enable_error_code = [ "ignore-without-code", "truthy-bool" ] extra_checks = true follow_imports = "silent" warn_redundant_casts = true From c18d0d012e705dab95c09a4d5bfa61a291c95832 Mon Sep 17 00:00:00 2001 From: jorenham Date: Tue, 27 Jan 2026 11:30:07 +0100 Subject: [PATCH 2/2] fix mypy "truthy-bool" errors --- Tests/test_image.py | 2 +- Tests/test_pyarrow.py | 2 -- Tests/test_qt_image_qapplication.py | 3 +-- checks/check_png_dos.py | 3 +-- src/PIL/Image.py | 15 +++++---------- src/PIL/ImageFont.py | 2 +- 6 files changed, 9 insertions(+), 18 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index c064939e81a..09ed9aeb547 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -570,7 +570,7 @@ def test_check_size(self) -> None: im = Image.new("L", (100, 0)) assert im.size == (100, 0) - assert Image.new("RGB", (1, 1)) + assert isinstance(Image.new("RGB", (1, 1)), Image.Image) # Should pass lists too i = Image.new("RGB", [1, 1]) assert isinstance(i.size, tuple) diff --git a/Tests/test_pyarrow.py b/Tests/test_pyarrow.py index a69504e78a0..08455a7f70c 100644 --- a/Tests/test_pyarrow.py +++ b/Tests/test_pyarrow.py @@ -112,8 +112,6 @@ def test_to_array(mode: str, dtype: pyarrow.DataType, mask: list[int] | None) -> reloaded = Image.fromarrow(arr, mode, img.size) - assert reloaded - assert_image_equal(img, reloaded) diff --git a/Tests/test_qt_image_qapplication.py b/Tests/test_qt_image_qapplication.py index b31e2a4ef25..ff5c877af23 100644 --- a/Tests/test_qt_image_qapplication.py +++ b/Tests/test_qt_image_qapplication.py @@ -56,9 +56,8 @@ def roundtrip(expected: Image.Image) -> None: def test_sanity(tmp_path: Path) -> None: # Segfault test app: QApplication | None = QApplication([]) - ex = Example() + ex = Example() # noqa: F841 assert app # Silence warning - assert ex # Silence warning for mode in ("1", "RGB", "RGBA", "L", "P"): # to QPixmap diff --git a/checks/check_png_dos.py b/checks/check_png_dos.py index 1bfb94ab7f7..15b2fe30821 100644 --- a/checks/check_png_dos.py +++ b/checks/check_png_dos.py @@ -28,8 +28,7 @@ def test_dos_text() -> None: try: im = Image.open(TEST_FILE) im.load() - except ValueError as msg: - assert msg, "Decompressed Data Too Large" + except ValueError: return assert isinstance(im, PngImagePlugin.PngImageFile) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index fdcc680b900..d5089ddc9f1 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -438,33 +438,28 @@ def preinit() -> None: return try: - from . import BmpImagePlugin + from . import BmpImagePlugin as BmpImagePlugin - assert BmpImagePlugin except ImportError: pass try: - from . import GifImagePlugin + from . import GifImagePlugin as GifImagePlugin - assert GifImagePlugin except ImportError: pass try: - from . import JpegImagePlugin + from . import JpegImagePlugin as JpegImagePlugin - assert JpegImagePlugin except ImportError: pass try: - from . import PpmImagePlugin + from . import PpmImagePlugin as PpmImagePlugin - assert PpmImagePlugin except ImportError: pass try: - from . import PngImagePlugin + from . import PngImagePlugin as PngImagePlugin - assert PngImagePlugin except ImportError: pass diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index d11f7bf01ad..b966a865e45 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -110,7 +110,7 @@ def _load_pilfont(self, filename: str) -> None: except Exception: pass else: - if image and image.mode in ("1", "L"): + if image.mode in ("1", "L"): break else: if image: