Message324398
This is not a bug in Python. The SyntaxError matches expected behavior. According to the Python grammar, you can't have "await" outside of a function. You have "await" at the globals scope which is not permitted. You may only "await" functions from within other functions.
Doctest is designed to emulate what you would type into the Python shell. Your doctest snippet does not work there either:
```
$ python3
Python 3.7.0 (default, Jun 28 2018, 05:55:06)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> async def hello_world():
... return "Hello, world!"
...
>>> await hello_world()
File "<stdin>", line 1
SyntaxError: 'await' outside function
```
To make your doctests work, use ``asyncio.run`` like so:
```
import asyncio
async def hello_world():
"""
Will greet the world with a friendly hello.
>>> asyncio.run(hello_world())
'hello world'
"""
return "hello world"
if __name__ == "__main__":
import doctest
doctest.testmod()
```
Stefan, you may get a quicker answer next time from a forum like StackOverflow.
Yury, it's probably a good idea to cover this case in your upcoming asyncio docs improvements. |
|
Date |
User |
Action |
Args |
2018-08-30 18:54:26 | grantjenks | set | recipients:
+ grantjenks, asvetlov, yselivanov, Stefan Tjarks |
2018-08-30 18:54:26 | grantjenks | set | messageid: <1535655266.28.0.56676864532.issue34445@psf.upfronthosting.co.za> |
2018-08-30 18:54:26 | grantjenks | link | issue34445 messages |
2018-08-30 18:54:26 | grantjenks | create | |
|