我发现了一些困扰我的东西
VSCode的一个重要特性是在编写接受**kw
的tkinter
类时得到的类型提示。以下是一个例子:
现在,我正试图从Tk.Canvas()
类中创建一个子类,幸运的是,vscode已经完成了写下这一点的艰苦工作:
class MyCanvas(Tk.Canvas):
def __init__(self, master: Misc | None, cnf: dict[str, Any] | None, *, background: _Color, bd: _ScreenUnits, bg: _Color, border: _ScreenUnits, borderwidth: _ScreenUnits, closeenough: float, confine: bool, cursor: _Cursor, height: _ScreenUnits, highlightbackground: _Color, highlightcolor: _Color, highlightthickness: _ScreenUnits, insertbackground: _Color, insertborderwidth: _ScreenUnits, insertofftime: int, insertontime: int, insertwidth: _ScreenUnits, name: str, offset: Any, relief: _Relief, scrollregion: Tuple[_ScreenUnits, _ScreenUnits, _ScreenUnits, _ScreenUnits] | Tuple[()], selectbackground: _Color, selectborderwidth: _ScreenUnits, selectforeground: _Color, state: Literal["normal", "disabled"], takefocus: _TakeFocusValue, width: _ScreenUnits, xscrollcommand: _XYScrollCommand, xscrollincrement: _ScreenUnits, yscrollcommand: _XYScrollCommand, yscrollincrement: _ScreenUnits) -> None:
super().__init__(master=master, cnf=cnf, background=background, bd=bd, bg=bg, border=border, borderwidth=borderwidth, closeenough=closeenough, confine=confine, cursor=cursor, height=height, highlightbackground=highlightbackground, highlightcolor=highlightcolor, highlightthickness=highlightthickness, insertbackground=insertbackground, insertborderwidth=insertborderwidth, insertofftime=insertofftime, insertontime=insertontime, insertwidth=insertwidth, name=name, offset=offset, relief=relief, scrollregion=scrollregion, selectbackground=selectbackground, selectborderwidth=selectborderwidth, selectforeground=selectforeground, state=state, takefocus=takefocus, width=width, xscrollcommand=xscrollcommand, xscrollincrement=xscrollincrement, yscrollcommand=yscrollcommand, yscrollincrement=yscrollincrement)
但是这个定义是非常错误的:它将属性视为变量而不是**kw
,并引用未定义的类,如_Color
和_ScreenUnits
。所以我去了Tk.Canvas.__init__()
定义看看发生了什么,我发现:
def __init__(self, master=None, cnf={}, **kw):
"""Construct a canvas widget with the parent MASTER.
Valid resource names: background, bd, bg, borderwidth, closeenough,
confine, cursor, height, highlightbackground, highlightcolor,
highlightthickness, insertbackground, insertborderwidth,
insertofftime, insertontime, insertwidth, offset, relief,
scrollregion, selectbackground, selectborderwidth, selectforeground,
state, takefocus, width, xscrollcommand, xscrollincrement,
yscrollcommand, yscrollincrement."""
Widget.__init__(self, master, 'canvas', cnf, kw)
所以这里我很困惑,因为在类定义中没有一个类型提示。那么,我得到的这些“类型暗示”是从哪里来的呢?有没有办法让它出现在我的子类的**kw
中
VSCode对python标准库的类型提示来自Typeshed,标准库的存根文件存储库(请参见此处:How does VS Code get type hints?)。可以在here on github找到
Tk.Canvas
的存根。在该文件的顶部,您可以找到_Color
、_ScreenUnits
等的定义(我同意VSCode在自动完成中提供这些定义,而不告诉您它们的定义,甚至不告诉您这些“外来”类型来自何处,这似乎有点缺陷。)截至2021-08-28的存根文件第84-111行:
另外,请注意,函数定义中裸
*
后面的所有参数都是keyword-only parameters。因此,我不同意typeshed中针对Tk.canvas.__init__
的类型注释是错误的——在我看来,它们并没有将任何参数标记为位置参数,而它们应该仅标记为关键字相关问题 更多 >
编程相关推荐