Tasks without saved reference can be garbage collected, and several functions in asyncio automatically converts coroutines into tasks - which means it's ~impossible to save a strong reference to those tasks. See e.g. https://docs.python.org/3/library/asyncio-task.html#asyncio.shield
The rule implementation would be fairly straightforward, just check for calls in the parameter list: asyncio.shield(anything()). This will give false alarms if anything is not a coroutine, but instead a sync function that creates a task, saves it globally, and returns it, but I suspect that's a minority of cases. We could save the names of any sync funcs to reduce the false alarm rate, but that wouldn't work when the functions are imported from other files.
We could also use type-checking to catch
a = my_coro()
asyncio.wait_for(a)
On py311+ asyncio.wait errors if directly passed coroutines.
Tasks without saved reference can be garbage collected, and several functions in asyncio automatically converts coroutines into tasks - which means it's ~impossible to save a strong reference to those tasks. See e.g. https://docs.python.org/3/library/asyncio-task.html#asyncio.shield
The rule implementation would be fairly straightforward, just check for calls in the parameter list:
asyncio.shield(anything())
. This will give false alarms ifanything
is not a coroutine, but instead a sync function that creates a task, saves it globally, and returns it, but I suspect that's a minority of cases. We could save the names of any sync funcs to reduce the false alarm rate, but that wouldn't work when the functions are imported from other files.We could also use type-checking to catch
On py311+
asyncio.wait
errors if directly passed coroutines.There appears to be movement in making the event loop save strong references, but that'll probably only affect py3.14/py3.15 or later https://github.com/python/cpython/pull/121264