8000 Make `tkinter.Event` equivalent to `tkinter.Event[tkinter.Misc]` by Akuli · Pull Request #14053 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
srittau merged 1 commit intopython:mainfrom
Akuli:tkinter-event-default
May 13, 2025
Merged

Make tkinter.Event equivalent to tkinter.Event[tkinter.Misc]#14053
srittau merged 1 commit intopython:mainfrom
Akuli:tkinter-event-default

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.

@github-actions
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

0