universalasync icon indicating copy to clipboard operation
universalasync copied to clipboard

Mypy support

Open Guibod opened this issue 1 year ago • 3 comments

Hey,

Great job there, i’m wondering if you plan to support proper python typing ? I’m happy to provide very easily a sync interface to my library unfortunately, everything mypy related is broken in sync context.

If not, do you have any tips to override the typing ?

Guibod avatar Jun 15 '24 07:06 Guibod

As i’m tampering with the library i managed to create my own typed wrapper above async_to_sync_wraps. It’s rudimentary but it could help a lot of people. :)

from typing import (
    Any,
    AsyncGenerator,
    Callable,
    Coroutine,
    Generator,
    TypeVar,
    overload,
    Union,
)

import universalasync

T = TypeVar("T")


@overload
def synchronize(
    function: Callable[..., Coroutine[Any, Any, T]]
) -> Callable[..., Union[Coroutine[Any, Any, T], T]]: ...
@overload
def synchronize(
    function: Callable[..., AsyncGenerator[T, None]]
) -> Callable[..., Union[AsyncGenerator[T, None], Generator[T, None, None]]]: ...


def synchronize(
    f: Callable[..., Coroutine[Any, Any, T]],
) -> Callable[..., T]:
    return universalasync.async_to_sync_wraps(f)

Guibod avatar Jun 15 '24 08:06 Guibod

The problem is that later, mypy does not manage to infer the async or sync value from the call context.

There was a closed request for a way to handle the request context of the call (async or sync) in mypy but it was rejected: https://github.com/python/mypy/issues/9837

Guibod avatar Jun 15 '24 09:06 Guibod

I’d like to request a review of my matter, pretty please. Don’t give up on me. :)

Guibod avatar Jul 03 '24 09:07 Guibod

Hi! Really sorry for the delay I would really like to add some proper typing, but my knowledge of python typing is limited Do you have any kind of example code I can try to run mypy on?

MrNaif2018 avatar Dec 16 '24 13:12 MrNaif2018