Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type hints to additional tests #7807

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io
import re
from typing import Callable

import pytest

Expand Down Expand Up @@ -29,7 +30,7 @@ def test_version() -> None:
# Check the correctness of the convenience function
# and the format of version numbers

def test(name, function) -> None:
def test(name: str, function: Callable[[str], bool]) -> None:
version = features.version(name)
if not features.check(name):
assert version is None
Expand Down Expand Up @@ -73,12 +74,12 @@ def test_libimagequant_version() -> None:


@pytest.mark.parametrize("feature", features.modules)
def test_check_modules(feature) -> None:
def test_check_modules(feature: str) -> None:
assert features.check_module(feature) in [True, False]


@pytest.mark.parametrize("feature", features.codecs)
def test_check_codecs(feature) -> None:
def test_check_codecs(feature: str) -> None:
assert features.check_codec(feature) in [True, False]


Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_blp.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_save(tmp_path: Path) -> None:
"Tests/images/timeout-ef9112a065e7183fa7faa2e18929b03e44ee16bf.blp",
],
)
def test_crashes(test_file) -> None:
def test_crashes(test_file: str) -> None:
with open(test_file, "rb") as f:
with Image.open(f) as im:
with pytest.raises(OSError):
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_bmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def test_sanity(tmp_path: Path) -> None:
def roundtrip(im) -> None:
def roundtrip(im: Image.Image) -> None:
outfile = str(tmp_path / "temp.bmp")

im.save(outfile, "BMP")
Expand Down Expand Up @@ -194,7 +194,7 @@ def test_rle4() -> None:
("Tests/images/bmp/g/pal8rle.bmp", 1064),
),
)
def test_rle8_eof(file_name, length) -> None:
def test_rle8_eof(file_name: str, length: int) -> None:
with open(file_name, "rb") as fp:
data = fp.read(length)
with Image.open(io.BytesIO(data)) as im:
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_im.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_eoferror() -> None:


@pytest.mark.parametrize("mode", ("RGB", "P", "PA"))
def test_roundtrip(mode, tmp_path: Path) -> None:
def test_roundtrip(mode: str, tmp_path: Path) -> None:
out = str(tmp_path / "temp.im")
im = hopper(mode)
im.save(out)
Expand Down
6 changes: 3 additions & 3 deletions Tests/test_file_pcx.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .helper import assert_image_equal, hopper


def _roundtrip(tmp_path: Path, im) -> None:
def _roundtrip(tmp_path: Path, im: Image.Image) -> None:
f = str(tmp_path / "temp.pcx")
im.save(f)
with Image.open(f) as im2:
Expand Down Expand Up @@ -44,7 +44,7 @@ def test_invalid_file() -> None:


@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB"))
def test_odd(tmp_path: Path, mode) -> None:
def test_odd(tmp_path: Path, mode: str) -> None:
# See issue #523, odd sized images should have a stride that's even.
# Not that ImageMagick or GIMP write PCX that way.
# We were not handling properly.
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_large_count(tmp_path: Path) -> None:
_roundtrip(tmp_path, im)


def _test_buffer_overflow(tmp_path: Path, im, size: int = 1024) -> None:
def _test_buffer_overflow(tmp_path: Path, im: Image.Image, size: int = 1024) -> None:
_last = ImageFile.MAXBLOCK
ImageFile.MAXBLOCK = size
try:
Expand Down
15 changes: 8 additions & 7 deletions Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tempfile
import time
from pathlib import Path
from typing import Any, Generator

import pytest

Expand All @@ -14,7 +15,7 @@
from .helper import hopper, mark_if_feature_version, skip_unless_feature


def helper_save_as_pdf(tmp_path: Path, mode, **kwargs):
def helper_save_as_pdf(tmp_path: Path, mode: str, **kwargs: Any) -> str:
# Arrange
im = hopper(mode)
outfile = str(tmp_path / ("temp_" + mode + ".pdf"))
Expand All @@ -41,13 +42,13 @@ def helper_save_as_pdf(tmp_path: Path, mode, **kwargs):


@pytest.mark.parametrize("mode", ("L", "P", "RGB", "CMYK"))
def test_save(tmp_path: Path, mode) -> None:
def test_save(tmp_path: Path, mode: str) -> None:
helper_save_as_pdf(tmp_path, mode)


@skip_unless_feature("jpg_2000")
@pytest.mark.parametrize("mode", ("LA", "RGBA"))
def test_save_alpha(tmp_path: Path, mode) -> None:
def test_save_alpha(tmp_path: Path, mode: str) -> None:
helper_save_as_pdf(tmp_path, mode)


