Merge pull request #8108 from radarhere/type_hint

This commit is contained in:
Hugo van Kemenade 2024-06-08 07:36:53 -06:00 committed by GitHub
commit 0a45381c2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 42 additions and 26 deletions

View File

@ -44,6 +44,7 @@ def test_direct() -> None:
caccess = im.im.pixel_access(False) caccess = im.im.pixel_access(False)
access = PyAccess.new(im, False) access = PyAccess.new(im, False)
assert access is not None
assert caccess[(0, 0)] == access[(0, 0)] assert caccess[(0, 0)] == access[(0, 0)]
print(f"Size: {im.width}x{im.height}") print(f"Size: {im.width}x{im.height}")

View File

@ -124,7 +124,7 @@ def test_unsupported_module() -> None:
@pytest.mark.parametrize("supported_formats", (True, False)) @pytest.mark.parametrize("supported_formats", (True, False))
def test_pilinfo(supported_formats) -> None: def test_pilinfo(supported_formats: bool) -> None:
buf = io.StringIO() buf = io.StringIO()
features.pilinfo(buf, supported_formats=supported_formats) features.pilinfo(buf, supported_formats=supported_formats)
out = buf.getvalue() out = buf.getvalue()

View File

@ -140,7 +140,7 @@ def test_load_dib() -> None:
(124, "g/pal8v5.bmp"), (124, "g/pal8v5.bmp"),
), ),
) )
def test_dib_header_size(header_size, path): def test_dib_header_size(header_size: int, path: str) -> None:
image_path = "Tests/images/bmp/" + path image_path = "Tests/images/bmp/" + path
with open(image_path, "rb") as fp: with open(image_path, "rb") as fp:
data = fp.read()[14:] data = fp.read()[14:]

View File

@ -1,10 +1,11 @@
from __future__ import annotations from __future__ import annotations
from pathlib import Path from pathlib import Path
from typing import IO
import pytest import pytest
from PIL import BufrStubImagePlugin, Image from PIL import BufrStubImagePlugin, Image, ImageFile
from .helper import hopper from .helper import hopper
@ -50,20 +51,20 @@ def test_save(tmp_path: Path) -> None:
def test_handler(tmp_path: Path) -> None: def test_handler(tmp_path: Path) -> None:
class TestHandler: class TestHandler(ImageFile.StubHandler):
opened = False opened = False
loaded = False loaded = False
saved = False saved = False
def open(self, im) -> None: def open(self, im: ImageFile.StubImageFile) -> None:
self.opened = True self.opened = True
def load(self, im): def load(self, im: ImageFile.StubImageFile) -> Image.Image:
self.loaded = True self.loaded = True
im.fp.close() im.fp.close()
return Image.new("RGB", (1, 1)) 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 self.saved = True
handler = TestHandler() handler = TestHandler()

View File

@ -5,7 +5,7 @@ from typing import IO
import pytest import pytest
from PIL import GribStubImagePlugin, Image from PIL import GribStubImagePlugin, Image, ImageFile
from .helper import hopper from .helper import hopper
@ -51,7 +51,7 @@ def test_save(tmp_path: Path) -> None:
def test_handler(tmp_path: Path) -> None: def test_handler(tmp_path: Path) -> None:
class TestHandler: class TestHandler(ImageFile.StubHandler):
opened = False opened = False
loaded = False loaded = False
saved = False saved = False

View File

@ -1,11 +1,12 @@
from __future__ import annotations from __future__ import annotations
from io import BytesIO
from pathlib import Path from pathlib import Path
from typing import IO from typing import IO
import pytest import pytest
from PIL import Hdf5StubImagePlugin, Image from PIL import Hdf5StubImagePlugin, Image, ImageFile
TEST_FILE = "Tests/images/hdf5.h5" TEST_FILE = "Tests/images/hdf5.h5"
@ -41,7 +42,7 @@ def test_load() -> None:
def test_save() -> None: def test_save() -> None:
# Arrange # Arrange
with Image.open(TEST_FILE) as im: with Image.open(TEST_FILE) as im:
dummy_fp = None dummy_fp = BytesIO()
dummy_filename = "dummy.filename" dummy_filename = "dummy.filename"
# Act / Assert: stub cannot save without an implemented handler # Act / Assert: stub cannot save without an implemented handler
@ -52,7 +53,7 @@ def test_save() -> None:
def test_handler(tmp_path: Path) -> None: def test_handler(tmp_path: Path) -> None:
class TestHandler: class TestHandler(ImageFile.StubHandler):
opened = False opened = False
loaded = False loaded = False
saved = False saved = False

