Skip to content

Using async decorator uses type hints incompatable with with asyncio.run or asyncio.create_task #184

Open
@kalebo

Description

It would be nice to be able to use this with asyncio methods like run and create_task without needing to resort to escape hatches like typing.cast or #type: ignore. I'm not sure that is just having a result.as_coroutine_result or there's a better way.

An example case is the following:

from result import as_async_result
import random
import asyncio

@as_async_result(Exception)
async def example() -> str:
    if random.random() < 0.5:
        raise RuntimeError("Boom")
    return "made it"

task: str = asyncio.run( example() )
print(task)

Running mypy on that will produce the following error:

error: Argument 1 to "run" has incompatible type "Awaitable[Ok[str] | Err[Exception]]"; expected "Coroutine[Any, Any, Never]"

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions