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 ImageSequence #7635

Merged
merged 2 commits into from
Dec 25, 2023

Conversation

radarhere
Copy link
Member

Helps #2625

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.

Thanks!


Shall we add this and from typing import Self to Image.py so mypy --strict src/PIL/ImageSequence.py passes?

diff --git a/src/PIL/Image.py b/src/PIL/Image.py
index 1cb484b852..b121774095 100644
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -1181,7 +1181,7 @@ class Image:
 
         return im
 
-    def copy(self):
+    def copy(self) -> Self:
         """
         Copies this image. Use this method if you wish to paste things
         into an image, but still retain the original.
@@ -2450,7 +2450,7 @@ class Image:
         if open_fp:
             fp.close()
 
-    def seek(self, frame):
+    def seek(self, frame: int) -> None:
         """
         Seeks to the given frame in this sequence file. If you seek
         beyond the end of the sequence, the method raises an
@@ -2537,7 +2537,7 @@ class Image:
 
         return self._new(self.im.getband(channel))
 
-    def tell(self):
+    def tell(self) -> int:
         """
         Returns the current frame number. See :py:meth:`~PIL.Image.Image.seek`.
 

Wondering if we should have a TypeAlias for Image.Image in Image.py?

Something like this?

from typing import TypeAlias

ImageType: TypeAlias = Image.Image

Or maybe explicit is better?

Anyway, not necessary for this PR.

@radarhere radarhere force-pushed the type_hints_imagesequence branch from 55a3d8b to f8d7503 Compare December 25, 2023 07:59
@radarhere
Copy link
Member Author

Testing, I find that from typing import Self is only available in Python >= 3.11. Apart from that though, I've made changes to allow --strict to pass.

@hugovk hugovk merged commit fbecf77 into python-pillow:main Dec 25, 2023
56 checks passed
@radarhere radarhere deleted the type_hints_imagesequence branch December 25, 2023 21:23
@radarhere
Copy link
Member Author

Wondering if we should have a TypeAlias for Image.Image in Image.py?

It turns out that TypeAlias was only added in Python 3.10 - https://docs.python.org/3/library/typing.html#typing.TypeAlias

@hugovk
Copy link
Member

hugovk commented Dec 25, 2023

We should be able do it instead like ImageType = Image.Image:

For backwards compatibility, type aliases can also be created through simple assignment

https://docs.python.org/3/library/typing.html#type-aliases

@radarhere
Copy link
Member Author

Ok, see what you think of #7639

@radarhere
Copy link
Member Author

Further thought decided against the idea of #7639 / a type alias for Image.Image.

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