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

Passing tensorflow placeholders as function parameters
Python(3.6)-How can I create a demo program that will create an object from the VotingMachine class I have created?
Python and Plotly offline: create (empty) graph and add traces with for-loop
Installing python on Mac not working
Extracting values from a dataframe column with dtype 'o'
Theano tensor type keeps changing and won't let me pass data
append items from shuffled list to a new list
Authenticate and Authorize for appfolder scope access with OneDrive Business Python SDK
Python logging across open source modules
How to transform nested strings in array to separated words?
Data to be read by humans in Python (large data sets)
How can I rename strings of indices?
what is wrong with this DP solution?
Recursion error in Python function
CLion external tools macro
tkinter error when copying contents from clipboard in Python

Categories

HOME
ember.js
ssas-2012
jar
rotation
freepascal
textwatcher
backup
youtube-livestreaming-api
computer-vision
docker-swarm
hana
is-empty
propertygrid
hyperledger-fabric
jrebel
actionscript-2
slim-3
dropbear
getorgchart
data-synchronization
typeahead
ida
jpeg2000
zend-framework-mvc
prestodb
cython
scalaz7
kitematic
samsung-mobile
continuous-deployment
guile
image-quality
frp
datadog
websauna
referenceerror
getjson
hawtio
mapguide
gettext
precedence
axis-labels
active-model-serializers
infusionsoft
android-preferences
sendgrid-templates
control-flow-graph
datalog
cppunit
amd
estimation
floor
pluck
tcserver
info
sqlexception
inject
cjson
typhoon
backstop.js
audioqueue
mongoose-populate
wcf-ria-services
visible
webkit2
tomcat5
boost-hana
image-editing
fps
scriptlet
asynccallback
fill
approval-tests
servlet-3.0
android-cursoradapter
app.xaml
debian-based
word-2013
mser
fragment-tab-host
system.web
android-looper
db4o
dynamic-proxy
angulartics
swrl
sharpmap
fraud-prevention
fortran77
vt100
gdataxml
transitive-closure-table
mozart-mvc
legacy-code
law-of-demeter
datacontract
smooth
hmacsha1
method-signature
writing

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