Skip to content

Commit 6f194bb

Browse files
committed
ROB: _font: Always returns a FontDescriptor; fix typing
mypy complained that the .from_font_resource method's return type is Optional[FontDescriptor]. Change the code to not confuse mypy.
1 parent 3b5c85f commit 6f194bb

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

pypdf/_font.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from dataclasses import dataclass, field
2-
from typing import Optional
32

43
from pypdf.generic import DictionaryObject
54

@@ -29,10 +28,10 @@ class FontDescriptor:
2928
character_widths: dict[str, int] = field(default_factory=dict)
3029

3130
@classmethod
32-
def from_font_resource(cls, pdf_font_dict: DictionaryObject) -> "Optional[FontDescriptor]":
31+
def from_font_resource(cls, pdf_font_dict: DictionaryObject) -> "FontDescriptor":
3332
from pypdf._codecs.core_fontmetrics import CORE_FONT_METRICS # noqa: PLC0415
3433
# Prioritize information from the PDF font dictionary
35-
font_name = pdf_font_dict.get("/BaseFont", "Unknown")
36-
if font_name[1:] in CORE_FONT_METRICS:
37-
return CORE_FONT_METRICS.get(font_name[1:])
34+
font_name = pdf_font_dict.get("/BaseFont", "Unknown").removeprefix("/")
35+
if font_name in CORE_FONT_METRICS:
36+
return CORE_FONT_METRICS[font_name]
3837
return cls(name=font_name)

0 commit comments

Comments
 (0)