Expand Down Expand Up @@ -112,7 +113,7 @@ def test_resolution(tmp_path: Path) -> None:
{"dpi": (75, 150), "resolution": 200},
),
)
def test_dpi(params, tmp_path: Path) -> None:
def test_dpi(params: dict[str, int | tuple[int, int]], tmp_path: Path) -> None:
im = hopper()

outfile = str(tmp_path / "temp.pdf")
Expand Down Expand Up @@ -156,7 +157,7 @@ def test_save_all(tmp_path: Path) -> None:
assert os.path.getsize(outfile) > 0

# Test appending using a generator
def im_generator(ims):
def im_generator(ims: list[Image.Image]) -> Generator[Image.Image, None, None]:
yield from ims

im.save(outfile, save_all=True, append_images=im_generator(ims))
Expand Down Expand Up @@ -226,7 +227,7 @@ def test_pdf_append_fails_on_nonexistent_file() -> None:
im.save(os.path.join(temp_dir, "nonexistent.pdf"), append=True)


def check_pdf_pages_consistency(pdf) -> None:
def check_pdf_pages_consistency(pdf: PdfParser.PdfParser) -> None:
pages_info = pdf.read_indirect(pdf.pages_ref)
assert b"Parent" not in pages_info
assert b"Kids" in pages_info
Expand Down Expand Up @@ -339,7 +340,7 @@ def test_pdf_append_to_bytesio() -> None:
@pytest.mark.timeout(1)
@pytest.mark.skipif("PILLOW_VALGRIND_TEST" in os.environ, reason="Valgrind is slower")
@pytest.mark.parametrize("newline", (b"\r", b"\n"))
def test_redos(newline) -> None:
def test_redos(newline: bytes) -> None:
malicious = b" trailer<<>>" + newline * 3456

# This particular exception isn't relevant here.
Expand Down
19 changes: 11 additions & 8 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import warnings
from io import BytesIO
from pathlib import Path
from types import ModuleType
from typing import Generator

import pytest

Expand All @@ -20,6 +22,7 @@
is_win32,
)

ElementTree: ModuleType | None
try:
from defusedxml import ElementTree
except ImportError:
Expand Down Expand Up @@ -156,7 +159,7 @@ def test_int_resolution(self) -> None:
"resolution_unit, dpi",
[(None, 72.8), (2, 72.8), (3, 184.912)],
)
def test_load_float_dpi(self, resolution_unit, dpi) -> None:
def test_load_float_dpi(self, resolution_unit: int | None, dpi: float) -> None:
with Image.open(
"Tests/images/hopper_float_dpi_" + str(resolution_unit) + ".tif"
) as im:
Expand Down Expand Up @@ -284,7 +287,7 @@ def test_unknown_pixel_mode(self) -> None:
("Tests/images/multipage.tiff", 3),
),
)
def test_n_frames(self, path, n_frames) -> None:
def test_n_frames(self, path: str, n_frames: int) -> None:
with Image.open(path) as im:
assert im.n_frames == n_frames
assert im.is_animated == (n_frames != 1)
Expand Down Expand Up @@ -402,7 +405,7 @@ def test__delitem__(self) -> None:
assert len_before == len_after + 1

@pytest.mark.parametrize("legacy_api", (False, True))
def test_load_byte(self, legacy_api) -> None:
def test_load_byte(self, legacy_api: bool) -> None:
ifd = TiffImagePlugin.ImageFileDirectory_v2()
data = b"abc"
ret = ifd.load_byte(data, legacy_api)
Expand Down Expand Up @@ -431,7 +434,7 @@ def test_ifd_tag_type(self) -> None:
assert 0x8825 in im.tag_v2

def test_exif(self, tmp_path: Path) -> None:
def check_exif(exif) -> None:
def check_exif(exif: Image.Exif) -> None:
assert sorted(exif.keys()) == [
256,
257,
Expand Down Expand Up @@ -511,7 +514,7 @@ def test_exif_frames(self) -> None:
assert im.getexif()[273] == (1408, 1907)

@pytest.mark.parametrize("mode", ("1", "L"))
def test_photometric(self, mode, tmp_path: Path) -> None:
def test_photometric(self, mode: str, tmp_path: Path) -> None:
filename = str(tmp_path / "temp.tif")
im = hopper(mode)
im.save(filename, tiffinfo={262: 0})
Expand Down Expand Up @@ -660,7 +663,7 @@ def test_planar_configuration_save(self, tmp_path: Path) -> None:
assert_image_equal_tofile(reloaded, infile)

@pytest.mark.parametrize("mode", ("P", "PA"))
def test_palette(self, mode, tmp_path: Path) -> None:
def test_palette(self, mode: str, tmp_path: Path) -> None:
outfile = str(tmp_path / "temp.tif")

im = hopper(mode)
Expand Down Expand Up @@ -689,7 +692,7 @@ def test_tiff_save_all(self) -> None:
assert reread.n_frames == 3

# Test appending using a generator
def im_generator(ims):
def im_generator(ims: list[Image.Image]) -> Generator[Image.Image, None, None]:
yield from ims

mp = BytesIO()
Expand Down Expand Up @@ -860,7 +863,7 @@ def test_timeout(self) -> None:
],
)
@pytest.mark.timeout(2)
def test_oom(self, test_file) -> None:
def test_oom(self, test_file: str) -> None:
with pytest.raises(UnidentifiedImageError):
with pytest.warns(UserWarning):
with Image.open(test_file):
Expand Down
19 changes: 12 additions & 7 deletions Tests/test_format_hsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

import colorsys
import itertools
from typing import Callable

from PIL import Image

from .helper import assert_image_similar, hopper


def int_to_float(i):
def int_to_float(i: int) -> float:
return i / 255


def str_to_float(i):
def str_to_float(i: str) -> float:
return ord(i) / 255


def tuple_to_ints(tp):
def tuple_to_ints(tp: tuple[float, float, float]) -> tuple[int, int, int]:
x, y, z = tp
return int(x * 255.0), int(y * 255.0), int(z * 255.0)

Expand All @@ -25,7 +26,7 @@ def test_sanity() -> None:
Image.new("HSV", (100, 100))


def wedge():
def wedge() -> Image.Image:
w = Image._wedge()
w90 = w.rotate(90)

Expand All @@ -49,7 +50,11 @@ def wedge():
return img


def to_xxx_colorsys(im, func, mode):
def to_xxx_colorsys(
im: Image.Image,
func: Callable[[float, float, float], tuple[float, float, float]],
mode: str,
) -> Image.Image:
# convert the hard way using the library colorsys routines.

(r, g, b) = im.split()
Expand All @@ -70,11 +75,11 @@ def to_xxx_colorsys(im, func, mode):
return hsv


def to_hsv_colorsys(im):
def to_hsv_colorsys(im: Image.Image) -> Image.Image:
return to_xxx_colorsys(im, colorsys.rgb_to_hsv, "HSV")


def to_rgb_colorsys(im):
def to_rgb_colorsys(im: Image.Image) -> Image.Image:
return to_xxx_colorsys(im, colorsys.hsv_to_rgb, "RGB")


Expand Down
6 changes: 5 additions & 1 deletion Tests/test_imagechops.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import Callable

from PIL import Image, ImageChops

from .helper import assert_image_equal, hopper
Expand Down Expand Up @@ -387,7 +389,9 @@ def test_overlay() -> None:


def test_logical() -> None:
def table(op, a, b):
def table(
op: Callable[[Image.Image, Image.Image], Image.Image], a: int, b: int
) -> tuple[int, int, int, int]:
out = []
for x in (a, b):
imx = Image.new("1", (1, 1), x)
Expand Down
11 changes: 6 additions & 5 deletions Tests/test_imagedraw2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from PIL import Image, ImageDraw, ImageDraw2, features
from PIL._typing import Coords

from .helper import (
assert_image_equal,
Expand Down Expand Up @@ -56,7 +57,7 @@ def test_sanity() -> None:


@pytest.mark.parametrize("bbox", BBOX)
def test_ellipse(bbox) -> None:
def test_ellipse(bbox: Coords) -> None:
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw2.Draw(im)
Expand Down Expand Up @@ -84,7 +85,7 @@ def test_ellipse_edge() -> None:


@pytest.mark.parametrize("points", POINTS)
def test_line(points) -> None:
def test_line(points: Coords) -> None:
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw2.Draw(im)
Expand All @@ -98,7 +99,7 @@ def test_line(points) -> None:


@pytest.mark.parametrize("points", POINTS)
def test_line_pen_as_brush(points) -> None:
def test_line_pen_as_brush(points: Coords) -> None:
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw2.Draw(im)
Expand All @@ -114,7 +115,7 @@ def test_line_pen_as_brush(points) -> None:


@pytest.mark.parametrize("points", POINTS)
def test_polygon(points) -> None:
def test_polygon(points: Coords) -> None:
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw2.Draw(im)
Expand All @@ -129,7 +130,7 @@ def test_polygon(points) -> None:


@pytest.mark.parametrize("bbox", BBOX)
def test_rectangle(bbox) -> None:
def test_rectangle(bbox: Coords) -> None:
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw2.Draw(im)
Expand Down
Loading
Loading