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

Python nested lists and recursion problem
Problems PUTting binary data to Django
How to use long integers in Python to build a range?
Should I use Lex or a home-brewed solution to parse a formula?
Launching default application for given type of file, OS X
Python CGI script IOError Broken Pipe
Django, grouping query items
Regular expression works normally, but fails when placed in an XML schema
too many threads due to synch communication
How can I specify a relative path in a Python logging config file?
Python “draw() must be called with Label instance as first argument (got _WindowMetaclass instance instead)”
Writing to a file in Python inserts null bytes
Disable logging during manage.py test?
turbogears request/user object in templates and request context
Django date filter to output “am” or “A.M.”
Schedule Python Script - Windows 7

Categories

HOME
websphere
drupal-7
raspbian
bing
laravel-5.2
javamail
orientation
i2c
subquery
uiactivityviewcontroller
powershell-v3.0
wicket
rfid
uiscrollview
tree-traversal
google-people
primes
device-detection
koa
esql
myob
csh
switching
bus-error
fetch-api
owl-api
cgal
dpi
dd-wrt
websauna
strophe
clockwork
ipfw
receipt
qtablewidget
transient
von-neumann
honeysql
windows-10-iot-core
pox
meta-raspberrypi
rule
ddms
microdata
crop
timesten
applepayjs
rpostgresql
forerunnerdb
pagefile
butterknife
quintus
cdk
addin-express
laravel-query-builder
c64
easing
kendo-combobox
multilingual-app-toolkit
gmsmapview
concur
approval-tests
optionbutton
jericho-html-parser
urn
twython
digits
lines-of-code
isml
sysfs
consensus
client-side-validation
django-1.6
symfony-2.6
left-recursion
gyroscope-framework
funq
emma
wss
kraken.js
reserved-words
flash-cc
sanitization
cilk-plus
onselect
stagefright
soa-suite
qss
awesomeprint
pascals-triangle
qt4.6
process-monitor
transitive-closure-table
lync-server-2010
moq-3
radchart
getusermedia
visual-web-gui
supersized
electronic-signature
printing-web-page
audiostreamer
winverifytrust
multibyte-functions
longjmp
configurable
self-reference
visual-studio-dbpro

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App