Skip to content

tkinter.Event is typed as generic but implementation isn't subscriptable #12591

Closed as not planned

Description

@yowoda

Implementation can be found here: https://github.com/python/cpython/blob/aa905925e1a0120e59e4ffe0874fbd098bf47e63/Lib/tkinter/__init__.py#L217

The stub:

class Event(Generic[_W_co]):

Not sure if this is intended behaviour (I assume it isn't) but right now type checkers can only be satisfied if doing
from __future__ import annotations or putting tk.Event[...] in quotes

Activity

  1. srittau commented on Aug 25, 2024

    @srittau
    Collaborator

    This is a common issue with type stubs and intended. We prefer to use generics if it improves type checking even if the implementation is not generic.

  2. Akuli commented on Aug 25, 2024

    @Akuli
    Collaborator

    CPython maintainers (I think Guido) have mentioned making tkinter.Event generic at runtime, but they never did it. Maybe you could submit a PR there? :)

    The reason tkinter.Event is generic is that tkinter.Event[tkinter.Label] is an event coming from clicking (or otherwise interacting with) a tkinter.Label widget, and when you access event.widget, the type checker knows it is a label and acts accordingly.

  3. yowoda commented on Aug 25, 2024

    @yowoda
    Author

    Sure, once I'm free the next few days I can definitely submit a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions