VSCode对tkinter.Canvas的类型提示来自哪里?

2024-06-28 19:58:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现了一些困扰我的东西

VSCode的一个重要特性是在编写接受**kwtkinter类时得到的类型提示。以下是一个例子:

vscode showing useful information about the **kw variable in Tk.Canvas()

现在,我正试图从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


Tags: masternoneinitcursorbdcolorcnfbg
1条回答
网友
1楼 · 发布于 2024-06-28 19:58:08

VSCode对python标准库的类型提示来自Typeshed,标准库的存根文件存储库(请参见此处:How does VS Code get type hints?)。可以在here on github找到Tk.Canvas的存根。在该文件的顶部,您可以找到_Color_ScreenUnits等的定义(我同意VSCode在自动完成中提供这些定义,而不告诉您它们的定义,甚至不告诉您这些“外来”类型来自何处,这似乎有点缺陷。)

截至2021-08-28的存根文件第84-111行:

_T = TypeVar("_T")
_TkinterSequence = Union[List[_T], Tuple[_T, ...]]
_TkinterSequence2D = Union[List[List[_T]], List[Tuple[_T, ...]], Tuple[List[_T], ...], Tuple[Tuple[_T, ...], ...]]

# Some widgets have an option named -compound that accepts different values
# than the _Compound defined here. Many other options have similar things.
_Anchor = Literal["nw", "n", "ne", "w", "center", "e", "sw", "s", "se"]  # manual page: Tk_GetAnchor
_Bitmap = str  # manual page: Tk_GetBitmap
_ButtonCommand = Union[str, Callable[[], Any]]  # return value is returned from Button.invoke()
_CanvasItemId = int
_Color = str  # typically '#rrggbb', '#rgb' or color names.
_Compound = Literal["top", "left", "center", "right", "bottom", "none"]  # -compound in manual page named 'options'
_Cursor = Union[str, Tuple[str], Tuple[str, str], Tuple[str, str, str], Tuple[str, str, str, str]]  # manual page: Tk_GetCursor
_EntryValidateCommand = Union[
    Callable[[], bool], str, _TkinterSequence[str]
]  # example when it's sequence:  entry['invalidcommand'] = [entry.register(print), '%P']
_ImageSpec = Union[_Image, str]  # str can be from e.g. tkinter.image_names()
_Padding = Union[
    _ScreenUnits,
    Tuple[_ScreenUnits],
    Tuple[_ScreenUnits, _ScreenUnits],
    Tuple[_ScreenUnits, _ScreenUnits, _ScreenUnits],
    Tuple[_ScreenUnits, _ScreenUnits, _ScreenUnits, _ScreenUnits],
]
_Relief = Literal["raised", "sunken", "flat", "ridge", "solid", "groove"]  # manual page: Tk_GetRelief
_ScreenUnits = Union[str, float]  # manual page: Tk_GetPixels
_XYScrollCommand = Union[str, Callable[[float, float], Any]]  # -xscrollcommand and -yscrollcommand in 'options' manual page
_TakeFocusValue = Union[int, Literal[""], Callable[[str], Optional[bool]]]  # -takefocus in manual page named 'options'

另外,请注意,函数定义中裸*后面的所有参数都是keyword-only parameters。因此,我不同意typeshed中针对Tk.canvas.__init__的类型注释是错误的——在我看来,它们并没有将任何参数标记为位置参数,而它们应该仅标记为关键字

相关问题 更多 >