-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added type hints to FontFile and subclasses #7643
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great to find and fix some bugs along the way!
src/PIL/PcfFontFile.py
Outdated
@@ -207,7 +210,7 @@ def _load_bitmaps(self, metrics): | |||
|
|||
data = fp.read(bitmapsize) | |||
|
|||
pad = BYTES_PER_ROW[padindex] | |||
pad: Callable[[int], int] = BYTES_PER_ROW[padindex] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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]] = [ ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your suggestion also works however, so I've pushed a commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I was missing the --strict
.
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.