Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy support #7

Open
Guibod opened this issue Jun 15, 2024 · 4 comments
Open

Mypy support #7

Guibod opened this issue Jun 15, 2024 · 4 comments

Comments

@Guibod
Copy link

Guibod commented Jun 15, 2024

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
Copy link
Author

Guibod commented Jun 15, 2024

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
Copy link
Author

Guibod commented Jun 15, 2024

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: python/mypy#9837

@Guibod
Copy link
Author

Guibod commented Jul 3, 2024

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

@MrNaif2018
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants