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

unmerge text with PyPDF2
os.listdir throws Error 5 (IO Error) when in background
downloading error using nltk.download()
Two quicksort implementations, differing comparisons
Find all solutions of row reduced echelon matrix in pure python
Accessing slider value directly in variable
Turn a datetime into days in python [closed]
Creating and working with list of lists of lists: in Python [duplicate]
Listening for specific data whilst using threading
How to import shared libraries created with numba export using ctypes?
Passing vectors and params from Python to R functions
Managing Autogenerated Indexes
Get file path from askopenfilename function in Tkinter
How to read a json-dictionary type file with pandas?
mplot3d python incompatible dimensions error
Is it normal for pygtk library download with macports to take a long time and print 33000 lines to terminal?

Categories

HOME
ssas-2012
vb6
bing
visual-studio-2013
jasmine
formal-verification
deep-linking
playframework
hana
rfc
propertygrid
browserify
spring-cloud-contract
netflix-feign
cakephp-2.9
viber
line-api
titan
r-lavaan
scenebuilder
yeoman-generator
aptana
movies
mousewheel
flexboxgrid
kitematic
richfaces
memsql
spring-test
http-method
jflex
samsung-mobile
geopandas
onesignal
rhandsontable
version-numbering
edge-detection
chrome-native-messaging
reactiveui
websauna
pep8-assembly
roundcube
encase
ipfw
edb
fractals
pdflatex
google-drive-realtime-api
remote-server
google-guava-cache
color-scheme
office365connectors
jquery-multidatespicker
wcf-security
jupyter-console
android-maps-v2
lftp
fastq
lumen-5.3
vcf
defold
webdatagrid
dwarf
mbaas
jexl
fedora20
photography
hpcc
linkageerror
picking
asp.net-mvc-partialview
qt-linguist
cctray
plone-3.x
direct3d12
hyperthreading
manjaro
uptodate
pgagent
cg
log4cplus
outline
csplit
eclipse-classpath
argument-passing
planetary.js
cppdepend
android-looper
data-generation
mstsc
phpsh
git-filter-branch
radchart
symfony-2.0
applicationcontext
hashalgorithm
querystringparameter
coff
serp
out-of-browser
recordset
revert
graniteds
front-controller
inline-if
writing





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