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 Tests/test_font_*.py #7743

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 2 deletions Tests/test_font_bdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
filename = "Tests/images/courB08.bdf"


def test_sanity():
def test_sanity() -> None:
with open(filename, "rb") as test_file:
font = BdfFontFile.BdfFontFile(test_file)

assert isinstance(font, FontFile.FontFile)
assert len([_f for _f in font.glyph if _f]) == 190


def test_invalid_file():
def test_invalid_file() -> None:
with open("Tests/images/flower.jpg", "rb") as fp:
with pytest.raises(SyntaxError):
BdfFontFile.BdfFontFile(fp)
4 changes: 2 additions & 2 deletions Tests/test_font_crash.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class TestFontCrash:
def _fuzz_font(self, font):
def _fuzz_font(self, font: ImageFont.FreeTypeFont) -> None:
# from fuzzers.fuzz_font
font.getbbox("ABC")
font.getmask("test text")
Expand All @@ -18,7 +18,7 @@ def _fuzz_font(self, font):
draw.text((10, 10), "Test Text", font=font, fill="#000")

@skip_unless_feature("freetype2")
def test_segfault(self):
def test_segfault(self) -> None:
with pytest.raises(OSError):
font = ImageFont.truetype("Tests/fonts/fuzz_font-5203009437302784")
self._fuzz_font(font)
6 changes: 3 additions & 3 deletions Tests/test_font_leaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestTTypeFontLeak(PillowLeakTestCase):
iterations = 10
mem_limit = 4096 # k

def _test_font(self, font):
def _test_font(self, font: ImageFont.FreeTypeFont) -> None:
im = Image.new("RGB", (255, 255), "white")
draw = ImageDraw.ImageDraw(im)
self._test_leak(
Expand All @@ -20,7 +20,7 @@ def _test_font(self, font):
)

@skip_unless_feature("freetype2")
def test_leak(self):
def test_leak(self) -> None:
ttype = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 20)
self._test_font(ttype)

Expand All @@ -30,6 +30,6 @@ class TestDefaultFontLeak(TestTTypeFontLeak):
iterations = 100
mem_limit = 1024 # k

def test_leak(self):
def test_leak(self) -> None:
default_font = ImageFont.load_default()
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this test is checking for leaks with a PIL font and was also broken by #7354.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I've created #7748 for this.

self._test_font(default_font)
21 changes: 12 additions & 9 deletions Tests/test_font_pcf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
from pathlib import PosixPath

import pytest

Expand All @@ -20,7 +21,7 @@
pytestmark = skip_unless_feature("zlib")


def save_font(request, tmp_path):
def save_font(request: pytest.FixtureRequest, tmp_path: PosixPath) -> str:
Copy link

Choose a reason for hiding this comment

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

Why not tmp_path: Path?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I've pushed a commit to update it, both the changes from here and #7732

with open(fontname, "rb") as test_file:
font = PcfFontFile.PcfFontFile(test_file)
assert isinstance(font, FontFile.FontFile)
Expand All @@ -29,7 +30,7 @@ def save_font(request, tmp_path):

tempname = str(tmp_path / "temp.pil")

def delete_tempfile():
def delete_tempfile() -> None:
try:
os.remove(tempname[:-4] + ".pbm")
except OSError:
Expand All @@ -47,25 +48,25 @@ def delete_tempfile():
return tempname


def test_sanity(request, tmp_path):
def test_sanity(request: pytest.FixtureRequest, tmp_path: PosixPath) -> None:
save_font(request, tmp_path)


def test_less_than_256_characters():
def test_less_than_256_characters() -> None:
with open("Tests/fonts/10x20-ISO8859-1-fewer-characters.pcf", "rb") as test_file:
font = PcfFontFile.PcfFontFile(test_file)
assert isinstance(font, FontFile.FontFile)
# check the number of characters in the font
assert len([_f for _f in font.glyph if _f]) == 127


def test_invalid_file():
def test_invalid_file() -> None:
with open("Tests/images/flower.jpg", "rb") as fp:
with pytest.raises(SyntaxError):
PcfFontFile.PcfFontFile(fp)


def test_draw(request, tmp_path):
def test_draw(request: pytest.FixtureRequest, tmp_path: PosixPath) -> None:
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
im = Image.new("L", (130, 30), "white")
Expand All @@ -74,7 +75,7 @@ def test_draw(request, tmp_path):
assert_image_similar_tofile(im, "Tests/images/test_draw_pbm_target.png", 0)


def test_textsize(request, tmp_path):
def test_textsize(request: pytest.FixtureRequest, tmp_path: PosixPath) -> None:
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
for i in range(255):
Expand All @@ -90,7 +91,9 @@ def test_textsize(request, tmp_path):
assert font.getbbox(msg) == (0, 0, len(msg) * 10, 20)


def _test_high_characters(request, tmp_path, message):
def _test_high_characters(
request: pytest.FixtureRequest, tmp_path: PosixPath, message: str | bytes
) -> None:
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
im = Image.new("L", (750, 30), "white")
Expand All @@ -99,7 +102,7 @@ def _test_high_characters(request, tmp_path, message):
assert_image_similar_tofile(im, "Tests/images/high_ascii_chars.png", 0)


def test_high_characters(request, tmp_path):
def test_high_characters(request: pytest.FixtureRequest, tmp_path: PosixPath) -> None:
message = "".join(chr(i + 1) for i in range(140, 232))
_test_high_characters(request, tmp_path, message)
# accept bytes instances.
Expand Down
29 changes: 23 additions & 6 deletions Tests/test_font_pcf_charsets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import os
from pathlib import PosixPath
from typing import TypedDict

import pytest

Expand All @@ -14,7 +16,14 @@

fontname = "Tests/fonts/ter-x20b.pcf"

charsets = {

class Charset(TypedDict):
glyph_count: int
message: str
image1: str


charsets: dict[str, Charset] = {
"iso8859-1": {
"glyph_count": 223,
"message": "hello, world",
Expand All @@ -36,7 +45,9 @@
pytestmark = skip_unless_feature("zlib")


def save_font(request, tmp_path, encoding):
def save_font(
request: pytest.FixtureRequest, tmp_path: PosixPath, encoding: str
) -> str:
with open(fontname, "rb") as test_file:
font = PcfFontFile.PcfFontFile(test_file, encoding)
assert isinstance(font, FontFile.FontFile)
Expand All @@ -45,7 +56,7 @@ def save_font(request, tmp_path, encoding):

tempname = str(tmp_path / "temp.pil")

def delete_tempfile():
def delete_tempfile() -> None:
try:
os.remove(tempname[:-4] + ".pbm")
except OSError:
Expand All @@ -64,12 +75,16 @@ def delete_tempfile():


@pytest.mark.parametrize("encoding", ("iso8859-1", "iso8859-2", "cp1250"))
def test_sanity(request, tmp_path, encoding):
def test_sanity(
request: pytest.FixtureRequest, tmp_path: PosixPath, encoding: str
) -> None:
save_font(request, tmp_path, encoding)


@pytest.mark.parametrize("encoding", ("iso8859-1", "iso8859-2", "cp1250"))
def test_draw(request, tmp_path, encoding):
def test_draw(
request: pytest.FixtureRequest, tmp_path: PosixPath, encoding: str
) -> None:
tempname = save_font(request, tmp_path, encoding)
font = ImageFont.load(tempname)
im = Image.new("L", (150, 30), "white")
Expand All @@ -80,7 +95,9 @@ def test_draw(request, tmp_path, encoding):


@pytest.mark.parametrize("encoding", ("iso8859-1", "iso8859-2", "cp1250"))
def test_textsize(request, tmp_path, encoding):
def test_textsize(
request: pytest.FixtureRequest, tmp_path: PosixPath, encoding: str
) -> None:
tempname = save_font(request, tmp_path, encoding)
font = ImageFont.load(tempname)
for i in range(255):
Expand Down