diff --git a/Tests/test_file_bufrstub.py b/Tests/test_file_bufrstub.py index 939e82e77..77ee5b0ea 100644 --- a/Tests/test_file_bufrstub.py +++ b/Tests/test_file_bufrstub.py @@ -64,6 +64,9 @@ def test_handler(tmp_path: Path) -> None: im.fp.close() return Image.new("RGB", (1, 1)) + def is_loaded(self) -> bool: + return self.loaded + def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None: self.saved = True @@ -71,10 +74,10 @@ def test_handler(tmp_path: Path) -> None: BufrStubImagePlugin.register_handler(handler) with Image.open(TEST_FILE) as im: assert handler.opened - assert not handler.loaded + assert not handler.is_loaded() im.load() - assert handler.loaded + assert handler.is_loaded() temp_file = str(tmp_path / "temp.bufr") im.save(temp_file) diff --git a/Tests/test_file_gribstub.py b/Tests/test_file_gribstub.py index 86a9064fc..aba473d24 100644 --- a/Tests/test_file_gribstub.py +++ b/Tests/test_file_gribstub.py @@ -64,6 +64,9 @@ def test_handler(tmp_path: Path) -> None: im.fp.close() return Image.new("RGB", (1, 1)) + def is_loaded(self) -> bool: + return self.loaded + def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None: self.saved = True @@ -71,10 +74,10 @@ def test_handler(tmp_path: Path) -> None: GribStubImagePlugin.register_handler(handler) with Image.open(TEST_FILE) as im: assert handler.opened - assert not handler.loaded + assert not handler.is_loaded() im.load() - assert handler.loaded + assert handler.is_loaded() temp_file = str(tmp_path / "temp.grib") im.save(temp_file) diff --git a/Tests/test_file_hdf5stub.py b/Tests/test_file_hdf5stub.py index ee1544c51..8275bd0d8 100644 --- a/Tests/test_file_hdf5stub.py +++ b/Tests/test_file_hdf5stub.py @@ -66,6 +66,9 @@ def test_handler(tmp_path: Path) -> None: im.fp.close() return Image.new("RGB", (1, 1)) + def is_loaded(self) -> bool: + return self.loaded + def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None: self.saved = True @@ -73,10 +76,10 @@ def test_handler(tmp_path: Path) -> None: Hdf5StubImagePlugin.register_handler(handler) with Image.open(TEST_FILE) as im: assert handler.opened - assert not handler.loaded + assert not handler.is_loaded() im.load() - assert handler.loaded + assert handler.is_loaded() temp_file = str(tmp_path / "temp.h5") im.save(temp_file) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 0f0976802..a8b50a6d4 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -775,7 +775,7 @@ class TestFilePng: mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO() - sys.stdout = mystdout + sys.stdout = mystdout # type: ignore[assignment] with Image.open(TEST_PNG_FILE) as im: im.save(sys.stdout, "PNG") diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 0a61830a4..d6451ec18 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -373,7 +373,7 @@ def test_save_stdout(buffer: bool) -> None: mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO() - sys.stdout = mystdout + sys.stdout = mystdout # type: ignore[assignment] with Image.open(TEST_FILE) as im: im.save(sys.stdout, "PPM") diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 484a1be8f..ddae8f235 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -162,7 +162,7 @@ def test_combined_larger_than_size() -> None: ("Tests/images/timeout-dedc7a4ebd856d79b4359bbcc79e8ef231ce38f6.psd", OSError), ], ) -def test_crashes(test_file: str, raises) -> None: +def test_crashes(test_file: str, raises: type[Exception]) -> None: with open(test_file, "rb") as f: with pytest.raises(raises): with Image.open(f): diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 8b816aa4f..1e0310001 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -11,7 +11,11 @@ from PIL.TiffImagePlugin import IFDRational from .helper import assert_deep_equal, hopper -TAG_IDS = {info.name: info.value for info in TiffTags.TAGS_V2.values()} +TAG_IDS: dict[str, int] = { + info.name: info.value + for info in TiffTags.TAGS_V2.values() + if info.value is not None +} def test_rt_metadata(tmp_path: Path) -> None: @@ -411,8 +415,8 @@ def test_empty_values() -> None: info = TiffImagePlugin.ImageFileDirectory_v2(head) info.load(data) # Should not raise ValueError. - info = dict(info) - assert 33432 in info + info_dict = dict(info) + assert 33432 in info_dict def test_photoshop_info(tmp_path: Path) -> None: diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 1caf032f6..cbc905de4 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -5,6 +5,7 @@ import re import sys import warnings from pathlib import Path +from typing import Any import pytest @@ -70,7 +71,9 @@ class TestFileWebp: # dwebp -ppm ../../Tests/images/hopper.webp -o hopper_webp_bits.ppm assert_image_similar_tofile(image, "Tests/images/hopper_webp_bits.ppm", 1.0) - def _roundtrip(self, tmp_path: Path, mode, epsilon, args={}) -> None: + def _roundtrip( + self, tmp_path: Path, mode: str, epsilon: float, args: dict[str, Any] = {} + ) -> None: temp_file = str(tmp_path / "temp.webp") hopper(mode).save(temp_file, **args) diff --git a/Tests/test_image.py b/Tests/test_image.py index a8f61d677..5b1ba6289 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -8,7 +8,7 @@ import sys import tempfile import warnings from pathlib import Path -from typing import IO +from typing import IO, Any import pytest @@ -175,11 +175,19 @@ class TestImage: def test_fp_name(self, tmp_path: Path) -> None: temp_file = str(tmp_path / "temp.jpg") - class FP: + class FP(io.BytesIO): name: str - def write(self, b: bytes) -> None: - pass + if sys.version_info >= (3, 12): + from collections.abc import Buffer + + def write(self, data: Buffer) -> int: + return len(data) + + else: + + def write(self, data: Any) -> int: + return len(data) fp = FP() fp.name = temp_file diff --git a/Tests/test_image_array.py b/Tests/test_image_array.py index d7e6c562c..bb6064882 100644 --- a/Tests/test_image_array.py +++ b/Tests/test_image_array.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING, Any import pytest from packaging.version import parse as parse_version @@ -13,13 +13,16 @@ numpy = pytest.importorskip("numpy", reason="NumPy not installed") im = hopper().resize((128, 100)) +if TYPE_CHECKING: + import numpy.typing as npt + def test_toarray() -> None: def test(mode: str) -> tuple[tuple[int, ...], str, int]: ai = numpy.array(im.convert(mode)) return ai.shape, ai.dtype.str, ai.nbytes - def test_with_dtype(dtype) -> None: + def test_with_dtype(dtype: npt.DTypeLike) -> None: ai = numpy.array(im, dtype=dtype) assert ai.dtype == dtype diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 00cdec952..a269b846a 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -351,7 +351,9 @@ class TestPyEncoder(CodecsTest): ImageFile._save( im, fp, [("MOCK", (xoff, yoff, -10, yoff + ysize), 0, "RGB")] ) - assert MockPyEncoder.last.cleanup_called + last: MockPyEncoder | None = MockPyEncoder.last + assert last + assert last.cleanup_called with pytest.raises(ValueError): ImageFile._save( diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index c3906de24..e50198087 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -548,7 +548,7 @@ def test_find_font( def loadable_font( filepath: str, size: int, index: int, encoding: str, *args: Any - ): + ) -> ImageFont.FreeTypeFont: _freeTypeFont = getattr(ImageFont, "_FreeTypeFont") if filepath == path_to_fake: return _freeTypeFont(FONT_PATH, size, index, encoding, *args) diff --git a/Tests/test_imagewin_pointers.py b/Tests/test_imagewin_pointers.py index e6c312a0c..c23a5c690 100644 --- a/Tests/test_imagewin_pointers.py +++ b/Tests/test_imagewin_pointers.py @@ -45,21 +45,22 @@ if is_win32(): memcpy = ctypes.cdll.msvcrt.memcpy memcpy.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t] - CreateCompatibleDC = ctypes.windll.gdi32.CreateCompatibleDC + windll = getattr(ctypes, "windll") + CreateCompatibleDC = windll.gdi32.CreateCompatibleDC CreateCompatibleDC.argtypes = [ctypes.wintypes.HDC] CreateCompatibleDC.restype = ctypes.wintypes.HDC - DeleteDC = ctypes.windll.gdi32.DeleteDC + DeleteDC = windll.gdi32.DeleteDC DeleteDC.argtypes = [ctypes.wintypes.HDC] - SelectObject = ctypes.windll.gdi32.SelectObject + SelectObject = windll.gdi32.SelectObject SelectObject.argtypes = [ctypes.wintypes.HDC, ctypes.wintypes.HGDIOBJ] SelectObject.restype = ctypes.wintypes.HGDIOBJ - DeleteObject = ctypes.windll.gdi32.DeleteObject + DeleteObject = windll.gdi32.DeleteObject DeleteObject.argtypes = [ctypes.wintypes.HGDIOBJ] - CreateDIBSection = ctypes.windll.gdi32.CreateDIBSection + CreateDIBSection = windll.gdi32.CreateDIBSection CreateDIBSection.argtypes = [ ctypes.wintypes.HDC, ctypes.c_void_p, diff --git a/Tests/test_psdraw.py b/Tests/test_psdraw.py index c3afa9089..130ffa863 100644 --- a/Tests/test_psdraw.py +++ b/Tests/test_psdraw.py @@ -59,7 +59,7 @@ def test_stdout(buffer: bool) -> None: mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO() - sys.stdout = mystdout + sys.stdout = mystdout # type: ignore[assignment] ps = PSDraw.PSDraw() _create_document(ps)