Skip to content

Commit be1508f

Browse files
authored
Merge pull request #7743 from radarhere/type_hint_test_font
2 parents 1185fb8 + 4814bee commit be1508f

8 files changed

Lines changed: 46 additions & 32 deletions

Tests/check_j2k_overflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from __future__ import annotations
22

3-
from pathlib import PosixPath
3+
from pathlib import Path
44

55
import pytest
66

77
from PIL import Image
88

99

10-
def test_j2k_overflow(tmp_path: PosixPath) -> None:
10+
def test_j2k_overflow(tmp_path: Path) -> None:
1111
im = Image.new("RGBA", (1024, 131584))
1212
target = str(tmp_path / "temp.jpc")
1313
with pytest.raises(OSError):

Tests/check_large_memory.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4-
from pathlib import PosixPath
4+
from pathlib import Path
55
from types import ModuleType
66

77
import pytest
@@ -31,18 +31,18 @@
3131
pytestmark = pytest.mark.skipif(sys.maxsize <= 2**32, reason="requires 64-bit system")
3232

3333

34-
def _write_png(tmp_path: PosixPath, xdim: int, ydim: int) -> None:
34+
def _write_png(tmp_path: Path, xdim: int, ydim: int) -> None:
3535
f = str(tmp_path / "temp.png")
3636
im = Image.new("L", (xdim, ydim), 0)
3737
im.save(f)
3838

3939

40-
def test_large(tmp_path: PosixPath) -> None:
40+
def test_large(tmp_path: Path) -> None:
4141
"""succeeded prepatch"""
4242
_write_png(tmp_path, XDIM, YDIM)
4343

4444

45-
def test_2gpx(tmp_path: PosixPath) -> None:
45+
def test_2gpx(tmp_path: Path) -> None:
4646
"""failed prepatch"""
4747
_write_png(tmp_path, XDIM, XDIM)
4848

Tests/check_large_memory_numpy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import sys
4-
from pathlib import PosixPath
4+
from pathlib import Path
55

66
import pytest
77

@@ -25,19 +25,19 @@
2525
pytestmark = pytest.mark.skipif(sys.maxsize <= 2**32, reason="requires 64-bit system")
2626

2727

28-
def _write_png(tmp_path: PosixPath, xdim: int, ydim: int) -> None:
28+
def _write_png(tmp_path: Path, xdim: int, ydim: int) -> None:
2929
dtype = np.uint8
3030
a = np.zeros((xdim, ydim), dtype=dtype)
3131
f = str(tmp_path / "temp.png")
3232
im = Image.fromarray(a, "L")
3333
im.save(f)
3434

3535

36-
def test_large(tmp_path: PosixPath) -> None:
36+
def test_large(tmp_path: Path) -> None:
3737
"""succeeded prepatch"""
3838
_write_png(tmp_path, XDIM, YDIM)
3939

4040

41-
def test_2gpx(tmp_path: PosixPath) -> None:
41+
def test_2gpx(tmp_path: Path) -> None:
4242
"""failed prepatch"""
4343
_write_png(tmp_path, XDIM, XDIM)

Tests/test_font_bdf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
filename = "Tests/images/courB08.bdf"
88

99

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

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

1717

18-
def test_invalid_file():
18+
def test_invalid_file() -> None:
1919
with open("Tests/images/flower.jpg", "rb") as fp:
2020
with pytest.raises(SyntaxError):
2121
BdfFontFile.BdfFontFile(fp)

Tests/test_font_crash.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

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

2020
@skip_unless_feature("freetype2")
21-
def test_segfault(self):
21+
def test_segfault(self) -> None:
2222
with pytest.raises(OSError):
2323
font = ImageFont.truetype("Tests/fonts/fuzz_font-5203009437302784")
2424
self._fuzz_font(font)

Tests/test_font_leaks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TestTTypeFontLeak(PillowLeakTestCase):
1010
iterations = 10
1111
mem_limit = 4096 # k
1212

13-
def _test_font(self, font):
13+
def _test_font(self, font: ImageFont.FreeTypeFont) -> None:
1414
im = Image.new("RGB", (255, 255), "white")
1515
draw = ImageDraw.ImageDraw(im)
1616
self._test_leak(
@@ -20,7 +20,7 @@ def _test_font(self, font):
2020
)
2121

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

@@ -30,6 +30,6 @@ class TestDefaultFontLeak(TestTTypeFontLeak):
3030
iterations = 100
3131
mem_limit = 1024 # k
3232

33-
def test_leak(self):
33+
def test_leak(self) -> None:
3434
default_font = ImageFont.load_default()
3535
self._test_font(default_font)

Tests/test_font_pcf.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
from pathlib import Path
45

56
import pytest
67

@@ -20,7 +21,7 @@
2021
pytestmark = skip_unless_feature("zlib")
2122

2223

23-
def save_font(request, tmp_path):
24+
def save_font(request: pytest.FixtureRequest, tmp_path: Path) -> str:
2425
with open(fontname, "rb") as test_file:
2526
font = PcfFontFile.PcfFontFile(test_file)
2627
assert isinstance(font, FontFile.FontFile)
@@ -29,7 +30,7 @@ def save_font(request, tmp_path):
2930

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

32-
def delete_tempfile():
33+
def delete_tempfile() -> None:
3334
try:
3435
os.remove(tempname[:-4] + ".pbm")
3536
except OSError:
@@ -47,25 +48,25 @@ def delete_tempfile():
4748
return tempname
4849

4950

50-
def test_sanity(request, tmp_path):
51+
def test_sanity(request: pytest.FixtureRequest, tmp_path: Path) -> None:
5152
save_font(request, tmp_path)
5253

5354

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

6162

62-
def test_invalid_file():
63+
def test_invalid_file() -> None:
6364
with open("Tests/images/flower.jpg", "rb") as fp:
6465
with pytest.raises(SyntaxError):
6566
PcfFontFile.PcfFontFile(fp)
6667

6768

68-
def test_draw(request, tmp_path):
69+
def test_draw(request: pytest.FixtureRequest, tmp_path: Path) -> None:
6970
tempname = save_font(request, tmp_path)
7071
font = ImageFont.load(tempname)
7172
im = Image.new("L", (130, 30), "white")
@@ -74,7 +75,7 @@ def test_draw(request, tmp_path):
7475
assert_image_similar_tofile(im, "Tests/images/test_draw_pbm_target.png", 0)
7576

7677

77-
def test_textsize(request, tmp_path):
78+
def test_textsize(request: pytest.FixtureRequest, tmp_path: Path) -> None:
7879
tempname = save_font(request, tmp_path)
7980
font = ImageFont.load(tempname)
8081
for i in range(255):
@@ -90,7 +91,9 @@ def test_textsize(request, tmp_path):
9091
assert font.getbbox(msg) == (0, 0, len(msg) * 10, 20)
9192

9293

93-
def _test_high_characters(request, tmp_path, message):
94+
def _test_high_characters(
95+
request: pytest.FixtureRequest, tmp_path: Path, message: str | bytes
96+
) -> None:
9497
tempname = save_font(request, tmp_path)
9598
font = ImageFont.load(tempname)
9699
im = Image.new("L", (750, 30), "white")
@@ -99,7 +102,7 @@ def _test_high_characters(request, tmp_path, message):
99102
assert_image_similar_tofile(im, "Tests/images/high_ascii_chars.png", 0)
100103

101104

102-
def test_high_characters(request, tmp_path):
105+
def test_high_characters(request: pytest.FixtureRequest, tmp_path: Path) -> None:
103106
message = "".join(chr(i + 1) for i in range(140, 232))
104107
_test_high_characters(request, tmp_path, message)
105108
# accept bytes instances.

Tests/test_font_pcf_charsets.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

33
import os
4+
from pathlib import Path
5+
from typing import TypedDict
46

57
import pytest
68

@@ -14,7 +16,14 @@
1416

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

17-
charsets = {
19+
20+
class Charset(TypedDict):
21+
glyph_count: int
22+
message: str
23+
image1: str
24+
25+
26+
charsets: dict[str, Charset] = {
1827
"iso8859-1": {
1928
"glyph_count": 223,
2029
"message": "hello, world",
@@ -36,7 +45,7 @@
3645
pytestmark = skip_unless_feature("zlib")
3746

3847

39-
def save_font(request, tmp_path, encoding):
48+
def save_font(request: pytest.FixtureRequest, tmp_path: Path, encoding: str) -> str:
4049
with open(fontname, "rb") as test_file:
4150
font = PcfFontFile.PcfFontFile(test_file, encoding)
4251
assert isinstance(font, FontFile.FontFile)
@@ -45,7 +54,7 @@ def save_font(request, tmp_path, encoding):
4554

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

48-
def delete_tempfile():
57+
def delete_tempfile() -> None:
4958
try:
5059
os.remove(tempname[:-4] + ".pbm")
5160
except OSError:
@@ -64,12 +73,12 @@ def delete_tempfile():
6473

6574

6675
@pytest.mark.parametrize("encoding", ("iso8859-1", "iso8859-2", "cp1250"))
67-
def test_sanity(request, tmp_path, encoding):
76+
def test_sanity(request: pytest.FixtureRequest, tmp_path: Path, encoding: str) -> None:
6877
save_font(request, tmp_path, encoding)
6978

7079

7180
@pytest.mark.parametrize("encoding", ("iso8859-1", "iso8859-2", "cp1250"))
72-
def test_draw(request, tmp_path, encoding):
81+
def test_draw(request: pytest.FixtureRequest, tmp_path: Path, encoding: str) -> None:
7382
tempname = save_font(request, tmp_path, encoding)
7483
font = ImageFont.load(tempname)
7584
im = Image.new("L", (150, 30), "white")
@@ -80,7 +89,9 @@ def test_draw(request, tmp_path, encoding):
8089

8190

8291
@pytest.mark.parametrize("encoding", ("iso8859-1", "iso8859-2", "cp1250"))
83-
def test_textsize(request, tmp_path, encoding):
92+
def test_textsize(
93+
request: pytest.FixtureRequest, tmp_path: Path, encoding: str
94+
) -> None:
8495
tempname = save_font(request, tmp_path, encoding)
8596
font = ImageFont.load(tempname)
8697
for i in range(255):

0 commit comments

Comments
 (0)