Skip to content

Commit ee1e94d

Browse files
authored
[tkinter] Annotate few methods (#15273)
1 parent 5e477a3 commit ee1e94d

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

stdlib/_tkinter.pyi

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
2+
from _typeshed import FileDescriptorLike, Incomplete
23
from collections.abc import Callable
3-
from typing import Any, ClassVar, Final, final
4+
from typing import Any, ClassVar, Final, Literal, final, overload
45
from typing_extensions import TypeAlias, deprecated
56

67
# _tkinter is meant to be only used internally by tkinter, but some tkinter
@@ -54,22 +55,23 @@ _TkinterTraceFunc: TypeAlias = Callable[[tuple[str, ...]], object]
5455
@final
5556
class TkappType:
5657
# Please keep in sync with tkinter.Tk
57-
def adderrorinfo(self, msg: str, /): ...
58+
def adderrorinfo(self, msg: str, /) -> None: ...
5859
def call(self, command: Any, /, *args: Any) -> Any: ...
59-
def createcommand(self, name: str, func, /): ...
60+
# TODO: Figure out what arguments the following `func` callbacks should accept
61+
def createcommand(self, name: str, func: Callable[..., object], /) -> None: ...
6062
if sys.platform != "win32":
61-
def createfilehandler(self, file, mask: int, func, /): ...
62-
def deletefilehandler(self, file, /) -> None: ...
63+
def createfilehandler(self, file: FileDescriptorLike, mask: int, func: Callable[..., object], /) -> None: ...
64+
def deletefilehandler(self, file: FileDescriptorLike, /) -> None: ...
6365

64-
def createtimerhandler(self, milliseconds: int, func, /): ...
65-
def deletecommand(self, name: str, /): ...
66-
def dooneevent(self, flags: int = 0, /): ...
66+
def createtimerhandler(self, milliseconds: int, func: Callable[..., object], /): ...
67+
def deletecommand(self, name: str, /) -> None: ...
68+
def dooneevent(self, flags: int = 0, /) -> int: ...
6769
def eval(self, script: str, /) -> str: ...
68-
def evalfile(self, fileName: str, /): ...
69-
def exprboolean(self, s: str, /): ...
70-
def exprdouble(self, s: str, /): ...
71-
def exprlong(self, s: str, /): ...
72-
def exprstring(self, s: str, /): ...
70+
def evalfile(self, fileName: str, /) -> str: ...
71+
def exprboolean(self, s: str, /) -> Literal[0, 1]: ...
72+
def exprdouble(self, s: str, /) -> float: ...
73+
def exprlong(self, s: str, /) -> int: ...
74+
def exprstring(self, s: str, /) -> str: ...
7375
def getboolean(self, arg, /) -> bool: ...
7476
def getdouble(self, arg, /) -> float: ...
7577
def getint(self, arg, /) -> int: ...
@@ -81,15 +83,23 @@ class TkappType:
8183
def loadtk(self) -> None: ...
8284
def mainloop(self, threshold: int = 0, /) -> None: ...
8385
def quit(self) -> None: ...
84-
def record(self, script: str, /): ...
86+
def record(self, script: str, /) -> str: ...
8587
def setvar(self, *ags, **kwargs): ...
8688
if sys.version_info < (3, 11):
8789
@deprecated("Deprecated since Python 3.9; removed in Python 3.11. Use `splitlist()` instead.")
8890
def split(self, arg, /): ...
8991

90-
def splitlist(self, arg, /): ...
92+
def splitlist(self, arg, /) -> tuple[Incomplete, ...]: ...
9193
def unsetvar(self, *args, **kwargs): ...
92-
def wantobjects(self, *args, **kwargs): ...
94+
if sys.version_info >= (3, 14):
95+
@overload
96+
def wantobjects(self) -> Literal[0, 1]: ...
97+
else:
98+
@overload
99+
def wantobjects(self) -> bool: ...
100+
101+
@overload
102+
def wantobjects(self, wantobjects: Literal[0, 1] | bool, /) -> None: ...
93103
def willdispatch(self) -> None: ...
94104
if sys.version_info >= (3, 12):
95105
def gettrace(self, /) -> _TkinterTraceFunc | None: ...
@@ -112,7 +122,7 @@ TK_VERSION: Final[str]
112122

113123
@final
114124
class TkttType:
115-
def deletetimerhandler(self): ...
125+
def deletetimerhandler(self) -> None: ...
116126

117127
if sys.version_info >= (3, 13):
118128
def create(
@@ -125,7 +135,7 @@ if sys.version_info >= (3, 13):
125135
sync: bool = False,
126136
use: str | None = None,
127137
/,
128-
): ...
138+
) -> TkappType: ...
129139

