Skip to content

asyncio.create_task doesn't accept async_generator_asend objects #10185

Closed
@kumaraditya303

Description

@kumaraditya303

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

import asyncio
from typing import AsyncGenerator


async def func() -> AsyncGenerator[int, None]:
    yield 1


async def main() -> None:
    a = func()
    print(await asyncio.create_task(a.asend(None)))

asyncio.run(main())

Error:

mypy main.py
main.py:11: error: Argument 1 to "create_task" has incompatible type "Awaitable[int]"; expected "Union[Generator[Any, None, <nothing>], Coroutine[Any, Any, <nothing>]]"  [arg-type]
main.py:11: error: Argument 1 to "create_task" has incompatible type "Awaitable[int]"; expected "Union[Generator[Any, None, object], Coroutine[Any, Any, object]]"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

mypy version: mypy 1.0.0 (compiled: yes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions