-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Edit getdata to recommend iterating in a type-correct way #9261
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
base: main
Are you sure you want to change the base?
Conversation
|
Just guessing from the other methods, would it be from collections.abc import Iterator
...
def __getitem__(self, index: int) -> float: ...
def __iter__(self) -> Iterator[float]: ...Or are other elements returned also? |
|
My suggestion would be Lines 1631 to 1633 in 5122c83
Regarding the first code you found Line 4 in 5122c83
I think this should actually be updated as well.
I don't know where this came from. |
>>> ic = im.getdata()
>>> ic.__iter__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ImagingCore' object has no attribute '__iter__'. Did you mean: '__str__'?I think the problem is in pyright/pyshed more than pillow. The A workaround that passes type-checking could be |
|
For completeness, I found this issue in typeshed and the upshot was that supporting |
|
Ok, in which case I will change the comment in Line 1436 in 5122c83
list(iter(im.getdata())) as it better matches the type
|
See https://github.com/python-pillow/Pillow/pull/92611 for discussion on this.
db20e21 to
fcc09af
Compare
| new_im.info["transparency"] = new_im.palette.getcolor( | ||
| cast(tuple[int, ...], trns), new_im # trns was converted to RGB | ||
| cast(tuple[int, ...], trns), | ||
| new_im, # trns was converted to RGB |
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 was your thinking behind this reformatting?
| def __arrow_c_array__( | ||
| self, requested_schema: "PyCapsule" = None # type: ignore[name-defined] # noqa: F821, UP037 | ||
| self, | ||
| requested_schema: "PyCapsule" = None, # type: ignore[name-defined] # noqa: F821, UP037 |
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 was your thinking behind this reformatting?
|
When I test, I don't find any issue with I'm a bit reluctant to tell everyone to change their code in this way - not everyone uses type checkers, and of that subset, only a third use pyright. A simpler alternative for pyright might actually be iterating over Others may disagree with me, but if you really think there is a problem here, I'd actually be more interested in deprecating Line 1447 in 76f04b4
|
Pillow/src/PIL/Image.py
Line 1436 in 5122c83
list(im.getdata())for e.g. printing data. This works fine in production but type checkers (such aspyright) do not like it asImagingCoredoes not expose an__iter__. This adds that so that the type checking is happy.Notes
__iter__returnsAnybut I do not know the codebase well enough to know if there are variations in what types it might return.Anyis sufficient to fix the type checker for my usecase