Skip to content

Commit

Permalink
Added type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 23, 2024
1 parent 920698e commit 42381aa
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 27 deletions.
7 changes: 5 additions & 2 deletions Tests/test_file_bufrstub.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ def load(self, im: ImageFile.StubImageFile) -> Image.Image:
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

handler = TestHandler()
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)
Expand Down
7 changes: 5 additions & 2 deletions Tests/test_file_gribstub.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ def load(self, im: Image.Image) -> Image.Image:
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

handler = TestHandler()
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)
Expand Down
7 changes: 5 additions & 2 deletions Tests/test_file_hdf5stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ def load(self, im: Image.Image) -> Image.Image:
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

handler = TestHandler()
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)
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ class MyStdOut:

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")
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_ppm.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class MyStdOut:

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")
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_psd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 7 additions & 3 deletions Tests/test_file_tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

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:
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_file_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import warnings
from pathlib import Path
from typing import Any

import pytest

Expand Down Expand Up @@ -70,7 +71,9 @@ def test_read_rgb(self) -> None:
# 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)
Expand Down
16 changes: 12 additions & 4 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import tempfile
import warnings
from pathlib import Path
from typing import IO
from typing import IO, Any

import pytest

Expand Down Expand Up @@ -175,11 +175,19 @@ def test_pathlib(self, tmp_path: Path) -> None:
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
Expand Down
7 changes: 5 additions & 2 deletions Tests/test_image_array.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,13 +13,16 @@

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

Expand Down
4 changes: 3 additions & 1 deletion Tests/test_imagefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ def test_negsize(self) -> None:
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(
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def _test_fake_loading_font(path_to_fake: str, fontname: str) -> None:

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)
Expand Down
11 changes: 6 additions & 5 deletions Tests/test_imagewin_pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,22 @@ class BITMAPINFOHEADER(ctypes.Structure):
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,
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_psdraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MyStdOut:

mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()

sys.stdout = mystdout
sys.stdout = mystdout # type: ignore[assignment]

ps = PSDraw.PSDraw()
_create_document(ps)
Expand Down

0 comments on commit 42381aa

Please sign in to comment.