Skip to content

Commit cc4ca5b

Browse files
authored
Added type hints (#9269)
2 parents 148a19e + b1e2f2e commit cc4ca5b

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

Tests/test_file_gbr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_multiple_load_operations() -> None:
3333
assert_image_equal_tofile(im, "Tests/images/gbr.png")
3434

3535

36-
def create_gbr_image(info: dict[str, int] = {}, magic_number=b"") -> BytesIO:
36+
def create_gbr_image(info: dict[str, int] = {}, magic_number: bytes = b"") -> BytesIO:
3737
return BytesIO(
3838
b"".join(
3939
_binary.o32be(i)

Tests/test_file_iptc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
def create_iptc_image(info: dict[str, int] = {}) -> BytesIO:
15-
def field(tag, value):
15+
def field(tag: tuple[int, int], value: bytes) -> bytes:
1616
return bytes((0x1C,) + tag + (0, len(value))) + value
1717

1818
data = field((3, 60), bytes((info.get("layers", 1), info.get("component", 0))))

Tests/test_imagetext.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def test_get_length(font: ImageFont.FreeTypeFont) -> None:
3232
assert ImageText.Text("y", font).get_length() == 12
3333
assert ImageText.Text("a", font).get_length() == 12
3434

35+
text = ImageText.Text("\n", font)
36+
with pytest.raises(ValueError, match="can't measure length of multiline text"):
37+
text.get_length()
38+
3539

3640
def test_get_bbox(font: ImageFont.FreeTypeFont) -> None:
3741
assert ImageText.Text("A", font).get_bbox() == (0, 4, 12, 16)
@@ -45,13 +49,20 @@ def test_standard_embedded_color(layout_engine: ImageFont.Layout) -> None:
4549
font = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
4650
text = ImageText.Text("Hello World!", font)
4751
text.embed_color()
52+
assert text.get_length() == 288
4853

4954
im = Image.new("RGB", (300, 64), "white")
5055
draw = ImageDraw.Draw(im)
5156
draw.text((10, 10), text, "#fa6")
5257

5358
assert_image_similar_tofile(im, "Tests/images/standard_embedded.png", 3.1)
5459

60+
text = ImageText.Text("", mode="1")
61+
with pytest.raises(
62+
ValueError, match="Embedded color supported only in RGB and RGBA modes"
63+
):
64+
text.embed_color()
65+
5566

5667
@skip_unless_feature("freetype2")
5768
def test_stroke() -> None:

src/PIL/ImageText.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _get_fontmode(self) -> str:
8888
else:
8989
return "L"
9090

91-
def get_length(self):
91+
def get_length(self) -> float:
9292
"""
9393
Returns length (in pixels with 1/64 precision) of text.
9494
@@ -130,8 +130,11 @@ def get_length(self):
130130
131131
:return: Either width for horizontal text, or height for vertical text.
132132
"""
133-
split_character = "\n" if isinstance(self.text, str) else b"\n"
134-
if split_character in self.text:
133+
if isinstance(self.text, str):
134+
multiline = "\n" in self.text
135+
else:
136+
multiline = b"\n" in self.text
137+
if multiline:
135138
msg = "can't measure length of multiline text"
136139
raise ValueError(msg)
137140
return self.font.getlength(
@@ -313,6 +316,5 @@ def get_bbox(
313316
max(bbox[3], bbox_line[3]),
314317
)
315318

316-
if bbox is None:
317-
return xy[0], xy[1], xy[0], xy[1]
319+
assert bbox is not None
318320
return bbox

0 commit comments

Comments
 (0)