7

Example code:

async def test():
  """
  >>> await test()
  hello world
  """
  print("hello world")

Attempting to run this with doctests results in SyntaxError: 'await' outside function.

Bharel
  • 23,672
  • 5
  • 40
  • 80

1 Answers1

6

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")
Bharel
  • 23,672
  • 5
  • 40
  • 80