Added type hints to FontFile and subclasses#7643
Merged
radarhere merged 4 commits intopython-pillow:mainfrom Dec 27, 2023
Merged
Added type hints to FontFile and subclasses#7643radarhere merged 4 commits intopython-pillow:mainfrom
radarhere merged 4 commits intopython-pillow:mainfrom
Conversation
hugovk
approved these changes
Dec 27, 2023
Member
hugovk
left a comment
There was a problem hiding this comment.
Great to find and fix some bugs along the way!
nulano
reviewed
Dec 27, 2023
src/PIL/PcfFontFile.py
Outdated
| data = fp.read(bitmapsize) | ||
|
|
||
| pad = BYTES_PER_ROW[padindex] | ||
| pad: Callable[[int], int] = BYTES_PER_ROW[padindex] |
Contributor
There was a problem hiding this comment.
I don't see why this one is needed?
Checking with typing.reveal_type, I see that it can be inferred if you add the following at the top of the file:
BYTES_PER_ROW: list[Callable[[int], int]] = [ ...
Member
Author
There was a problem hiding this comment.
Without it, python3 -m mypy --strict src/PIL/PcfFontFile.py gives
src/PIL/PcfFontFile.py:223: error: Call to untyped function (unknown) in typed context [no-untyped-call]
Image.frombytes("1", (xsize, ysize), data[b:e], "raw", mode, pad(xsize))
^~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)
Member
Author
There was a problem hiding this comment.
Your suggestion also works however, so I've pushed a commit.
Contributor
There was a problem hiding this comment.
Ah, I was missing the --strict.
nulano
approved these changes
Dec 27, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Helps #2625.
Adds type hints to FontFile, BdfFontFile and PcfFontFile.
In doing so,
Pillow/src/PIL/FontFile.py
Line 68 in 1e8bad8
is the only time that
FontFile.compile()returns something other thanNone. I've changed that to be consistent.to "ValueError: No bitmap created" instead.