Edit getdata to recommend iterating in a type-correct way#9261
Edit getdata to recommend iterating in a type-correct way#9261DominicOram wants to merge 1 commit intopython-pillow:mainfrom
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? |
>>> 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.
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.
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 |
|
Any thoughts? |
👍 |
|
I've created #9292 to suggest deprecating |
|
Sorry, lost track of this. If the preferred solution is to deprecate |
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