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

Fix typing issue with deorating sync function #10

Merged
merged 2 commits into from
May 23, 2024
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
15 changes: 12 additions & 3 deletions aioclock/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import asyncio
import sys
from functools import wraps
from typing import Any, Callable
from typing import Any, Awaitable, Callable, TypeVar

if sys.version_info < (3, 10):
from typing_extensions import ParamSpec
else:
from typing import ParamSpec

from fast_depends import inject

Expand All @@ -10,6 +16,9 @@
from aioclock.types import Triggers
from aioclock.utils import flatten_chain

T = TypeVar("T")
P = ParamSpec("P")


class AioClock:
"""
Expand Down Expand Up @@ -117,9 +126,9 @@ async def main():
```
"""

def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
def decorator(func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]:
@wraps(func)
async def wrapper(*args, **kwargs) -> Any:
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
return await func(*args, **kwargs)

self._app_tasks.append(
Expand Down
15 changes: 12 additions & 3 deletions aioclock/group.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import asyncio
import sys
from functools import wraps
from typing import Any, Callable, Union
from typing import Awaitable, Callable, TypeVar, Union

if sys.version_info < (3, 10):
from typing_extensions import ParamSpec
else:
from typing import ParamSpec

from fast_depends import inject

from aioclock.provider import get_provider
from aioclock.task import Task
from aioclock.triggers import BaseTrigger

T = TypeVar("T")
P = ParamSpec("P")


class Group:
def __init__(self, *, tasks: Union[list[Task], None] = None):
Expand Down Expand Up @@ -50,9 +59,9 @@ async def send_email():
```
"""

def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
def decorator(func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]:
@wraps(func)
async def wrapper(*args, **kwargs) -> Any:
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
return await func(*args, **kwargs)

self._tasks.append(
Expand Down
4 changes: 2 additions & 2 deletions aioclock/task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Any, Callable
from typing import Any, Awaitable, Callable

from aioclock.logger import logger
from aioclock.triggers import BaseTrigger
Expand All @@ -12,7 +12,7 @@ class Task:
This is internally used, when you decorate your function with `aioclock.task`.
"""

func: Callable[..., Any]
func: Callable[..., Awaitable[Any]]
"""Decorated function that will be run by AioClock."""

trigger: BaseTrigger
Expand Down
Loading