From eb347f3a1c13dc00449bf178a53c24f2ae838450 Mon Sep 17 00:00:00 2001 From: Daniel Enesi <137162024+DanielOnGitHub17@users.noreply.github.com> Date: Fri, 2 Jan 2026 04:18:17 -0600 Subject: [PATCH] Improve type annotations for `ttk.Widget` (#15189) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- stdlib/tkinter/ttk.pyi | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/stdlib/tkinter/ttk.pyi b/stdlib/tkinter/ttk.pyi index 1d72acd99..7143c7cce 100644 --- a/stdlib/tkinter/ttk.pyi +++ b/stdlib/tkinter/ttk.pyi @@ -2,10 +2,10 @@ import _tkinter import sys import tkinter from _typeshed import MaybeNone -from collections.abc import Callable, Iterable +from collections.abc import Callable, Iterable, Sequence from tkinter.font import _FontDescription -from typing import Any, Literal, TypedDict, overload, type_check_only -from typing_extensions import Never, TypeAlias, Unpack +from typing import Any, Literal, TypedDict, TypeVar, overload, type_check_only +from typing_extensions import Never, ParamSpec, TypeAlias, Unpack __all__ = [ "Button", @@ -54,6 +54,9 @@ _Statespec: TypeAlias = tuple[Unpack[tuple[str, ...]], Any] _ImageStatespec: TypeAlias = tuple[Unpack[tuple[str, ...]], tkinter._Image | str] _VsapiStatespec: TypeAlias = tuple[Unpack[tuple[str, ...]], int] +_P = ParamSpec("_P") +_T = TypeVar("_T") + class _Layout(TypedDict, total=False): side: Literal["left", "right", "top", "bottom"] sticky: str # consists of letters 'n', 's', 'w', 'e', may contain repeats, may be empty @@ -201,10 +204,15 @@ class Style: def theme_use(self, themename: None = None) -> str: ... class Widget(tkinter.Widget): - def __init__(self, master: tkinter.Misc | None, widgetname, kw=None) -> None: ... + def __init__(self, master: tkinter.Misc | None, widgetname: str | None, kw: dict[str, Any] | None = None) -> None: ... def identify(self, x: int, y: int) -> str: ... - def instate(self, statespec, callback=None, *args, **kw): ... - def state(self, statespec=None): ... + @overload + def instate(self, statespec: Sequence[str], callback: None = None) -> bool: ... + @overload + def instate( + self, statespec: Sequence[str], callback: Callable[_P, _T], *args: _P.args, **kw: _P.kwargs + ) -> Literal[False] | _T: ... + def state(self, statespec: Sequence[str] | None = None) -> tuple[str, ...]: ... class Button(Widget): def __init__(