Skip to content
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

Merged
merged 4 commits into from
Dec 27, 2023

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Dec 27, 2023

Helps #2625.

Adds type hints to FontFile, BdfFontFile and PcfFontFile.

In doing so,

  • I found that
    return ""

    is the only time that FontFile.compile() returns something other than None. I've changed that to be consistent.
  • I changed the following error
>>> from PIL import FontFile
>>> font = FontFile.FontFile()
>>> font.save("out")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/FontFile.py", line 97, in save
    self.bitmap.save(os.path.splitext(filename)[0] + ".pbm", "PNG")
AttributeError: 'NoneType' object has no attribute 'save'

to "ValueError: No bitmap created" instead.

Copy link
Member

@hugovk hugovk left a 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!

@@ -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]
Copy link
Contributor

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]] = [ ...

Copy link
Member Author

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)

Copy link
Member Author

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.

Copy link
Contributor

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.

@radarhere radarhere merged commit 78b96c0 into python-pillow:main Dec 27, 2023
56 checks passed
@radarhere radarhere deleted the type_hints branch December 27, 2023 11:59
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.

3 participants