diff --git a/stdlib/@tests/test_cases/check_tkinter.py b/stdlib/@tests/test_cases/check_tkinter.py index eb344f3a234a..ef72f7a2b0f0 100644 --- a/stdlib/@tests/test_cases/check_tkinter.py +++ b/stdlib/@tests/test_cases/check_tkinter.py @@ -2,19 +2,17 @@ import sys import tkinter -import traceback import types from typing import Optional, Tuple from typing_extensions import assert_type - -def custom_handler(exc: type[BaseException], val: BaseException, tb: types.TracebackType | None) -> None: - print("oh no") +root = tkinter.Tk() +tkinter.Tk.report_callback_exception(root, TypeError, TypeError("no"), None) -root = tkinter.Tk() -root.report_callback_exception = traceback.print_exception -root.report_callback_exception = custom_handler +class App(tkinter.Tk): + def report_callback_exception(self, exc: type[BaseException], val: BaseException, tb: types.TracebackType | None) -> None: + print("oh no") def foo(x: int, y: str) -> None: diff --git a/stdlib/@tests/test_cases/check_tkinter_method_assign.py b/stdlib/@tests/test_cases/check_tkinter_method_assign.py new file mode 100644 index 000000000000..5fbd2a2d6c7b --- /dev/null +++ b/stdlib/@tests/test_cases/check_tkinter_method_assign.py @@ -0,0 +1,20 @@ +# mypy: disable-error-code="method-assign" +from __future__ import annotations + +import tkinter +import traceback +import types + + +def custom_handler(exc: type[BaseException], val: BaseException, tb: types.TracebackType | None) -> None: + print("oh no") + + +def custom_wrong_handler(exc: BaseException, val: BaseException, tb: types.TracebackType | None) -> None: + print("oh no") + + +root = tkinter.Tk() +root.report_callback_exception = traceback.print_exception +root.report_callback_exception = custom_handler +root.report_callback_exception = custom_wrong_handler # type: ignore diff --git a/stdlib/tkinter/__init__.pyi b/stdlib/tkinter/__init__.pyi index 9f1c346cdc19..ed55cf6de2b1 100644 --- a/stdlib/tkinter/__init__.pyi +++ b/stdlib/tkinter/__init__.pyi @@ -1063,7 +1063,8 @@ class Tk(Misc, Wm): config = configure def destroy(self) -> None: ... def readprofile(self, baseName: str, className: str) -> None: ... - report_callback_exception: Callable[[type[BaseException], BaseException, TracebackType | None], object] + def report_callback_exception(self, exc: type[BaseException], val: BaseException, tb: TracebackType | None, /) -> object: ... + # Tk has __getattr__ so that tk_instance.foo falls back to tk_instance.tk.foo # Please keep in sync with _tkinter.TkappType. # Some methods are intentionally missing because they are inherited from Misc instead.