python 3 curses 包装器的类型提示

Type hinting for python 3 curses wrapper

对于Python中的curses programming 3、使用curses.wrapper函数封装curses程序,处理错误和设置很有用。

import curses

def main(stdscr):
    # Curses program here
    pass

curses.wrapper(main)

但是如何将 type hinting 添加到 stdscr 对象中呢?添加类型提示将允许 IDE 为 stdscr 对象提供更好的智能感知并显示可用的方法和属性。 (我正在使用 Visual Studio 代码 btw)

以下代码:

import curses

def main(stdscr):
    s = str(type(stdscr))
    stdscr.clear()
    stdscr.addstr(0,0,s)
    stdscr.refresh()
    stdscr.getkey()

curses.wrapper(main)

...显示 type(stdscr)<class '_curses.window'>

但是这不起作用:

import curses
import _curses

# This does not work:
def main(stdscr: _curses.window):
    # No intellisense provided for stdscr
    pass

curses.wrapper(main)

我不太确定还能尝试什么。

你可以通过在引号中写 window 类型 curses._CursesWindows 来解决这个问题 像这样:

import curses

def main(stdscr: 'curses._CursesWindow'):
    # now intellisense will provide completions :)
    pass

curses.wrapper(main)