Skip to content

Commit a953e6a

Browse files
add typing for @job decorator (#669)
1 parent ec5cfef commit a953e6a

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

django_rq/decorators.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
11
from rq.decorators import job as _rq_job
2-
from typing import TYPE_CHECKING, Union
2+
from typing import Any, Callable, Optional, overload, Protocol, TYPE_CHECKING, TypeVar, Union
33

44
from django.conf import settings
55

66
from .queues import get_queue
77

88
if TYPE_CHECKING:
9+
from redis import Redis
910
from rq import Queue
11+
from typing_extensions import ParamSpec
1012

13+
P = ParamSpec('P')
14+
R = TypeVar('R', covariant=True)
1115

12-
def job(func_or_queue, connection=None, *args, **kwargs):
16+
class _JobFn(Protocol[P, R]):
17+
def delay(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
18+
def enqueue(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
19+
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
20+
21+
22+
@overload
23+
def job(func_or_queue: 'Callable[P, R]') -> '_JobFn[P, R]': ...
24+
25+
@overload
26+
def job(
27+
func_or_queue: Union['Queue', str],
28+
connection: Optional['Redis'] = None,
29+
*args: Any,
30+
**kwargs: Any,
31+
) -> Callable[['Callable[P, R]'], '_JobFn[P, R]']: ...
32+
33+
34+
def job(
35+
func_or_queue: Union['Callable[P, R]', 'Queue', str],
36+
connection: Optional['Redis'] = None,
37+
*args: Any,
38+
**kwargs: Any,
39+
) -> Union['_JobFn[P, R]', Callable[['Callable[P, R]'], '_JobFn[P, R]']]:
1340
"""
1441
The same as RQ's job decorator, but it automatically works out
1542
the ``connection`` argument from RQ_QUEUES.

0 commit comments

Comments
 (0)