Page 1 of 1

The dreaded forgotten await

Posted: Fri May 22, 2020 1:44 am
by tve
I don't know how many times over the past month I spend a good 10 minutes troubleshooting some mysterious failure to find that I had forgotten the "await" when calling a coro. Usually I'll look at that line of code multiple times and see nothing wrong with it before the lightbulb finally goes on. Is there any way to get an error message somehow somewhere so I don't spend half of my life chasing down this silly mistake over and over again?

Re: The dreaded forgotten await

Posted: Fri May 22, 2020 5:23 am
by pythoncoder
YES. I've suffered from that one too. It is infuriating.

However detecting a syntax error may not be easy as there are things you can legitimately do to a coroutine apart from awaiting it, such as assigning it to a variable or passing it to a function. I did look at writing a script to detect these silently failing errors, but there are a lot of edge cases. The following run without error on MicroPython and CPython 3.8 (changing the import line).

Code: Select all

import uasyncio as asyncio

async def foo():
    print('start')
    g = asyncio.sleep(2)
    await g
    print('done')

asyncio.run(foo())
Or how about

Code: Select all

import uasyncio as asyncio

async def bar(coro, t):
    print('start')
    await coro(t)
    print('done')


async def foo():
    await bar(asyncio.sleep, 2)

asyncio.run(foo())

Re: The dreaded forgotten await

Posted: Fri May 22, 2020 7:20 am
by tve
I'm aware of the valid uses of coros without await... But at this point I'd be willing to have a checker that barfs unless there's an annotation of some form. In the end, having to add such annotations would be worth it if it saves the aggravations. At least to me.

Re: The dreaded forgotten await

Posted: Fri May 22, 2020 7:23 am
by kevinkk525
In Cpython you would get a warning:

Code: Select all

Warning (from warnings module):
  File "<pyshell#8>", line 3
RuntimeWarning: coroutine 'test' was never awaited
But I have no idea how that could be implemented in micropython. It would be helpful.