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 #8125

Merged
merged 1 commit into from
Jun 10, 2024
Merged

Added type hints #8125

merged 1 commit into from
Jun 10, 2024

Conversation

radarhere
Copy link
Member

No description provided.

entries.append(
{"type": type, "size": HEADERSIZE + len(stream), "stream": stream}
)
entries.append((type, HEADERSIZE + len(stream), stream))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What triggered this change?

Copy link
Member Author

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.

Copy link
Member

@hugovk hugovk Jun 10, 2024

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.

Comment on lines +344 to +345
if isinstance(filename, bytes):
filename = filename.decode("ascii")
Copy link
Member

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?

Copy link
Member Author

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.

filename: str | bytes = ""

Pillow/src/PIL/Image.py

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))
Copy link
Member

@hugovk hugovk Jun 10, 2024

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.

@radarhere radarhere merged commit 9a8759d into python-pillow:main Jun 10, 2024
56 checks passed
@radarhere radarhere deleted the type_hint branch June 10, 2024 20:51
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.

2 participants