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

Merged
merged 1 commit into from
Jun 1, 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
2 changes: 1 addition & 1 deletion Tests/test_font_leaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestTTypeFontLeak(PillowLeakTestCase):
iterations = 10
mem_limit = 4096 # k

def _test_font(self, font: ImageFont.FreeTypeFont) -> None:
def _test_font(self, font: ImageFont.FreeTypeFont | ImageFont.ImageFont) -> None:
im = Image.new("RGB", (255, 255), "white")
draw = ImageDraw.ImageDraw(im)
self._test_leak(
Expand Down
20 changes: 15 additions & 5 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,18 @@ def test_open_formats(self) -> None:
JPGFILE = "Tests/images/hopper.jpg"

with pytest.raises(TypeError):
with Image.open(PNGFILE, formats=123):
with Image.open(PNGFILE, formats=123): # type: ignore[arg-type]
pass

for formats in [["JPEG"], ("JPEG",), ["jpeg"], ["Jpeg"], ["jPeG"], ["JpEg"]]:
format_list: list[list[str] | tuple[str, ...]] = [
["JPEG"],
("JPEG",),
["jpeg"],
["Jpeg"],
["jPeG"],
["JpEg"],
]
for formats in format_list:
with pytest.raises(UnidentifiedImageError):
with Image.open(PNGFILE, formats=formats):
pass
Expand Down Expand Up @@ -138,7 +146,7 @@ def test_invalid_image(self) -> None:

def test_bad_mode(self) -> None:
with pytest.raises(ValueError):
with Image.open("filename", "bad mode"):
with Image.open("filename", "bad mode"): # type: ignore[arg-type]
pass

def test_stringio(self) -> None:
Expand Down Expand Up @@ -497,9 +505,11 @@ def test_effect_spread_zero(self) -> None:
def test_check_size(self) -> None:
# Checking that the _check_size function throws value errors when we want it to
with pytest.raises(ValueError):
Image.new("RGB", 0) # not a tuple
# not a tuple
Image.new("RGB", 0) # type: ignore[arg-type]
with pytest.raises(ValueError):
Image.new("RGB", (0,)) # Tuple too short
# tuple too short
Image.new("RGB", (0,)) # type: ignore[arg-type]
with pytest.raises(ValueError):
Image.new("RGB", (-1, -1)) # w,h < 0

Expand Down
26 changes: 14 additions & 12 deletions Tests/test_imagecms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
from io import BytesIO
from pathlib import Path
from typing import Any
from typing import Any, Literal, cast

import pytest

Expand Down Expand Up @@ -209,13 +209,13 @@ def test_exceptions() -> None:
ImageCms.buildTransform("foo", "bar", "RGB", "RGB")

with pytest.raises(ImageCms.PyCMSError, match="Invalid type for Profile"):
ImageCms.getProfileName(None)
ImageCms.getProfileName(None) # type: ignore[arg-type]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this warning look like?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests/test_imagecms.py:212: error: Argument 1 to "getProfileName" has incompatible type "None"; expected
"Union[str, SupportsRead[bytes], CmsProfile, ImageCmsProfile]"  [arg-type]
            ImageCms.getProfileName(None)
                                    ^~~~

The test's intention is to go through

def getProfileName(profile: _CmsProfileCompatible) -> str:

where

Pillow/src/PIL/ImageCms.py

Lines 369 to 371 in c757439

_CmsProfileCompatible = Union[
str, SupportsRead[bytes], core.CmsProfile, ImageCmsProfile
]

and then

Pillow/src/PIL/ImageCms.py

Lines 239 to 240 in c757439

class ImageCmsProfile:
def __init__(self, profile: str | SupportsRead[bytes] | core.CmsProfile) -> None:

to reach
msg = "Invalid type for Profile" # type: ignore[unreachable]

The type is deliberately incorrect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

skip_missing()

# Python <= 3.9: "an integer is required (got type NoneType)"
# Python > 3.9: "'NoneType' object cannot be interpreted as an integer"
with pytest.raises(ImageCms.PyCMSError, match="integer"):
ImageCms.isIntentSupported(SRGB, None, None)
ImageCms.isIntentSupported(SRGB, None, None) # type: ignore[arg-type]


def test_display_profile() -> None:
Expand All @@ -239,7 +239,7 @@ def test_unsupported_color_space() -> None:
"Color space not supported for on-the-fly profile creation (unsupported)"
),
):
ImageCms.createProfile("unsupported")
ImageCms.createProfile("unsupported") # type: ignore[arg-type]


def test_invalid_color_temperature() -> None:
Expand Down Expand Up @@ -352,7 +352,7 @@ def test_extended_information() -> None:
p = o.profile

def assert_truncated_tuple_equal(
tup1: tuple[Any, ...], tup2: tuple[Any, ...], digits: int = 10
tup1: tuple[Any, ...] | None, tup2: tuple[Any, ...], digits: int = 10
) -> None:
# Helper function to reduce precision of tuples of floats
# recursively and then check equality.
Expand All @@ -368,6 +368,7 @@ def truncate_tuple(tuple_value: tuple[Any, ...]) -> tuple[Any, ...]:
for val in tuple_value
)

assert tup1 is not None
assert truncate_tuple(tup1) == truncate_tuple(tup2)

assert p.attributes == 4294967296
Expand Down Expand Up @@ -513,22 +514,22 @@ def test_non_ascii_path(tmp_path: Path) -> None:
def test_profile_typesafety() -> None:
# does not segfault
with pytest.raises(TypeError, match="Invalid type for Profile"):
ImageCms.ImageCmsProfile(0).tobytes()
ImageCms.ImageCmsProfile(0) # type: ignore[arg-type]
with pytest.raises(TypeError, match="Invalid type for Profile"):
ImageCms.ImageCmsProfile(1).tobytes()
ImageCms.ImageCmsProfile(1) # type: ignore[arg-type]

# also check core function
with pytest.raises(TypeError):
ImageCms.core.profile_tobytes(0)
ImageCms.core.profile_tobytes(0) # type: ignore[arg-type]
with pytest.raises(TypeError):
ImageCms.core.profile_tobytes(1)
ImageCms.core.profile_tobytes(1) # type: ignore[arg-type]

if not is_pypy():
# core profile should not be directly instantiable
with pytest.raises(TypeError):
ImageCms.core.CmsProfile()
with pytest.raises(TypeError):
ImageCms.core.CmsProfile(0)
ImageCms.core.CmsProfile(0) # type: ignore[call-arg]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests/test_imagecms.py:532: error: Too many arguments for "CmsProfile"  [call-arg]
                ImageCms.core.CmsProfile(0)
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~



@pytest.mark.skipif(is_pypy(), reason="fails on PyPy")
Expand All @@ -537,7 +538,7 @@ def test_transform_typesafety() -> None:
with pytest.raises(TypeError):
ImageCms.core.CmsTransform()
with pytest.raises(TypeError):
ImageCms.core.CmsTransform(0)
ImageCms.core.CmsTransform(0) # type: ignore[call-arg]


def assert_aux_channel_preserved(
Expand Down Expand Up @@ -637,7 +638,8 @@ def test_auxiliary_channels_isolated() -> None:
continue

# convert with and without AUX data, test colors are equal
source_profile = ImageCms.createProfile(src_format[1])
src_colorSpace = cast(Literal["LAB", "XYZ", "sRGB"], src_format[1])
source_profile = ImageCms.createProfile(src_colorSpace)
destination_profile = ImageCms.createProfile(dst_format[1])
source_image = src_format[3]
test_transform = ImageCms.buildTransform(
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_imagestat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def test_sanity() -> None:
st.stddev

with pytest.raises(AttributeError):
st.spam()
st.spam() # type: ignore[attr-defined]

with pytest.raises(TypeError):
ImageStat.Stat(1)
ImageStat.Stat(1) # type: ignore[arg-type]


def test_hopper() -> None:
Expand Down
Loading