Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added type hints #8319

Merged
merged 5 commits into from
Aug 24, 2024
Merged

Added type hints #8319

merged 5 commits into from
Aug 24, 2024

Conversation

radarhere
Copy link
Member

No description provided.

try:
fp = sys.stdout.buffer
except AttributeError:
fp = sys.stdout
Copy link
Member Author

Choose a reason for hiding this comment

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

When mypy looks at sys.stdout here, it says

expression has type "Union[TextIO, Any]"

I think #5437 misinterpreted the docs, and that really, sys.stdout will not accept the bytes that we are sending.

https://docs.python.org/3/library/sys.html#sys.stdout

be aware that the standard streams may be replaced with file-like objects like io.StringIO which do not support the buffer attribute.

return self._new(self.im.point_transform(scale, offset))
# for other modes, convert the function to a table
flatLut = [lut(i) for i in range(256)] * self.im.bands
flatLut = [lut(i) for i in range(256)] * self.im.bands # type: ignore[arg-type]
Copy link
Member Author

Choose a reason for hiding this comment

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

These ignores are to prevent

src/PIL/Image.py:1939: error: Argument 1 to "_getscaleoffset" has incompatible type "Union[Callable[[int], float], Callable[[_E], Union[_E, float]]]";
expected "Callable[[_E], Union[_E, float]]" [arg-type]
scale, offset = _getscaleoffset(lut)
^~~
src/PIL/Image.py:1942: error: Argument 1 has incompatible type "int"; expected "_E" [arg-type]
flatLut = [lut(i) for i in range(256)] * self.im.bands

It is because Callable[[_E], _E | float] is for the first line, and Callable[[int], float] is for the second line, and I don't think there's an elegant way to differentiate the values.

Copy link
Member

Choose a reason for hiding this comment

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

It's an underscore name so "private" and we can rename it without deprecation. I see very little use on GitHub:

https://github.com/search?q=%2F%5CbImage%5C._E%5Cb%2F+language%3APython&ref=opensearch&type=code

https://github.com/search?q=language%3APython+%2Ffrom+Image+import.*_E%5Cb%2F&ref=opensearch&type=code

But I don't mind being cautious and deprecating first if you prefer.

@radarhere radarhere force-pushed the type_hint branch 2 times, most recently from 330e341 to dab3346 Compare August 20, 2024 12:53
@@ -363,6 +363,7 @@ Classes
:show-inheritance:
.. autoclass:: PIL.Image.ImagePointHandler
.. autoclass:: PIL.Image.ImageTransformHandler
.. autoclass:: PIL.Image._E
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

For a user-facing class, can we give it a description or a better name?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a description, and deprecated it in favour of a new name, ImagePointTransform.

@radarhere radarhere added the Deprecation Feature that will be removed in the future label Aug 24, 2024
@radarhere radarhere removed the Deprecation Feature that will be removed in the future label Aug 24, 2024
@radarhere radarhere merged commit 4721c31 into python-pillow:main Aug 24, 2024
50 of 51 checks passed
@radarhere radarhere deleted the type_hint branch August 24, 2024 13:54
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants