Added type hints

This commit is contained in:
Andrew Murray 2024-01-27 15:19:43 +11:00
parent f55c9c6e01
commit 737314923f
25 changed files with 60 additions and 54 deletions

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from PIL import Image
def test_sanity():
def test_sanity() -> None:
# Make sure we have the binary extension
Image.core.new("L", (100, 100))

View File

@ -3,12 +3,12 @@ from __future__ import annotations
from PIL import _binary
def test_standard():
def test_standard() -> None:
assert _binary.i8(b"*") == 42
assert _binary.o8(42) == b"*"
def test_little_endian():
def test_little_endian() -> None:
assert _binary.i16le(b"\xff\xff\x00\x00") == 65535
assert _binary.i32le(b"\xff\xff\x00\x00") == 65535
@ -16,7 +16,7 @@ def test_little_endian():
assert _binary.o32le(65535) == b"\xff\xff\x00\x00"
def test_big_endian():
def test_big_endian() -> None:
assert _binary.i16be(b"\x00\x00\xff\xff") == 0
assert _binary.i32be(b"\x00\x00\xff\xff") == 65535

View File

@ -7,7 +7,7 @@ from PIL import CurImagePlugin, Image
TEST_FILE = "Tests/images/deerstalker.cur"
def test_sanity():
def test_sanity() -> None:
with Image.open(TEST_FILE) as im:
assert im.size == (32, 32)
assert isinstance(im, CurImagePlugin.CurImageFile)
@ -17,7 +17,7 @@ def test_sanity():
assert im.getpixel((16, 16)) == (84, 87, 86, 255)
def test_invalid_file():
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):

View File

@ -7,18 +7,18 @@ from PIL import FtexImagePlugin, Image
from .helper import assert_image_equal_tofile, assert_image_similar
def test_load_raw():
def test_load_raw() -> None:
with Image.open("Tests/images/ftex_uncompressed.ftu") as im:
assert_image_equal_tofile(im, "Tests/images/ftex_uncompressed.png")
def test_load_dxt1():
def test_load_dxt1() -> None:
with Image.open("Tests/images/ftex_dxt1.ftc") as im:
with Image.open("Tests/images/ftex_dxt1.png") as target:
assert_image_similar(im, target.convert("RGBA"), 15)
def test_invalid_file():
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):

View File

@ -7,12 +7,12 @@ from PIL import GbrImagePlugin, Image
from .helper import assert_image_equal_tofile
def test_gbr_file():
def test_gbr_file() -> None:
with Image.open("Tests/images/gbr.gbr") as im:
assert_image_equal_tofile(im, "Tests/images/gbr.png")
def test_load():
def test_load() -> None:
with Image.open("Tests/images/gbr.gbr") as im:
assert im.load()[0, 0] == (0, 0, 0, 0)
@ -20,14 +20,14 @@ def test_load():
assert im.load()[0, 0] == (0, 0, 0, 0)
def test_multiple_load_operations():
def test_multiple_load_operations() -> None:
with Image.open("Tests/images/gbr.gbr") as im:
im.load()
im.load()
assert_image_equal_tofile(im, "Tests/images/gbr.png")
def test_invalid_file():
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):

View File

@ -7,18 +7,18 @@ from PIL import GdImageFile, UnidentifiedImageError
TEST_GD_FILE = "Tests/images/hopper.gd"
def test_sanity():
def test_sanity() -> None:
with GdImageFile.open(TEST_GD_FILE) as im:
assert im.size == (128, 128)
assert im.format == "GD"
def test_bad_mode():
def test_bad_mode() -> None:
with pytest.raises(ValueError):
GdImageFile.open(TEST_GD_FILE, "bad mode")
def test_invalid_file():
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(UnidentifiedImageError):

View File

@ -5,7 +5,7 @@ import pytest
from PIL.GimpPaletteFile import GimpPaletteFile
def test_sanity():
def test_sanity() -> None:
with open("Tests/images/test.gpl", "rb") as fp:
GimpPaletteFile(fp)
@ -22,7 +22,7 @@ def test_sanity():
GimpPaletteFile(fp)
def test_get_palette():
def test_get_palette() -> None:
# Arrange
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
palette_file = GimpPaletteFile(fp)

View File

