6

I'm trying to figure out what to put in my 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'>. This doesn't seem like it will work with Type hinting as it has a space in it. The expected result would be WindowObject listed in the documentation. I can't find a path to WindowObject in the curses module itself. EDIT: The documentation is incorrect here.

How do I write main with accurate type annotation?

Technoloft
  • 622
  • 4
  • 18
  • 2
    Just don't. `WindowObject` is a documentation error; the name of the window object type is undocumented and subject to change, and I don't think the window object type is even available as a module attribute. – user2357112 May 15 '17 at 20:42
  • Absolutamente right @user2357112, type() method always tells the type object, no matter what – developer_hatch May 15 '17 at 20:56
  • This is an odd case though. I haven't seen a type() with a space in it. How does the Typing library handle these? I'm not saying I NEED to do it but this is a curious case. – Technoloft May 16 '17 at 04:08

2 Answers2

3

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

Currently, the stubs default to typing curses.wrapper as:

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

...which, in turn, is equivalent to:

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

So, that means that there really is no suitable type to assign to your main function's parameter at the moment, apart from Any.

That said, if you're up for it, you might be able to contribute some stubs to complete the curses module yourself! It doesn't seem like the Window object is that terribly complex and should hopefully be relatively straightforward to type.

The main complication might be hammering out where exactly the 'Window' object should be imported from, if it doesn't exist within the curses module itself. You might perhaps want to stick the 'Windows' object within the typing module itself, just like typing.re.Pattern and typing.re.Match.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
1

Your problem is that the type you spect is just not the real type of the object, the method type() always tells you the type correctly, so by sure the doc. is wrong.

developer_hatch
  • 15,898
  • 3
  • 42
  • 75