X Tutup
Skip to content

Type hints in _type1font.py#30187

Open
jkseppan wants to merge 1 commit intomatplotlib:mainfrom
jkseppan:typecheck-type1font
Open

Type hints in _type1font.py#30187
jkseppan wants to merge 1 commit intomatplotlib:mainfrom
jkseppan:typecheck-type1font

Conversation

@jkseppan
Copy link
Copy Markdown
Member

This file now passes mypy --strict (but its imports don't).

Improve error handling: a new class _ParseError instead of ValueError
or RuntimeError. Raise most of the errors from utility functions to
simplify the parsing logic. Move FontBBox parsing into a new utility
function.

This file now passes `mypy --strict` (but its imports don't).

Improve error handling: a new class _ParseError instead of ValueError
or RuntimeError. Raise most of the errors from utility functions to
simplify the parsing logic. Move FontBBox parsing into a new utility
function.
@tacaswell tacaswell added this to the v3.11.0 milestone Jun 19, 2025
Comment on lines +111 to +113
if isinstance(self, _BinaryToken):
return self.raw[1:]
raise _ParseError(f"Token {self} is not a binary token")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't care too much about it, but the error/nonerror code flow in this function is reversed from the above one.

Comment on lines +175 to +179
def value(self) -> str | bytes:
return self._value()

@functools.lru_cache
def value(self):
def _value(self) -> str | bytes:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does lru_cache break something here? It seems the same as before, with an extra method.

_abbr: dict[T.Literal['RD', 'ND', 'NP'], str]

def __init__(self, input):
def __init__(self, input: str | tuple[bytes, bytes, bytes]):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The doc says already-decoded parts; is that wrong?

self._parse()

def _read(self, file):
def _read(self, file: T.IO[bytes]) -> bytes:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That can be?

Suggested change
def _read(self, file: T.IO[bytes]) -> bytes:
def _read(self, file: T.BinaryIO) -> bytes:

Comment on lines +564 to +565
case int():
key_int = key
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Coverage seems to think this line isn't tested; what needed this change?

Comment on lines +628 to 629
pos: dict[str, list[tuple[int, int]]] = {}
pos = {}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pos is set here twice.


# Fill in the various *Name properties
if 'FontName' not in prop:
if not prop['FontName']:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we be certain that an empty string is not allowed here (and below)?


@staticmethod
def _charstring_tokens(data):
def _charstring_tokens(data: T.Iterable[int]) -> T.Generator[str | int, None, None]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

typing.Generator is deprecated.

Suggested change
def _charstring_tokens(data: T.Iterable[int]) -> T.Generator[str | int, None, None]:
def _charstring_tokens(data: T.Iterable[int]) -> collections.abc.Iterator[str | int]:

Comment on lines +226 to +227
def _is_number(token: _Token[str] | _Token[bytes]) -> T.TypeGuard[_NumberToken]:
return token.is_number()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unused?

Comment on lines -686 to -691
index_token = next(tokens)
if not index_token.is_number():
_log.warning(
f"Parsing encoding: expected number, got {index_token}"
)
continue
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This previously ignored parsing failures; did you intend to raise here?

Comment on lines +664 to +665
T.cast(dict[str, T.Any], prop)[key], endpos = \
subparsers[key](source, data)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe split these lines:

Suggested change
T.cast(dict[str, T.Any], prop)[key], endpos = \
subparsers[key](source, data)
value, endpos = subparsers[key](source, data)
T.cast(dict[str, T.Any], prop)[key] = value


if token.is_delim():
value = _expression(token, source, data).raw
value: T.Any = _expression(token, source, data).raw
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This annotation seems unneeded?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

X Tutup