@ -9,13 +9,13 @@ from PIL import Image, ImtImagePlugin
from .helper import assert_image_equal_tofile
def test_sanity():
def test_sanity() -> None:
with Image.open("Tests/images/bw_gradient.imt") as im:
assert_image_equal_tofile(im, "Tests/images/bw_gradient.png")
@pytest.mark.parametrize("data", (b"\n", b"\n-", b"width 1\n"))
def test_invalid_file(data):
def test_invalid_file(data: bytes) -> None:
with io.BytesIO(data) as fp:
with pytest.raises(SyntaxError):
ImtImagePlugin.ImtImageFile(fp)

View File

@ -7,14 +7,14 @@ from PIL import Image, McIdasImagePlugin
from .helper import assert_image_equal_tofile
def test_invalid_file():
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
McIdasImagePlugin.McIdasImageFile(invalid_file)
def test_valid_file():
def test_valid_file() -> None:
# Arrange
# https://ghrc.nsstc.nasa.gov/hydro/details/cmx3g8
# https://ghrc.nsstc.nasa.gov/pub/fieldCampaigns/camex3/cmx3g8/browse/

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from PIL import Image
def test_load_raw():
def test_load_raw() -> None:
with Image.open("Tests/images/hopper.pcd") as im:
im.load() # should not segfault.

View File

@ -9,7 +9,7 @@ from .helper import assert_image_similar, hopper
TEST_FILE = "Tests/images/hopper.pxr"
def test_sanity():
def test_sanity() -> None:
with Image.open(TEST_FILE) as im:
im.load()
assert im.mode == "RGB"
@ -21,7 +21,7 @@ def test_sanity():
assert_image_similar(im, im2, 4.8)
def test_invalid_file():
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):

View File

@ -7,7 +7,7 @@ from PIL import Image, QoiImagePlugin
from .helper import assert_image_equal_tofile
def test_sanity():
def test_sanity() -> None:
with Image.open("Tests/images/hopper.qoi") as im:
assert im.mode == "RGB"
assert im.size == (128, 128)
@ -23,7 +23,7 @@ def test_sanity():
assert_image_equal_tofile(im, "Tests/images/pil123rgba.png")
def test_invalid_file():
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):

View File

@ -7,7 +7,7 @@ from .helper import assert_image_equal_tofile
TEST_FILE = "Tests/images/hopper.wal"
def test_open():
def test_open() -> None:
with WalImageFile.open(TEST_FILE) as im:
assert im.format == "WAL"
assert im.format_description == "Quake2 Texture"
@ -19,7 +19,7 @@ def test_open():
assert_image_equal_tofile(im, "Tests/images/hopper_wal.png")
def test_load():
def test_load() -> None:
with WalImageFile.open(TEST_FILE) as im:
assert im.load()[0, 0] == 122

View File

@ -1,5 +1,7 @@
from __future__ import annotations
from pathlib import Path
import pytest
from PIL import Image
@ -10,7 +12,7 @@ _webp = pytest.importorskip("PIL._webp", reason="WebP support not installed")
RGB_MODE = "RGB"
def test_write_lossless_rgb(tmp_path):
def test_write_lossless_rgb(tmp_path: Path) -> None:
if _webp.WebPDecoderVersion() < 0x0200:
pytest.skip("lossless not included")

View File

@ -9,7 +9,7 @@ from .helper import assert_image_similar, hopper
TEST_FILE = "Tests/images/hopper.xpm"
def test_sanity():
def test_sanity() -> None:
with Image.open(TEST_FILE) as im:
im.load()
assert im.mode == "P"
@ -20,14 +20,14 @@ def test_sanity():
assert_image_similar(im.convert("RGB"), hopper("RGB"), 60)
def test_invalid_file():
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
XpmImagePlugin.XpmImageFile(invalid_file)
def test_load_read():
def test_load_read() -> None:
# Arrange
with Image.open(TEST_FILE) as im:
dummy_bytes = 1

View File

@ -9,7 +9,7 @@ from .helper import assert_image_similar, hopper
TEST_FILE = "Tests/images/hopper.p7"
def test_open():
def test_open() -> None:
# Act
with Image.open(TEST_FILE) as im:
# Assert
@ -20,7 +20,7 @@ def test_open():
assert_image_similar(im, im_hopper, 9)
def test_unexpected_eof():
def test_unexpected_eof() -> None:
# Test unexpected EOF reading XV thumbnail file
# Arrange
bad_file = "Tests/images/hopper_bad.p7"
@ -30,7 +30,7 @@ def test_unexpected_eof():
XVThumbImagePlugin.XVThumbImageFile(bad_file)
def test_invalid_file():
def test_invalid_file() -> None:
# Arrange
invalid_file = "Tests/images/flower.jpg"

