Python type hinting with curses

I am trying to figure out what to add to the type annotation at the top of this function.

I have the following trivial example:

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)

      

This returns <type '_curses.curses window'>

. It doesn't look like it will work with a type hint as it has a space in it. The expected result will be WindowObject

indicated in the documentation . I cannot find the path to WindowObject in the curses module itself.
EDIT. The documentation is wrong here.

How do I write a master record with precise type annotation?

+3


source to share


2 answers


Unfortunately, the curses module does not appear to be fully printed in the file. There was some preliminary work done a few months ago , but the Windows object hasn't been added yet. You can check the Python 3 'curses' stubs for yourself here and here .

Currently, butts are typed by default curses.wrapper

as:

def wrapper(func, *args, **kwds): ...

      

... which in turn is equivalent to:



def wrapper(func: Callable[..., Any], *args: Any, **kwds: Any): ...

      

So this means that at the moment there is no suitable type to assign to your function parameter main

other than Any

.

However, if you do, you can add some stubs to complete the module curses

yourself! The Window Object seems to be a terribly complex and hopefully relatively simple type.

The main complication is that the Window object must be imported if it does not exist in the curses module itself. You might want to insert the object "Windows" in the module itself typing

as typing.re.Pattern

andtyping.re.Match

.

+2


source


Your problem is that the type you represent is simply not the real type of the object, the type of the method () always specifies the type correctly, so be sure to doc. wrong.



+1


source







All Articles