Skip to content

Commit b77f994

Browse files
authored
Improve _tkinter (#6908)
1 parent 1a0e0a4 commit b77f994

File tree

6 files changed

+112
-70
lines changed

6 files changed

+112
-70
lines changed

pyrightconfig.stricter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"stdlib/distutils/command",
1111
"stdlib/lib2to3/refactor.pyi",
1212
"stdlib/sqlite3/dbapi2.pyi",
13+
"stdlib/_tkinter.pyi",
1314
"stdlib/tkinter",
1415
"stdlib/unittest/mock.pyi",
1516
"stdlib/xml/dom/NodeFilter.pyi",

stdlib/_tkinter.pyi

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ from typing_extensions import Literal, final
1717
# (<textindex object: '1.0'>, <textindex object: '2.0'>)
1818
@final
1919
class Tcl_Obj:
20-
string: str # str(tclobj) returns this
20+
string: str | bytes
2121
typename: str
22+
__hash__: None # type: ignore[assignment]
23+
def __eq__(self, other): ...
24+
def __ge__(self, other): ...
25+
def __gt__(self, other): ...
26+
def __le__(self, other): ...
27+
def __lt__(self, other): ...
28+
def __ne__(self, other): ...
2229

2330
class TclError(Exception): ...
2431

@@ -42,39 +49,40 @@ class TclError(Exception): ...
4249
@final
4350
class TkappType:
4451
# Please keep in sync with tkinter.Tk
52+
def adderrorinfo(self, __msg): ...
4553
def call(self, __command: Any, *args: Any) -> Any: ...
54+
def createcommand(self, __name, __func): ...
55+
if sys.platform != "win32":
56+
def createfilehandler(self, __file, __mask, __func): ...
57+
def deletefilehandler(self, __file): ...
58+
def createtimerhandler(self, __milliseconds, __func): ...
59+
def deletecommand(self, __name): ...
60+
def dooneevent(self, __flags: int = ...): ...
4661
def eval(self, __script: str) -> str: ...
47-
adderrorinfo: Any
48-
createcommand: Any
49-
createfilehandler: Any
50-
createtimerhandler: Any
51-
deletecommand: Any
52-
deletefilehandler: Any
53-
dooneevent: Any
54-
evalfile: Any
55-
exprboolean: Any
56-
exprdouble: Any
57-
exprlong: Any
58-
exprstring: Any
59-
getboolean: Any
60-
getdouble: Any
61-
getint: Any
62-
getvar: Any
63-
globalgetvar: Any
64-
globalsetvar: Any
65-
globalunsetvar: Any
66-
interpaddr: Any
67-
loadtk: Any
68-
mainloop: Any
69-
quit: Any
70-
record: Any
71-
setvar: Any
62+
def evalfile(self, __fileName): ...
63+
def exprboolean(self, __s): ...
64+
def exprdouble(self, __s): ...
65+
def exprlong(self, __s): ...
66+
def exprstring(self, __s): ...
67+
def getboolean(self, __arg): ...
68+
def getdouble(self, __arg): ...
69+
def getint(self, __arg): ...
70+
def getvar(self, *args, **kwargs): ...
71+
def globalgetvar(self, *args, **kwargs): ...
72+
def globalsetvar(self, *args, **kwargs): ...
73+
def globalunsetvar(self, *args, **kwargs): ...
74+
def interpaddr(self): ...
75+
def loadtk(self) -> None: ...
76+
def mainloop(self, __threshold: int = ...): ...
77+
def quit(self): ...
78+
def record(self, __script): ...
79+
def setvar(self, *ags, **kwargs): ...
7280
if sys.version_info < (3, 11):
73-
split: Any
74-
splitlist: Any
75-
unsetvar: Any
76-
wantobjects: Any
77-
willdispatch: Any
81+
def split(self, __arg): ...
82+
def splitlist(self, __arg): ...
83+
def unsetvar(self, *args, **kwargs): ...
84+
def wantobjects(self, *args, **kwargs): ...
85+
def willdispatch(self): ...
7886

7987
# These should be kept in sync with tkinter.tix constants, except ALL_EVENTS which doesn't match TCL_ALL_EVENTS
8088
ALL_EVENTS: Literal[-3]
@@ -91,9 +99,18 @@ WRITABLE: Literal[4]
9199
TCL_VERSION: str
92100
TK_VERSION: str
93101

94-
# TODO: figure out what these are (with e.g. help()) and get rid of Any
95-
TkttType: Any
96-
_flatten: Any
97-
create: Any
98-
getbusywaitinterval: Any
99-
setbusywaitinterval: Any
102+
class TkttType:
103+
def deletetimerhandler(self): ...
104+
105+
def create(
106+
__screenName: str | None = ...,
107+
__baseName: str | None = ...,
108+
__className: str = ...,
109+
__interactive: bool = ...,
110+
__wantobjects: bool = ...,
111+
__wantTk: bool = ...,
112+
__sync: bool = ...,
113+
__use: str | None = ...,
114+
): ...
115+
def getbusywaitinterval(): ...
116+
def setbusywaitinterval(__new_val): ...

stdlib/tkinter/__init__.pyi

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -610,44 +610,38 @@ class Tk(Misc, Wm):
610610
@overload
611611
def configure(self, cnf: str) -> tuple[str, str, str, Any, Any]: ...
612612
config = configure
613-
def loadtk(self) -> None: ... # differs from _tkinter.TkappType.loadtk
614613
def destroy(self) -> None: ...
615614
def readprofile(self, baseName: str, className: str) -> None: ...
616615
report_callback_exception: _ExceptionReportingCallback
617616
# Tk has __getattr__ so that tk_instance.foo falls back to tk_instance.tk.foo
618-
# Please keep in sync with _tkinter.TkappType
619-
call: Callable[..., Any]
620-
def eval(self, __code: str) -> str: ...
621-
adderrorinfo: Any
622-
createcommand: Any
623-
createfilehandler: Any
624-
createtimerhandler: Any
625-
deletecommand: Any
626-
deletefilehandler: Any
627-
dooneevent: Any
628-
evalfile: Any
629-
exprboolean: Any
630-
exprdouble: Any
631-
exprlong: Any
632-
exprstring: Any
633-
getboolean: Any
634-
getdouble: Any
635-
getint: Any
636-
getvar: Any
637-
globalgetvar: Any
638-
globalsetvar: Any
639-
globalunsetvar: Any
640-
interpaddr: Any
641-
mainloop: Any
642-
quit: Any
643-
record: Any
644-
setvar: Any
617+
# Please keep in sync with _tkinter.TkappType.
618+
# Some methods are intentionally missing because they are inherited from Misc instead.
619+
def adderrorinfo(self, __msg): ...
620+
def call(self, __command: Any, *args: Any) -> Any: ...
621+
def createcommand(self, __name, __func): ...
622+
if sys.platform != "win32":
623+
def createfilehandler(self, __file, __mask, __func): ...
624+
def deletefilehandler(self, __file): ...
625+
def createtimerhandler(self, __milliseconds, __func): ...
626+
def dooneevent(self, __flags: int = ...): ...
627+
def eval(self, __script: str) -> str: ...
628+
def evalfile(self, __fileName): ...
629+
def exprboolean(self, __s): ...
630+
def exprdouble(self, __s): ...
631+
def exprlong(self, __s): ...
632+
def exprstring(self, __s): ...
633+
def globalgetvar(self, *args, **kwargs): ...
634+
def globalsetvar(self, *args, **kwargs): ...
635+
def globalunsetvar(self, *args, **kwargs): ...
636+
def interpaddr(self): ...
637+
def loadtk(self) -> None: ...
638+
def record(self, __script): ...
645639
if sys.version_info < (3, 11):
646-
split: Any
647-
splitlist: Any
648-
unsetvar: Any
649-
wantobjects: Any
650-
willdispatch: Any
640+
def split(self, __arg): ...
641+
def splitlist(self, __arg): ...
642+
def unsetvar(self, *args, **kwargs): ...
643+
def wantobjects(self, *args, **kwargs): ...
644+
def willdispatch(self): ...
651645

652646
def Tcl(screenName: Any | None = ..., baseName: Any | None = ..., className: str = ..., useTk: bool = ...): ...
653647

tests/stubtest_allowlists/darwin.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ stat.FILE_ATTRIBUTE_[A-Z_]+
4343
(sqlite3(.dbapi2)?.Connection.enable_load_extension)?
4444
(sqlite3(.dbapi2)?.Connection.load_extension)?
4545

46+
# Methods that come from __getattr__() at runtime
47+
tkinter.Tk.createfilehandler
48+
tkinter.Tk.deletefilehandler
49+
4650
# Exists at runtime, but missing from stubs
4751
distutils.msvccompiler.MSVCCompiler.get_msvc_paths
4852
distutils.msvccompiler.MSVCCompiler.set_path_env_var

tests/stubtest_allowlists/linux.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ stat.FILE_ATTRIBUTE_[A-Z_]+
4747
(sqlite3(.dbapi2)?.Connection.enable_load_extension)?
4848
(sqlite3(.dbapi2)?.Connection.load_extension)?
4949

50+
# Methods that come from __getattr__() at runtime
51+
tkinter.Tk.createfilehandler
52+
tkinter.Tk.deletefilehandler
53+
5054
# Exists at runtime, but missing from stubs
5155
distutils.msvccompiler.MSVCCompiler.get_msvc_paths
5256
distutils.msvccompiler.MSVCCompiler.set_path_env_var

tests/stubtest_allowlists/py3_common.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,28 @@ tkinter.Place.forget
370370
tkinter.Place.place_slaves
371371
tkinter.Place.slaves
372372

373+
# Methods that come from __getattr__() at runtime
374+
tkinter.Tk.adderrorinfo
375+
tkinter.Tk.call
376+
tkinter.Tk.createcommand
377+
tkinter.Tk.createtimerhandler
378+
tkinter.Tk.dooneevent
379+
tkinter.Tk.evalfile
380+
tkinter.Tk.exprboolean
381+
tkinter.Tk.exprdouble
382+
tkinter.Tk.exprlong
383+
tkinter.Tk.exprstring
384+
tkinter.Tk.globalgetvar
385+
tkinter.Tk.globalsetvar
386+
tkinter.Tk.globalunsetvar
387+
tkinter.Tk.interpaddr
388+
tkinter.Tk.record
389+
tkinter.Tk.split
390+
tkinter.Tk.splitlist
391+
tkinter.Tk.unsetvar
392+
tkinter.Tk.wantobjects
393+
tkinter.Tk.willdispatch
394+
373395
# Undocumented and have a comment in the source code saying "State variables (don't mess with these)"
374396
wsgiref.handlers.BaseHandler.bytes_sent
375397
wsgiref.handlers.BaseHandler.headers

0 commit comments

Comments
 (0)