This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author grantjenks
Recipients Stefan Tjarks, asvetlov, grantjenks, yselivanov
Date 2018-08-30.18:54:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1535655266.28.0.56676864532.issue34445@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2018-08-30 18:54:26grantjenkssetrecipients: + grantjenks, asvetlov, yselivanov, Stefan Tjarks
2018-08-30 18:54:26grantjenkssetmessageid: <1535655266.28.0.56676864532.issue34445@psf.upfronthosting.co.za>
2018-08-30 18:54:26grantjenkslinkissue34445 messages
2018-08-30 18:54:26grantjenkscreate