thesilican
thesilican

Reputation: 763

Type hinting for python 3 curses wrapper

For curses programming in Python 3, it is useful to use the curses.wrapper function to encapsulate the curses program, to handle errors and setup.

import curses

def main(stdscr):
    # Curses program here
    pass

curses.wrapper(main)

But how do you add type hinting to the stdscr object? Adding type hinting would allow IDEs to provide better intellisense to the stdscr object and show available methods and properties. (I'm using Visual Studio Code btw)

The following code:

import curses

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

curses.wrapper(main)

...shows that type(stdscr) is <class '_curses.window'>

However this does not work:

import curses
import _curses

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

curses.wrapper(main)

And I'm not exactly sure what else to try.

Upvotes: 4

Views: 851

Answers (1)

AbdAlWahab Fanr
AbdAlWahab Fanr

Reputation: 90

You can solve this problem by writing the window type curses._CursesWindows in quotes Like this:

import curses

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

curses.wrapper(main)

Upvotes: 6

Related Questions