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

Checking if first item of a list of list exists in first item of another list of list
Multiple LED at different time
Can I use Flask and flask-restul to return JSON to API calls but HTML to browsers?
Keras input dimension
Adding a loop to “digital root” function in Python3
How to use a linear index to access a 2D array in Python
Tflearn Custom Objective function
Text Classification - Label Pre Process [closed]
how to get matched substring list in a json-type string with python?
Lambda Expressions: Returning Multiple Values
Using CUDA8 in theano
matplotlib.widgets Button doesn't work inside a class
fault in readline() or condition
How to set PYTHONSTARTUP script for Intellij/PyCharm
Learning Python the Hard Way (Third Edition) Exercise 11 Print statements not printed until after input is entered
Pandas read in chunks of data with variable number of rows?

Categories

HOME
arrays
laravel-5
sidekiq
dynamics-crm
jar
oauth
vb6
wsdl
performancecounter
webrtc
paramiko
facebook-graph-api
angular2-routing
jasmine
survival-analysis
h2
zend-framework2
sbt-assembly
rfid
data-analysis
mod-pagespeed
uisplitviewcontroller
titan
eclipselink
jconsole
rails-activerecord
scalaz7
header-files
jquery-waypoints
logarithm
activepython
maximo
thinking-sphinx
gzip
xor
m2e
data-extraction
forecasting
code-behind
equivalence
rule
wsadmin
cgo
react-native-router-flux
stereo-3d
rkt
recycle-bin
tuleap
gauss
ticker
photon-controller
search-box
jszip
qos
cjson
gitweb
multilingual-app-toolkit
approval-tests
loose-typing
plone-3.x
direct3d12
windows-vista
selendroid
ipojo
ibm-data-studio
gulp-livereload
adodb
musl
cpu-speed
livechat
jstat
suffix-array
initialization-vector
windows-mobile-6
va-list
sttwitter
dig
vmware-server
surveyor-gem
database-permissions
pascals-triangle
javascriptserializer
legacy-code
windows-phone-7-emulator
text-services-framework
jquery-selectbox
serp
associative
smooth
squeel
hibernate3-maven-plugin
msf
ajaxpro
hardware-infrastructure
self-reference

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