Conversation
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.
| if isinstance(self, _BinaryToken): | ||
| return self.raw[1:] | ||
| raise _ParseError(f"Token {self} is not a binary token") |
There was a problem hiding this comment.
I don't care too much about it, but the error/nonerror code flow in this function is reversed from the above one.
| def value(self) -> str | bytes: | ||
| return self._value() | ||
|
|
||
| @functools.lru_cache | ||
| def value(self): | ||
| def _value(self) -> str | bytes: |
There was a problem hiding this comment.
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]): |
There was a problem hiding this comment.
The doc says already-decoded parts; is that wrong?
| self._parse() | ||
|
|
||
| def _read(self, file): | ||
| def _read(self, file: T.IO[bytes]) -> bytes: |
There was a problem hiding this comment.
That can be?
| def _read(self, file: T.IO[bytes]) -> bytes: | |
| def _read(self, file: T.BinaryIO) -> bytes: |
| case int(): | ||
| key_int = key |
There was a problem hiding this comment.
Coverage seems to think this line isn't tested; what needed this change?
| pos: dict[str, list[tuple[int, int]]] = {} | ||
| pos = {} |
|
|
||
| # Fill in the various *Name properties | ||
| if 'FontName' not in prop: | ||
| if not prop['FontName']: |
There was a problem hiding this comment.
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]: |
There was a problem hiding this comment.
typing.Generator is deprecated.
| 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]: |
| def _is_number(token: _Token[str] | _Token[bytes]) -> T.TypeGuard[_NumberToken]: | ||
| return token.is_number() |
| index_token = next(tokens) | ||
| if not index_token.is_number(): | ||
| _log.warning( | ||
| f"Parsing encoding: expected number, got {index_token}" | ||
| ) | ||
| continue |
There was a problem hiding this comment.
This previously ignored parsing failures; did you intend to raise here?
| T.cast(dict[str, T.Any], prop)[key], endpos = \ | ||
| subparsers[key](source, data) |
There was a problem hiding this comment.
Maybe split these lines:
| 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 |
There was a problem hiding this comment.
This annotation seems unneeded?
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.