basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

false negative `reportUnusedCoroutine` when assigned to `_`

Open KotlinIsland opened this issue 5 months ago • 5 comments

Description

Code sample in basedpyright playground

async def f():
    await f()
    f() # not awaited
    _ = f() # no error

KotlinIsland avatar Oct 03 '25 02:10 KotlinIsland

i would think assigning it to _ indicates that you're explicitly ignoring the coroutine, just like with reportUnusedCallResult.

the eslint equivalent rule @typescript-eslint/no-floating-promises has a similar feature where you can use the void keyword to explicitly ignore the promise.

DetachHead avatar Oct 03 '25 03:10 DetachHead

it's a runtime crash to not await a coroutine

from asyncio import run


async def f():
    f2()


async def f2(): pass


run(f())

KotlinIsland avatar Oct 04 '25 01:10 KotlinIsland

its a RuntimeWarning not a crash:

/home/me/projects/test/asdf.py:6: RuntimeWarning: coroutine 'f' was never awaited
  f()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

but on second thought it probably does make sense to report an error here, since the assignment to _ is intended to explicitly ignore the result. but in the case of a coroutine with a return value, you would still want an error that you aren't awaiting the result before discarding it

DetachHead avatar Oct 04 '25 02:10 DetachHead

yeah, exactly. it should be

_ = await f()

KotlinIsland avatar Oct 04 '25 02:10 KotlinIsland

in an ideal world we would track which values are awaited or not at the type level, just like unassigned variables

KotlinIsland avatar Oct 04 '25 02:10 KotlinIsland