View File

@ -171,7 +171,7 @@ class TestFileJpeg:
[TEST_FILE, "Tests/images/pil_sample_cmyk.jpg"], [TEST_FILE, "Tests/images/pil_sample_cmyk.jpg"],
) )
def test_dpi(self, test_image_path: str) -> None: def test_dpi(self, test_image_path: str) -> None:
def test(xdpi: int, ydpi: int | None = None): def test(xdpi: int, ydpi: int | None = None) -> tuple[int, int] | None:
with Image.open(test_image_path) as im: with Image.open(test_image_path) as im:
im = self.roundtrip(im, dpi=(xdpi, ydpi or xdpi)) im = self.roundtrip(im, dpi=(xdpi, ydpi or xdpi))
return im.info.get("dpi") return im.info.get("dpi")

View File

@ -198,7 +198,9 @@ class TestFileWebp:
(0, (0,), (-1, 0, 1, 2), (253, 254, 255, 256)), (0, (0,), (-1, 0, 1, 2), (253, 254, 255, 256)),
) )
@skip_unless_feature("webp_anim") @skip_unless_feature("webp_anim")
def test_invalid_background(self, background, tmp_path: Path) -> None: def test_invalid_background(
self, background: int | tuple[int, ...], tmp_path: Path
) -> None:
temp_file = str(tmp_path / "temp.webp") temp_file = str(tmp_path / "temp.webp")
im = hopper() im = hopper()
with pytest.raises(OSError): with pytest.raises(OSError):

View File

@ -69,7 +69,7 @@ def test_write_animation_RGB(tmp_path: Path) -> None:
are visually similar to the originals. are visually similar to the originals.
""" """
def check(temp_file) -> None: def check(temp_file: str) -> None:
with Image.open(temp_file) as im: with Image.open(temp_file) as im:
assert im.n_frames == 2 assert im.n_frames == 2

View File

@ -1,10 +1,11 @@
from __future__ import annotations from __future__ import annotations
from pathlib import Path from pathlib import Path
from typing import IO
import pytest import pytest
from PIL import Image, WmfImagePlugin from PIL import Image, ImageFile, WmfImagePlugin
from .helper import assert_image_similar_tofile, hopper from .helper import assert_image_similar_tofile, hopper
@ -34,10 +35,13 @@ def test_load() -> None:
def test_register_handler(tmp_path: Path) -> None: def test_register_handler(tmp_path: Path) -> None:
class TestHandler: class TestHandler(ImageFile.StubHandler):
methodCalled = False methodCalled = False
def save(self, im, fp, filename) -> None: def load(self, im: ImageFile.StubImageFile) -> Image.Image:
return Image.new("RGB", (1, 1))
def save(self, im: Image.Image, fp: IO[bytes], filename: str) -> None:
self.methodCalled = True self.methodCalled = True
handler = TestHandler() handler = TestHandler()
@ -70,7 +74,7 @@ def test_load_set_dpi() -> None:
@pytest.mark.parametrize("ext", (".wmf", ".emf")) @pytest.mark.parametrize("ext", (".wmf", ".emf"))
def test_save(ext, tmp_path: Path) -> None: def test_save(ext: str, tmp_path: Path) -> None:
im = hopper() im = hopper()
tmpfile = str(tmp_path / ("temp" + ext)) tmpfile = str(tmp_path / ("temp" + ext))

View File

@ -259,6 +259,7 @@ class TestCffi(AccessTest):
caccess = im.im.pixel_access(False) caccess = im.im.pixel_access(False)
with pytest.warns(DeprecationWarning): with pytest.warns(DeprecationWarning):
access = PyAccess.new(im, False) access = PyAccess.new(im, False)
assert access is not None
w, h = im.size w, h = im.size
for x in range(0, w, 10): for x in range(0, w, 10):
@ -289,6 +290,7 @@ class TestCffi(AccessTest):
caccess = im.im.pixel_access(False) caccess = im.im.pixel_access(False)
with pytest.warns(DeprecationWarning): with pytest.warns(DeprecationWarning):
access = PyAccess.new(im, False) access = PyAccess.new(im, False)
assert access is not None
w, h = im.size w, h = im.size
for x in range(0, w, 10): for x in range(0, w, 10):
@ -299,6 +301,8 @@ class TestCffi(AccessTest):
# Attempt to set the value on a read-only image # Attempt to set the value on a read-only image
with pytest.warns(DeprecationWarning): with pytest.warns(DeprecationWarning):
access = PyAccess.new(im, True) access = PyAccess.new(im, True)
assert access is not None
with pytest.raises(ValueError): with pytest.raises(ValueError):
access[(0, 0)] = color access[(0, 0)] = color
@ -341,6 +345,8 @@ class TestCffi(AccessTest):
im = Image.new(mode, (1, 1)) im = Image.new(mode, (1, 1))
with pytest.warns(DeprecationWarning): with pytest.warns(DeprecationWarning):
access = PyAccess.new(im, False) access = PyAccess.new(im, False)
assert access is not None
access.putpixel((0, 0), color) access.putpixel((0, 0), color)
if len(color) == 3: if len(color) == 3:

View File

@ -111,7 +111,9 @@ def test_load_first_unless_jpeg() -> None:
with Image.open("Tests/images/hopper.jpg") as im: with Image.open("Tests/images/hopper.jpg") as im:
draft = im.draft draft = im.draft
def im_draft(mode: str, size: tuple[int, int]): def im_draft(
mode: str, size: tuple[int, int]
) -> tuple[str, tuple[int, int, float, float]] | None:
result = draft(mode, size) result = draft(mode, size)
assert result is not None assert result is not None

View File

@ -58,7 +58,6 @@ def test_blur_formats(test_images: dict[str, ImageFile.ImageFile]) -> None:
blur = ImageFilter.GaussianBlur blur = ImageFilter.GaussianBlur
with pytest.raises(ValueError): with pytest.raises(ValueError):
im.convert("1").filter(blur) im.convert("1").filter(blur)
blur(im.convert("L"))
with pytest.raises(ValueError): with pytest.raises(ValueError):
im.convert("I").filter(blur) im.convert("I").filter(blur)
with pytest.raises(ValueError): with pytest.raises(ValueError):

View File

@ -46,7 +46,7 @@ def roundtrip(expected: Image.Image) -> None:
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed") @pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
def test_sanity(tmp_path: Path) -> None: def test_sanity(tmp_path: Path) -> None:
# Segfault test # Segfault test
app = QApplication([]) app: QApplication | None = QApplication([])
ex = Example() ex = Example()
assert app # Silence warning assert app # Silence warning
assert ex # Silence warning assert ex # Silence warning

View File

@ -17,7 +17,7 @@ from . import Image, ImageFile
_handler = None _handler = None
def register_handler(handler: ImageFile.StubHandler) -> None: def register_handler(handler: ImageFile.StubHandler | None) -> None:
""" """
Install application-specific BUFR image handler. Install application-specific BUFR image handler.

View File

@ -17,7 +17,7 @@ from . import Image, ImageFile
_handler = None _handler = None
def register_handler(handler: ImageFile.StubHandler) -> None: def register_handler(handler: ImageFile.StubHandler | None) -> None:
""" """
Install application-specific GRIB image handler. Install application-specific GRIB image handler.

View File

@ -17,7 +17,7 @@ from . import Image, ImageFile
_handler = None _handler = None
def register_handler(handler: ImageFile.StubHandler) -> None: def register_handler(handler: ImageFile.StubHandler | None) -> None:
""" """
Install application-specific HDF5 image handler. Install application-specific HDF5 image handler.

View File

@ -30,7 +30,7 @@ from ._binary import si32le as _long
_handler = None _handler = None
def register_handler(handler: ImageFile.StubHandler) -> None: def register_handler(handler: ImageFile.StubHandler | None) -> None:
""" """
Install application-specific WMF image handler. Install application-specific WMF image handler.