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 #7794

Merged
merged 1 commit into from
Feb 13, 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
4 changes: 4 additions & 0 deletions Tests/test_color_lut.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import annotations

from array import array
from types import ModuleType

import pytest

from PIL import Image, ImageFilter

from .helper import assert_image_equal

numpy: ModuleType | None
try:
import numpy
except ImportError:
Expand Down Expand Up @@ -397,6 +399,7 @@ def test_convert_table(self) -> None:

@pytest.mark.skipif(numpy is None, reason="NumPy not installed")
def test_numpy_sources(self) -> None:
assert numpy is not None
table = numpy.ones((5, 6, 7, 3), dtype=numpy.float16)
with pytest.raises(ValueError, match="should have either channels"):
lut = ImageFilter.Color3DLUT((5, 6, 7), table)
Expand Down Expand Up @@ -430,6 +433,7 @@ def test_numpy_sources(self) -> None:

@pytest.mark.skipif(numpy is None, reason="NumPy not installed")
def test_numpy_formats(self) -> None:
assert numpy is not None
g = Image.linear_gradient("L")
im = Image.merge(
"RGB",
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_core_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,6 @@ def test_units(self) -> None:
{"PILLOW_BLOCKS_MAX": "wat"},
),
)
def test_warnings(self, var) -> None:
def test_warnings(self, var: dict[str, str]) -> None:
with pytest.warns(UserWarning):
Image._apply_env_variables(var)
18 changes: 9 additions & 9 deletions Tests/test_file_dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
TEST_FILE_DX10_BC1_TYPELESS,
),
)
def test_sanity_dxt1_bc1(image_path) -> None:
def test_sanity_dxt1_bc1(image_path: str) -> None:
"""Check DXT1 and BC1 images can be opened"""
with Image.open(TEST_FILE_DXT1.replace(".dds", ".png")) as target:
target = target.convert("RGBA")
Expand Down Expand Up @@ -96,7 +96,7 @@ def test_sanity_dxt5() -> None:
TEST_FILE_BC4U,
),
)
def test_sanity_ati1_bc4u(image_path) -> None:
def test_sanity_ati1_bc4u(image_path: str) -> None:
"""Check ATI1 and BC4U images can be opened"""

with Image.open(image_path) as im:
Expand All @@ -117,7 +117,7 @@ def test_sanity_ati1_bc4u(image_path) -> None:
TEST_FILE_DX10_BC4_TYPELESS,
),
)
def test_dx10_bc4(image_path) -> None:
def test_dx10_bc4(image_path: str) -> None:
"""Check DX10 BC4 images can be opened"""

with Image.open(image_path) as im:
Expand All @@ -138,7 +138,7 @@ def test_dx10_bc4(image_path) -> None:
TEST_FILE_BC5U,
),
)
def test_sanity_ati2_bc5u(image_path) -> None:
def test_sanity_ati2_bc5u(image_path: str) -> None:
"""Check ATI2 and BC5U images can be opened"""

with Image.open(image_path) as im:
Expand All @@ -162,7 +162,7 @@ def test_sanity_ati2_bc5u(image_path) -> None:
(TEST_FILE_BC5S, TEST_FILE_BC5S),
),
)
def test_dx10_bc5(image_path, expected_path) -> None:
def test_dx10_bc5(image_path: str, expected_path: str) -> None:
"""Check DX10 BC5 images can be opened"""

with Image.open(image_path) as im:
Expand All @@ -176,7 +176,7 @@ def test_dx10_bc5(image_path, expected_path) -> None:


@pytest.mark.parametrize("image_path", (TEST_FILE_BC6H, TEST_FILE_BC6HS))
def test_dx10_bc6h(image_path) -> None:
def test_dx10_bc6h(image_path: str) -> None:
"""Check DX10 BC6H/BC6HS images can be opened"""

with Image.open(image_path) as im:
Expand Down Expand Up @@ -257,7 +257,7 @@ def test_dx10_r8g8b8a8_unorm_srgb() -> None:
("RGBA", (800, 600), TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA),
],
)
def test_uncompressed(mode, size, test_file) -> None:
def test_uncompressed(mode: str, size: tuple[int, int], test_file: str) -> None:
"""Check uncompressed images can be opened"""

with Image.open(test_file) as im:
Expand Down Expand Up @@ -359,7 +359,7 @@ def test_unsupported_bitcount() -> None:
"Tests/images/unimplemented_pfflags.dds",
),
)
def test_not_implemented(test_file) -> None:
def test_not_implemented(test_file: str) -> None:
with pytest.raises(NotImplementedError):
with Image.open(test_file):
pass
Expand All @@ -381,7 +381,7 @@ def test_save_unsupported_mode(tmp_path: Path) -> None:
("RGBA", "Tests/images/pil123rgba.png"),
],
)
def test_save(mode, test_file, tmp_path: Path) -> None:
def test_save(mode: str, test_file: str, tmp_path: Path) -> None:
out = str(tmp_path / "temp.dds")
with Image.open(test_file) as im:
assert im.mode == mode
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_fli.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_seek() -> None:
],
)
@pytest.mark.timeout(timeout=3)
def test_timeouts(test_file) -> None:
def test_timeouts(test_file: str) -> None:
with open(test_file, "rb") as f:
with Image.open(f) as im:
with pytest.raises(OSError):
Expand All @@ -160,7 +160,7 @@ def test_timeouts(test_file) -> None:
"Tests/images/crash-5762152299364352.fli",
],
)
def test_crash(test_file) -> None:
def test_crash(test_file: str) -> None:
with open(test_file, "rb") as f:
with Image.open(f) as im:
with pytest.raises(OSError):
Expand Down
7 changes: 4 additions & 3 deletions Tests/test_file_gribstub.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from pathlib import Path
from typing import IO

import pytest

Expand Down Expand Up @@ -55,15 +56,15 @@ class TestHandler:
loaded = False
saved = False

def open(self, im) -> None:
def open(self, im: Image.Image) -> None:
self.opened = True

def load(self, im):
def load(self, im: Image.Image) -> Image.Image:
self.loaded = True
im.fp.close()
return Image.new("RGB", (1, 1))

def save(self, im, fp, filename) -> None:
def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
self.saved = True

handler = TestHandler()
Expand Down
7 changes: 4 additions & 3 deletions Tests/test_file_hdf5stub.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from pathlib import Path
from typing import IO

import pytest

Expand Down Expand Up @@ -56,15 +57,15 @@ class TestHandler:
loaded = False
saved = False

def open(self, im) -> None:
def open(self, im: Image.Image) -> None:
self.opened = True

def load(self, im):
def load(self, im: Image.Image) -> Image.Image:
self.loaded = True
im.fp.close()
return Image.new("RGB", (1, 1))

def save(self, im, fp, filename) -> None:
def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
self.saved = True

handler = TestHandler()
Expand Down
22 changes: 12 additions & 10 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from io import BytesIO
from pathlib import Path
from types import ModuleType
from typing import Any

import pytest
Expand Down Expand Up @@ -33,6 +34,7 @@
skip_unless_feature,
)

ElementTree: ModuleType | None
try:
from defusedxml import ElementTree
except ImportError:
Expand Down Expand Up @@ -440,25 +442,25 @@ def getsampling(im: Image.Image):
for subsampling in (-1, 3): # (default, invalid)
im = self.roundtrip(hopper(), subsampling=subsampling)
assert getsampling(im) == (2, 2, 1, 1, 1, 1)
for subsampling in (0, "4:4:4"):
im = self.roundtrip(hopper(), subsampling=subsampling)
for subsampling1 in (0, "4:4:4"):
im = self.roundtrip(hopper(), subsampling=subsampling1)
assert getsampling(im) == (1, 1, 1, 1, 1, 1)
for subsampling in (1, "4:2:2"):
im = self.roundtrip(hopper(), subsampling=subsampling)
for subsampling1 in (1, "4:2:2"):
im = self.roundtrip(hopper(), subsampling=subsampling1)
assert getsampling(im) == (2, 1, 1, 1, 1, 1)
for subsampling in (2, "4:2:0", "4:1:1"):
im = self.roundtrip(hopper(), subsampling=subsampling)
for subsampling1 in (2, "4:2:0", "4:1:1"):
im = self.roundtrip(hopper(), subsampling=subsampling1)
assert getsampling(im) == (2, 2, 1, 1, 1, 1)

# RGB colorspace
for subsampling in (-1, 0, "4:4:4"):
for subsampling1 in (-1, 0, "4:4:4"):
# "4:4:4" doesn't really make sense for RGB, but the conversion
# to an integer happens at a higher level
im = self.roundtrip(hopper(), keep_rgb=True, subsampling=subsampling)
im = self.roundtrip(hopper(), keep_rgb=True, subsampling=subsampling1)
assert getsampling(im) == (1, 1, 1, 1, 1, 1)
for subsampling in (1, "4:2:2", 2, "4:2:0", 3):
for subsampling1 in (1, "4:2:2", 2, "4:2:0", 3):
with pytest.raises(OSError):
self.roundtrip(hopper(), keep_rgb=True, subsampling=subsampling)
self.roundtrip(hopper(), keep_rgb=True, subsampling=subsampling1)

with pytest.raises(TypeError):
self.roundtrip(hopper(), subsampling="1:1:1")
Expand Down
8 changes: 4 additions & 4 deletions Tests/test_file_palm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .helper import assert_image_equal, hopper, magick_command


def helper_save_as_palm(tmp_path: Path, mode) -> None:
def helper_save_as_palm(tmp_path: Path, mode: str) -> None:
# Arrange
im = hopper(mode)
outfile = str(tmp_path / ("temp_" + mode + ".palm"))
Expand All @@ -24,7 +24,7 @@ def helper_save_as_palm(tmp_path: Path, mode) -> None:
assert os.path.getsize(outfile) > 0


def open_with_magick(magick, tmp_path: Path, f):
def open_with_magick(magick: list[str], tmp_path: Path, f: str) -> Image.Image:
outfile = str(tmp_path / "temp.png")
rc = subprocess.call(
magick + [f, outfile], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
Expand All @@ -33,7 +33,7 @@ def open_with_magick(magick, tmp_path: Path, f):
return Image.open(outfile)


def roundtrip(tmp_path: Path, mode) -> None:
def roundtrip(tmp_path: Path, mode: str) -> None:
magick = magick_command()
if not magick:
return
Expand Down Expand Up @@ -66,6 +66,6 @@ def test_p_mode(tmp_path: Path) -> None:


@pytest.mark.parametrize("mode", ("L", "RGB"))
def test_oserror(tmp_path: Path, mode) -> None:
def test_oserror(tmp_path: Path, mode: str) -> None:
with pytest.raises(OSError):
helper_save_as_palm(tmp_path, mode)
2 changes: 1 addition & 1 deletion Tests/test_file_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
("jpg", "hopper.jpg", "JPEG"),
),
)
def test_sanity(codec, test_path, format) -> None:
def test_sanity(codec: str, test_path: str, format: str) -> None:
if features.check(codec):
with TarIO.TarIO(TEST_TAR_FILE, test_path) as tar:
with Image.open(tar) as im:
Expand Down
2 changes: 2 additions & 0 deletions Tests/test_file_webp_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from io import BytesIO
from pathlib import Path
from types import ModuleType

import pytest

Expand All @@ -14,6 +15,7 @@
skip_unless_feature("webp_mux"),
]

ElementTree: ModuleType | None
try:
from defusedxml import ElementTree
except ImportError:
Expand Down
Loading
Loading