The dreaded forgotten await
The dreaded forgotten await
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?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: The dreaded forgotten await
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).
Or how about
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())
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())
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: The dreaded forgotten await
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.
-
- Posts: 969
- Joined: Sat Feb 03, 2018 7:02 pm
Re: The dreaded forgotten await
In Cpython you would get a warning:
But I have no idea how that could be implemented in micropython. It would be helpful.
Code: Select all
Warning (from warnings module):
File "<pyshell#8>", line 3
RuntimeWarning: coroutine 'test' was never awaited
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode