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

DM-40638: Improve typing of run_with_asyncio #196

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/20230905_090226_rra_DM_40638a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Bug fixes

- Fix typing of the `safir.asyncio.run_with_asyncio` decorator so that it doesn't mask the type of the underlying function.
16 changes: 12 additions & 4 deletions src/safir/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
from datetime import timedelta
from functools import wraps
from types import EllipsisType
from typing import Any, Generic, TypeVar
from typing import Generic, ParamSpec, TypeVar

from .datetime import current_datetime

#: Parameter spec for functions decorated by `run_with_asyncio`.
P = ParamSpec("P")

#: Type variable for return type of decorated by `run_with_asyncio`.
F = TypeVar("F")

#: Type variable of objects being stored in `AsyncMultiQueue`.
T = TypeVar("T")

__all__ = [
"AsyncMultiQueue",
"AsyncMultiQueueError",
"F",
"P",
"run_with_asyncio",
"T",
]
Expand Down Expand Up @@ -187,8 +195,8 @@ def qsize(self) -> int:


def run_with_asyncio(
f: Callable[..., Coroutine[Any, Any, T]]
) -> Callable[..., T]:
f: Callable[P, Coroutine[None, None, T]]
) -> Callable[P, T]:
"""Run the decorated function with `asyncio.run`.

Intended to be used as a decorator around an async function that needs to
Expand Down Expand Up @@ -230,7 +238,7 @@ async def init() -> None:
"""

@wraps(f)
def wrapper(*args: Any, **kwargs: Any) -> T:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
return asyncio.run(f(*args, **kwargs))

return wrapper