130140
else:
131141
def create(
@@ -138,7 +148,7 @@ else:
138148
sync: bool = False,
139149
use: str | None = None,
140150
/,
141-
): ...
151+
) -> TkappType: ...
142152

143153
def getbusywaitinterval() -> int: ...
144154
def setbusywaitinterval(new_val: int, /) -> None: ...

stdlib/tkinter/__init__.pyi

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _tkinter
22
import sys
3-
from _typeshed import Incomplete, MaybeNone, StrOrBytesPath
3+
from _typeshed import FileDescriptorLike, Incomplete, MaybeNone, StrOrBytesPath
44
from collections.abc import Callable, Iterable, Mapping, Sequence
55
from tkinter.constants import *
66
from tkinter.font import _FontDescription
@@ -1005,34 +1005,43 @@ class Tk(Misc, Wm):
10051005
# Tk has __getattr__ so that tk_instance.foo falls back to tk_instance.tk.foo
10061006
# Please keep in sync with _tkinter.TkappType.
10071007
# Some methods are intentionally missing because they are inherited from Misc instead.
1008-
def adderrorinfo(self, msg: str, /): ...
1008+
def adderrorinfo(self, msg: str, /) -> None: ...
10091009
def call(self, command: Any, /, *args: Any) -> Any: ...
1010-
def createcommand(self, name: str, func, /): ...
1010+
# TODO: Figure out what arguments the following `func` callbacks should accept
1011+
def createcommand(self, name: str, func: Callable[..., object], /) -> None: ...
10111012
if sys.platform != "win32":
1012-
def createfilehandler(self, file, mask: int, func, /): ...
1013-
def deletefilehandler(self, file, /) -> None: ...
1013+
def createfilehandler(self, file: FileDescriptorLike, mask: int, func: Callable[..., object], /) -> None: ...
1014+
def deletefilehandler(self, file: FileDescriptorLike, /) -> None: ...
10141015

1015-
def createtimerhandler(self, milliseconds: int, func, /): ...
1016-
def dooneevent(self, flags: int = 0, /): ...
1016+
def createtimerhandler(self, milliseconds: int, func: Callable[..., object], /): ...
1017+
def dooneevent(self, flags: int = 0, /) -> int: ...
10171018
def eval(self, script: str, /) -> str: ...
1018-
def evalfile(self, fileName: str, /): ...
1019-
def exprboolean(self, s: str, /): ...
1020-
def exprdouble(self, s: str, /): ...
1021-
def exprlong(self, s: str, /): ...
1022-
def exprstring(self, s: str, /): ...
1019+
def evalfile(self, fileName: str, /) -> str: ...
1020+
def exprboolean(self, s: str, /) -> Literal[0, 1]: ...
1021+
def exprdouble(self, s: str, /) -> float: ...
1022+
def exprlong(self, s: str, /) -> int: ...
1023+
def exprstring(self, s: str, /) -> str: ...
10231024
def globalgetvar(self, *args, **kwargs): ...
10241025
def globalsetvar(self, *args, **kwargs): ...
10251026
def globalunsetvar(self, *args, **kwargs): ...
10261027
def interpaddr(self) -> int: ...
10271028
def loadtk(self) -> None: ...
1028-
def record(self, script: str, /): ...
1029+
def record(self, script: str, /) -> str: ...
10291030
if sys.version_info < (3, 11):
10301031
@deprecated("Deprecated since Python 3.9; removed in Python 3.11. Use `splitlist()` instead.")
10311032
def split(self, arg, /): ...
10321033

1033-
def splitlist(self, arg, /): ...
1034+
def splitlist(self, arg, /) -> tuple[Incomplete, ...]: ...
10341035
def unsetvar(self, *args, **kwargs): ...
1035-
def wantobjects(self, *args, **kwargs): ...
1036+
if sys.version_info >= (3, 14):
1037+
@overload
1038+
def wantobjects(self) -> Literal[0, 1]: ...
1039+
else:
1040+
@overload
1041+
def wantobjects(self) -> bool: ...
1042+
1043+
@overload
1044+
def wantobjects(self, wantobjects: Literal[0, 1] | bool, /) -> None: ...
10361045
def willdispatch(self) -> None: ...
10371046

10381047
def Tcl(screenName: str | None = None, baseName: str | None = None, className: str = "Tk", useTk: bool = False) -> Tk: ...

0 commit comments

Comments
 (0)