Skip to content

Make tkinter.Event equivalent to tkinter.Event[tkinter.Misc] #14053

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

Merged
merged 1 commit into from
May 13, 2025

Conversation

Akuli
Copy link
Collaborator

@Akuli Akuli commented May 13, 2025

A tkinter.Event represents something happening in a widget. The event's widget field is basically the widget that caused it. The somewhat misleadingly named Misc class means "any widget".

When writing an event handler function, you can specify the type of event.widget by using e.g. event: tkinter.Event[tkinter.Label]. Before this PR, it is required with strict type checker settings, and you get Any if you don't use it. With this PR, it is assumed to be Misc. This is suitable for event handlers that don't use the .widget field.

For example:

import tkinter

def handle_event(event: tkinter.Event) -> None:
    reveal_type(event.widget)

Mypy output before:

a.py:3: error: Missing type parameters for generic type "Event"  [type-arg]
a.py:4: note: Revealed type is "Any"
Found 1 error in 1 file (checked 1 source file)

Mypy ouput with this PR:

a.py:4: note: Revealed type is "tkinter.Misc"
Success: no issues found in 1 source file

The only reason why I didn't use default=Misc initially is that the default parameter didn't exist at the time.

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit 9589e36 into python:main May 13, 2025
64 checks passed
mmingyu pushed a commit to mmingyu/typeshed that referenced this pull request May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants