带有诅咒的 Python 类型提示

Tec*_*oft 5 python type-hinting python-curses python-3.5 python-3.6

我想弄清楚在这个函数顶部的类型注释中应该放什么。

我有以下简单的例子:

import curses

def main(stdscr):
    stdscr.clear()

    stdscr.addstr(2, 0, "What is the type of stdscr?")
    stdscr.addstr(5, 0, "It is: {}".format(type(stdscr)))

    stdscr.refresh()
    stdscr.getkey()

curses.wrapper(main)
Run Code Online (Sandbox Code Playgroud)

这返回<type '_curses.curses window'>. 这似乎不适用于类型提示,因为它有一个空格。 预期结果将WindowObject列在文档中。我在curses 模块本身中找不到WindowObject 的路径。编辑:此处的文档不正确。

如何使用准确的类型注释编写 main ?

Mic*_*x2a 3

不幸的是,curses 模块似乎没有在 typeshed 中完全键入。几个月前已经完成了一些初步工作,但 Windows 对象尚未添加。您可以在此处此处自行检查 Python 3“curses”存根。

目前,存根默认输入curses.wrapper为:

def wrapper(func, *args, **kwds): ...
Run Code Online (Sandbox Code Playgroud)

...这又相当于:

def wrapper(func: Callable[..., Any], *args: Any, **kwds: Any): ...
Run Code Online (Sandbox Code Playgroud)

main因此,这意味着除了 之外,目前确实没有合适的类型可以分配给函数的参数Any

也就是说,如果您愿意,您也许可以贡献一些存根来curses自己完成该模块!Window 对象似乎并没有那么复杂,并且应该能够相对简单地键入。

主要的复杂性可能是敲定“Window”对象应该从哪里导入,如果它不存在于curses模块本身中的话。您可能希望将“Windows”对象粘贴到typing模块本身中,就像typing.re.Pattern和 一样typing.re.Match