-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix tkinter ImageSpec #11721 #13049
Fix tkinter ImageSpec #11721 #13049
Conversation
This comment has been minimized.
This comment has been minimized.
@srittau Do i need to comment Any here too? If so what would be the appropriate comment. |
This comment has been minimized.
This comment has been minimized.
Thanks for working on this! Tkinter is more tricky than you'd expect :) After thinking about this again, I'm pretty sure that using import tkinter
import PIL.Image
root = tkinter.Tk()
img = PIL.Image.open("foo.png")
label = tkinter.Label(image=img) The problem is that you forgot to wrap the PIL image in a import tkinter
import PIL.Image
import PIL.ImageTk
root = tkinter.Tk()
img = PIL.Image.open("foo.png")
img_tk = PIL.ImageTk.PhotoImage(img)
label = tkinter.Label(root, image=img_tk) To make type checkers complain about the wrong code, let's use the following protocol: class _ImageLike(Protocol):
def width(self) -> int: ...
def height(self) -> int: ... This matches tkinter's image classes (
Do you want to change this PR, or do you want me to make a new PR with the protocol? |
Apparently this has been a protocol before, but it was changed in #9733. There I said:
And now that PIL ships its own type stubs, that is a good reason to prefer a protocol over fake classes that don't really work anymore. |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
@Akuli i will do it. Just pushed the fix. Also do i need to squash my commits, or in this repo you follow the CPython workflow were commits are squashed on merge? |
Your changes look good. Thanks!
I don't think we have a good way to ensure that PIL is installed while running tkinter tests. So I installed PIL and mypy locally and ran mypy over the code from my previous comment. Your branch works great :)
The CI runs
You don't need to squash, we use "Squash and merge" :) |
Fixes #11721