Skip to content
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
2 changes: 1 addition & 1 deletion python/fluxqueue/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__all__ = ["FluxQueue"]

from .core import FluxQueue
from .client import FluxQueue
25 changes: 20 additions & 5 deletions python/fluxqueue/core.py → python/fluxqueue/client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import inspect
from collections.abc import Callable
from collections.abc import Callable, Coroutine
from functools import wraps
from typing import Any, ParamSpec, TypeAlias, cast
from typing import Any, ParamSpec, cast, get_type_hints, overload

from ._core import FluxQueueCore
from .utils import get_task_name

P = ParamSpec("P")
TaskDecorator: TypeAlias = Callable[[Callable[P, Any]], Callable[P, Any]]


class FluxQueue:
Expand All @@ -30,7 +29,7 @@ def task(
name: str | None = None,
queue: str = "default",
max_retries: int = 3,
) -> TaskDecorator[P]:
):
"""
Mark a function as a FluxQueue task.

Expand All @@ -52,7 +51,23 @@ def task(
before treating it as dead.
"""

def decorator(func: Callable[P, Any]) -> Callable[P, Any]:
@overload
def decorator(func: Callable[P, None]) -> Callable[P, None]: ...

@overload
def decorator(
func: Callable[P, Coroutine[Any, Any, None]],
) -> Callable[P, Coroutine[Any, Any, None]]: ...

def decorator(
func: Callable[P, None | Coroutine[Any, Any, None]],
) -> Callable[P, None | Coroutine[Any, Any, None]]:
type_hints = get_type_hints(func)
return_type = type_hints.get("return")

if return_type and return_type is not type(None):
raise TypeError(f"Task function must return None, got {return_type}")

is_async = inspect.iscoroutinefunction(func)
task_name = get_task_name(func, name)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ async def async_hello(name: str):
assert b"Async George" in redis_result[0] # type: ignore

test_env.redis_client.flushdb()


def test_invalid_return_type(test_env: TestEnvFixture):
with pytest.raises(TypeError):

@test_env.fluxqueue.task() # type: ignore
def test_task() -> int:
return 5

test_task()