View File

@ -1,11 +1,13 @@
from __future__ import annotations
from pathlib import Path
import pytest
from PIL import FontFile
def test_save(tmp_path):
def test_save(tmp_path: Path) -> None:
tempname = str(tmp_path / "temp.pil")
font = FontFile.FontFile()

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from PIL import Image
def test_white():
def test_white() -> None:
with Image.open("Tests/images/lab.tif") as i:
i.load()
@ -24,7 +24,7 @@ def test_white():
assert list(b) == [128] * 100
def test_green():
def test_green() -> None:
# l= 50 (/100), a = -100 (-128 .. 128) b=0 in PS
# == RGB: 0, 152, 117
with Image.open("Tests/images/lab-green.tif") as i:
@ -32,7 +32,7 @@ def test_green():
assert k == (128, 28, 128)
def test_red():
def test_red() -> None:
# l= 50 (/100), a = 100 (-128 .. 128) b=0 in PS
# == RGB: 255, 0, 124
with Image.open("Tests/images/lab-red.tif") as i:

View File

@ -5,7 +5,7 @@ import pytest
from PIL import Image
def test_setmode():
def test_setmode() -> None:
im = Image.new("L", (1, 1), 255)
im.im.setmode("1")
assert im.im.getpixel((0, 0)) == 255

View File

@ -24,7 +24,7 @@ from PIL import Image
path = "Tests/images/hopper.jpg"
def test_sanity():
def test_sanity() -> None:
with Image.open(path):
pass
try:

View File

@ -5,7 +5,7 @@ import subprocess
import sys
def test_main():
def test_main() -> None:
out = subprocess.check_output([sys.executable, "-m", "PIL"]).decode("utf-8")
lines = out.splitlines()
assert lines[0] == "-" * 68

View File

@ -7,7 +7,7 @@ from PIL import __version__
pyroma = pytest.importorskip("pyroma", reason="Pyroma not installed")
def test_pyroma():
def test_pyroma() -> None:
# Arrange
data = pyroma.projectdata.get_data(".")

View File

@ -3,13 +3,13 @@ from __future__ import annotations
from .helper import assert_image_equal, assert_image_similar, hopper
def check_upload_equal():
def check_upload_equal() -> None:
result = hopper("P").convert("RGB")
target = hopper("RGB")
assert_image_equal(result, target)
def check_upload_similar():
def check_upload_similar() -> None:
result = hopper("P").convert("RGB")
target = hopper("RGB")
assert_image_similar(result, target, 0)

View File

@ -1,11 +1,13 @@
from __future__ import annotations
from pathlib import Path
import pytest
from PIL import _util
def test_is_path():
def test_is_path() -> None:
# Arrange
fp = "filename.ext"
@ -16,7 +18,7 @@ def test_is_path():
assert it_is
def test_path_obj_is_path():
def test_path_obj_is_path() -> None:
# Arrange
from pathlib import Path
@ -29,7 +31,7 @@ def test_path_obj_is_path():
assert it_is
def test_is_not_path(tmp_path):
def test_is_not_path(tmp_path: Path) -> None:
# Arrange
with (tmp_path / "temp.ext").open("w") as fp:
pass
@ -41,7 +43,7 @@ def test_is_not_path(tmp_path):
assert not it_is_not
def test_is_directory():
def test_is_directory() -> None:
# Arrange
directory = "Tests"
@ -52,7 +54,7 @@ def test_is_directory():
assert it_is
def test_is_not_directory():
def test_is_not_directory() -> None:
# Arrange
text = "abc"
@ -63,7 +65,7 @@ def test_is_not_directory():
assert not it_is_not
def test_deferred_error():
def test_deferred_error() -> None:
# Arrange
# Act

View File

@ -14,11 +14,11 @@ class TestWebPLeaks(PillowLeakTestCase):
mem_limit = 3 * 1024 # kb
iterations = 100
def test_leak_load(self):
def test_leak_load(self) -> None:
with open(test_file, "rb") as f:
im_data = f.read()
def core():
def core() -> None:
with Image.open(BytesIO(im_data)) as im:
im.load()