Reputation: 26901
Example code:
async def test():
"""
>>> await test()
hello world
"""
print("hello world")
Attempting to run this with doctests results in SyntaxError: 'await' outside function
.
Upvotes: 9
Views: 913
Reputation: 26901
Doctest runs the code in the code outside any function. You can't use await expressions outside of an async function.
In order to run async functions, you can use asyncio.run()
like so:
import asyncio
async def test():
"""
>>> asyncio.run(test())
hello world
"""
print("hello world")
Upvotes: 7