Skip to content

Commit

Permalink
Merge pull request python-pillow#7755 from radarhere/type_hints
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Jan 27, 2024
2 parents f55c9c6 + 7373149 commit 232bddd
Show file tree
Hide file tree
Showing 25 changed files with 60 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Tests/test_000_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from PIL import Image


def test_sanity():
def test_sanity() -> None:
# Make sure we have the binary extension
Image.core.new("L", (100, 100))

Expand Down
6 changes: 3 additions & 3 deletions Tests/test_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
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

assert _binary.o16le(65535) == b"\xff\xff"
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

Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_cur.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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)
Expand All @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions Tests/test_file_ftex.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
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):
Expand Down
8 changes: 4 additions & 4 deletions Tests/test_file_gbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
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)

# Test again now that it has already been loaded once
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):
Expand Down
6 changes: 3 additions & 3 deletions Tests/test_file_gd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_gimppalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from PIL.GimpPaletteFile import GimpPaletteFile


def test_sanity():
def test_sanity() -> None:
with open("Tests/images/test.gpl", "rb") as fp:
GimpPaletteFile(fp)

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_imt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
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)
4 changes: 2 additions & 2 deletions Tests/test_file_mcidas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
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/
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_file_pcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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.

Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_pixar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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"
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_qoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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)
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_file_wal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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"
Expand All @@ -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

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

from pathlib import Path

import pytest

from PIL import Image
Expand All @@ -10,7 +12,7 @@
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")

Expand Down
6 changes: 3 additions & 3 deletions Tests/test_file_xpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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"
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Tests/test_file_xvthumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
TEST_FILE = "Tests/images/hopper.p7"


def test_open():
def test_open() -> None:
# Act
with Image.open(TEST_FILE) as im:
# Assert
Expand All @@ -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"
Expand All @@ -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"

Expand Down
4 changes: 3 additions & 1 deletion Tests/test_fontfile.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
6 changes: 3 additions & 3 deletions Tests/test_format_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from PIL import Image


def test_white():
def test_white() -> None:
with Image.open("Tests/images/lab.tif") as i:
i.load()

Expand All @@ -24,15 +24,15 @@ 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:
k = i.getpixel((0, 0))
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:
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_lib_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
path = "Tests/images/hopper.jpg"


def test_sanity():
def test_sanity() -> None:
with Image.open(path):
pass
try:
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_pyroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
pyroma = pytest.importorskip("pyroma", reason="Pyroma not installed")


def test_pyroma():
def test_pyroma() -> None:
# Arrange
data = pyroma.projectdata.get_data(".")

Expand Down
Loading

0 comments on commit 232bddd

Please sign in to comment.