python


Python Type hinting with curses


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?

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.

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 functions 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.


Related Links

How to handle > (>) symbol in Django forms
Process variables from one method to another inside one class
Text to dictionary doesn't work
Django: Python variable data as background image
Webscraping Beautiful Soup - Table ends prematurely? [duplicate]
Query msaccess with unicode fails
How to get the source code from ast in python
asyncio scheduling callback at specific time
Python Multiprocessing with Subprocess Flags fail to run
Ending program after a certain period of time
TKinter, Python - Creating TopLevel popups using iteration and then closing them again without killing root
ValueError: too many values to unpack (sin/cos graph with Python 2.7)
Can't print object, despite _str_ method in a class
code does not write anything in the file
Autosummary with toctree not creating documentation for methods
Read in year, DOY and microsecond data as datetime

Categories

HOME
erlang
payment-gateway
jar
checkbox
laravel-5.3
google-sheets-api
jscript
snap.svg
dataframe
codeblocks
eclipse-cdt
android-source
tee
data-analysis
rapidjson
redux-observable
grub2
uiautomator
kitematic
jquery-scrollify
sumo
switching
emv
dss
fetch-api
pcre
dd-wrt
twitter-bootstrap-2
pyscripter
preg-grep
hotmail
idl
data-extraction
npm-shrinkwrap
phpspreadsheet
viewstate
nunit-3.0
npm-publish
android-download-manager
flex4.5
dart-pub
service-fabric-stateful
g1gc
taglib
amd
fastq
encapsulation
dojox.grid.datagrid
automator
tiddlywiki
infinite-scroll
addin-express
adler32
master
galleriffic
audioqueue
multifile-uploader
dbscan
acm
android-recyclerview
android-cursoradapter
registrykey
uialertview
response-headers
virus
searchkick
ejabberd-saas
lexicographic
ogr2ogr
system.web
codeigniter-a3m
unison
svcutil.exe
winrt-httpclient
osx-snow-leopard
cilk-plus
stagefright
digital-design
fortran77
awesomeprint
prng
zend-framework-modules
marmalade-edk
lync-server-2010
crocodoc
css-tables
supersized
jquery-selectbox
cross-domain-policy
phpcrawl
nagle
adobe-contribute
writing





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android


MS Developer
developer works
python
ios
c
html
jquery


RDBMS discuss