-
-
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 #8125
Added type hints #8125
Conversation
entries.append( | ||
{"type": type, "size": HEADERSIZE + len(stream), "stream": stream} | ||
) | ||
entries.append((type, HEADERSIZE + len(stream), stream)) |
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.
What triggered 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.
Once i type hinted the _save
method in this plugin, mypy reported
src/PIL/IcnsImagePlugin.py:358: error: Generator has incompatible item type "object"; expected "bool" [misc]
file_length += sum(entry["size"] for entry in entries)
^~~~~~~~~~~~~
src/PIL/IcnsImagePlugin.py:365: error: No overload variant of "write" of "IO" matches argument type "object" [call-overload]
fp.write(entry["type"])
^~~~~~~~~~~~~~~~~~~~~~~
src/PIL/IcnsImagePlugin.py:365: note: Possible overload variants:
src/PIL/IcnsImagePlugin.py:365: note: def write(self, Buffer, /) -> int
src/PIL/IcnsImagePlugin.py:365: note: def write(self, bytes, /) -> int
src/PIL/IcnsImagePlugin.py:370: error: No overload variant of "write" of "IO" matches argument type "object" [call-overload]
fp.write(entry["type"])
^~~~~~~~~~~~~~~~~~~~~~~
src/PIL/IcnsImagePlugin.py:370: note: Possible overload variants:
src/PIL/IcnsImagePlugin.py:370: note: def write(self, Buffer, /) -> int
src/PIL/IcnsImagePlugin.py:370: note: def write(self, bytes, /) -> int
src/PIL/IcnsImagePlugin.py:372: error: No overload variant of "write" of "IO" matches argument type "object" [call-overload]
fp.write(entry["stream"])
^~~~~~~~~~~~~~~~~~~~~~~~~
src/PIL/IcnsImagePlugin.py:372: note: Possible overload variants:
src/PIL/IcnsImagePlugin.py:372: note: def write(self, Buffer, /) -> int
src/PIL/IcnsImagePlugin.py:372: note: def write(self, bytes, /) -> int
Found 4 errors in 1 file (checked 1 source file)
This change fixes it. I can use a named tuple or something if you prefer.
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.
Thanks. Named tuples are nice, but this is very localised so I think we're fine with a basic tuple here.
if isinstance(filename, bytes): | ||
filename = filename.decode("ascii") |
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.
Is this also fixing a bug?
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.
In main at the moment, save handlers can receive strings or bytes as the filename.
Line 2454 in 53e82e4
filename: str | bytes = "" |
Lines 231 to 232 in 53e82e4
SAVE: dict[str, Callable[[Image, IO[bytes], str | bytes], None]] = {} | |
SAVE_ALL: dict[str, Callable[[Image, IO[bytes], str | bytes], None]] = {} |
Part of this PR is propagating that out to some plugins that were only using strings.
entries.append( | ||
{"type": type, "size": HEADERSIZE + len(stream), "stream": stream} | ||
) | ||
entries.append((type, HEADERSIZE + len(stream), stream)) |
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.
Thanks. Named tuples are nice, but this is very localised so I think we're fine with a basic tuple here.
No description provided.