-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add various type annotations #8046
base: main
Are you sure you want to change the base?
Conversation
FWIW I have some WIP type annotations for ImageFont and _imagingft here: main...nulano:Pillow:types-imagefont |
I think this is ready for review. The docs build might still fail, but I'd need help with that. |
Also, the pypy3.10 failure seems unrelated? |
Yeah, the PyPy one is a bit flaky. |
The error
is saying that the The way to solve this is by updating docs/reference/Image.rst to include it, by adding
But given that our other protocols so far are |
@@ -1,3 +1,18 @@ | |||
from typing import Any | |||
|
|||
from typing_extensions import Buffer |
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.
Just pointing out that is adding typing_extensions as a requirement. Previous discussion about this can be seen in #7642
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.
This is not super critical. We could use bytes | bytesarray | memoryview
here, instead. This would exclude other types using the buffer protocol, but I'm not sure how common that is with Pillow. (And it can always be # type: ignore
d, of course.)
@@ -34,7 +34,7 @@ def transform( | |||
self, | |||
size: tuple[int, int], | |||
image: Image.Image, | |||
**options: dict[str, str | int | tuple[int, ...] | list[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.
Could you explain why you made this change?
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.
The problem here is that options
can be any of the remaining Image.transform
arguments, i.e.:
resample: int = Resampling.NEAREST,
fill: int = 1,
fillcolor: float | tuple[float, ...] | str | None
dict
is wrong in any case, but we could annotate it as float | tuple[float, ...] | str | None
(with int
being redundant with float
), but that would imply that any argument could have that type. I see four alternatives:
- Don't bother and use
Any
. - Use
float | tuple[float, ...] | str | None
as that would prevent some typing errors, but not all (e.g.fill=(1,2,3)
would not be caught), at the cost of a slightly misleading type hints. - Just replace
options
with the actual forwarded arguments. (The option I would probably choose, as there are only three options.) - Use
Unpack
, which means we would need to introduce a newTypedDict
for the arguments.
Co-authored-by: Andrew Murray <[email protected]>
I've created srittau#2 with various suggestions. |
Changes proposed in this pull request:
Image
,ImageFont
,ImageDraw
, and_imagingft
modules.