Skip to content

Slightly improve annotation of tkinter.Tk#2498

Merged
JelleZijlstra merged 1 commit into
python:masterfrom
srittau:tkinter-fix
Oct 2, 2018
Merged

Slightly improve annotation of tkinter.Tk#2498
JelleZijlstra merged 1 commit into
python:masterfrom
srittau:tkinter-fix

Conversation

@srittau
Copy link
Copy Markdown
Collaborator

@srittau srittau commented Oct 1, 2018

Closes: #1767

@JelleZijlstra JelleZijlstra merged commit 2d3edbd into python:master Oct 2, 2018
@srittau srittau deleted the tkinter-fix branch October 16, 2018 01:30
yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this pull request Jan 23, 2019
@Jesse205
Copy link
Copy Markdown

Jesse205 commented May 27, 2026

The type annotation for report_callback_exception in this PR will cause regression issues:

Issue 1

In a Tk subclass, after overriding report_callback_exception, pyright reports an error.

from tkinter import Tk
from types import TracebackType
from typing import override


class App(Tk):
    @override
    def report_callback_exception(
        self,
        exc_type: type[BaseException],
        exc_value: BaseException,
        exc_traceback: TracebackType | None,
    ):
        pass
(report-callback-exception) PS G:\programming\python\lab\report_callback_exception> pyright
g:\programming\python\lab\report_callback_exception\main.py
  g:\programming\python\lab\report_callback_exception\main.py:8:9 - error: Method "report_callback_exception" overrides class "Tk" in an incompatible manner
    Positional parameter count mismatch; base method has 3, but override has 4
    Parameter 2 type mismatch: base parameter is type "BaseException", override parameter is type "type[BaseException]"
    Parameter 3 type mismatch: base parameter is type "TracebackType | None", override parameter is type "BaseException"
      Type "BaseException" is not assignable to type "type[BaseException]"
      Type "TracebackType | None" is not assignable to type "BaseException"
        "TracebackType" is not assignable to "BaseException" (reportIncompatibleMethodOverride)
1 error, 0 warnings, 0 informations

Issue 2

Without manual assignment, but manually calling Tk.report_callback_exception, pyright does not report an error, yet this code cannot run at all.

from tkinter import Tk
import sys


def main():
    try:
        print(0 / 0)
    except Exception:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        assert exc_type is not None
        assert exc_value is not None
        Tk.report_callback_exception(exc_type, exc_value, exc_traceback)
(report-callback-exception) PS G:\programming\python\lab\report_callback_exception> & g:\programming\python\lab\report_callback_exception\.venv\Scripts\python.exe g:/programming/python/lab/report_callback_exception/main.py
Traceback (most recent call last):
  File "g:\programming\python\lab\report_callback_exception\main.py", line 7, in main
    0 / 0
    ~~^~~
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "g:\programming\python\lab\report_callback_exception\main.py", line 16, in <module>
    main()
    ~~~~^^
  File "g:\programming\python\lab\report_callback_exception\main.py", line 12, in main
    Tk.report_callback_exception(exc_type, exc_value, exc_traceback)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Tk.report_callback_exception() missing 1 required positional argument: 'tb'
(report-callback-exception) PS G:\programming\python\lab\report_callback_exception> pyright
0 errors, 0 warnings, 0 informations

srittau said in #1767 (comment):

"This looks more like a mypy problem to me. It is somehow expecting an additional self argument here."

If that is indeed the case, I think we should revert the type annotation for report_callback_exception and report the error to mypy instead, because the root cause is not here.

The above statement is irrelevant to this issue.

mypy's error when assigning to a method is intentional, therefore #1767 is not an issue.

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.

3 participants