typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

asyncio.create_task doesn't accept coroutine like objects

Open kumaraditya303 opened this issue 2 years ago • 0 comments

The following code fails to typecheck with latest mypy but works just fine at runtime.

from typing import Any
import asyncio

class MyCoro:
    def __init__(self):
        async def coro() -> None:
            print("coro works!")

        self.coro = coro()

    def send(self, value: Any) -> None:
        return self.coro.send(value)

    def throw(self, exc: BaseException) -> None:
        return self.coro.throw(exc)

    def close(self) -> None:
        return self.coro.close()

    def __await__(self) -> Any:
        return self.coro.__await__()

async def main() -> None:
    coro = MyCoro()
    await asyncio.create_task(coro)


asyncio.run(main())

Error:

cpython on  isolate-decimal [?] via 🐍 v3.11.2 
❯ mypy main.py 
main.py:25: error: Argument 1 to "create_task" has incompatible type "MyCoro"; expected "Union[Generator[Any, None, <nothing>], Coroutine[Any, Any, <nothing>]]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

kumaraditya303 avatar Jun 01 '23 07:06 kumaraditya303