diff options
author | Selwin Ong <selwin.ong@gmail.com> | 2023-05-15 07:34:27 +0700 |
---|---|---|
committer | Selwin Ong <selwin.ong@gmail.com> | 2023-05-15 07:34:27 +0700 |
commit | 0bfa0e3fd87ec31cb33c110cb6595dae2722ffde (patch) | |
tree | cc48a5680376965a1eb7344ca5c99a223b7e7ead | |
parent | a250f65b2c0285bb4f5a6b094d2bc016c0937a66 (diff) | |
download | rq-reliable-queue.tar.gz |
Minor typing fixesreliable-queue
-rw-r--r-- | rq/decorators.py | 6 | ||||
-rw-r--r-- | rq/job.py | 2 | ||||
-rw-r--r-- | tests/test_decorator.py | 41 |
3 files changed, 28 insertions, 21 deletions
diff --git a/rq/decorators.py b/rq/decorators.py index 2bf46e8..7d45e5f 100644 --- a/rq/decorators.py +++ b/rq/decorators.py @@ -1,5 +1,5 @@ from functools import wraps -from typing import TYPE_CHECKING, Callable, Dict, Optional, List, Any, Union +from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Type, Union if TYPE_CHECKING: from redis import Redis @@ -20,9 +20,9 @@ class job: # noqa timeout: Optional[int] = None, result_ttl: int = DEFAULT_RESULT_TTL, ttl: Optional[int] = None, - queue_class: Optional['Queue'] = None, + queue_class: Optional[Type['Queue']] = None, depends_on: Optional[List[Any]] = None, - at_front: Optional[bool] = None, + at_front: bool = False, meta: Optional[Dict[Any, Any]] = None, description: Optional[str] = None, failure_ttl: Optional[int] = None, @@ -619,7 +619,7 @@ class Job: self.worker_name: Optional[str] = None self._status = None self._dependency_ids: List[str] = [] - self.meta: Optional[Dict] = {} + self.meta: Dict = {} self.serializer = resolve_serializer(serializer) self.retries_left: Optional[int] = None self.retry_intervals: Optional[List[int]] = None diff --git a/tests/test_decorator.py b/tests/test_decorator.py index fb945e5..69ddde1 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -11,13 +11,11 @@ from tests.fixtures import decorated_job class TestDecorator(RQTestCase): - def setUp(self): super().setUp() def test_decorator_preserves_functionality(self): - """Ensure that a decorated function's functionality is still preserved. - """ + """Ensure that a decorated function's functionality is still preserved.""" self.assertEqual(decorated_job(1, 2), 3) def test_decorator_adds_delay_attr(self): @@ -34,9 +32,11 @@ class TestDecorator(RQTestCase): """Ensure that passing in queue name to the decorator puts the job in the right queue. """ + @job(queue='queue_name') def hello(): return 'Hi' + result = hello.delay() self.assertEqual(result.origin, 'queue_name') @@ -51,12 +51,12 @@ class TestDecorator(RQTestCase): @job('default', result_ttl=10) def hello(): return 'Why hello' + result = hello.delay() self.assertEqual(result.result_ttl, 10) def test_decorator_accepts_ttl_as_argument(self): - """Ensure that passing in ttl to the decorator sets the ttl on the job - """ + """Ensure that passing in ttl to the decorator sets the ttl on the job""" # Ensure default result = decorated_job.delay(1, 2) self.assertEqual(result.ttl, None) @@ -64,12 +64,12 @@ class TestDecorator(RQTestCase): @job('default', ttl=30) def hello(): return 'Hello' + result = hello.delay() self.assertEqual(result.ttl, 30) def test_decorator_accepts_meta_as_argument(self): - """Ensure that passing in meta to the decorator sets the meta on the job - """ + """Ensure that passing in meta to the decorator sets the meta on the job""" # Ensure default result = decorated_job.delay(1, 2) self.assertEqual(result.meta, {}) @@ -82,6 +82,7 @@ class TestDecorator(RQTestCase): @job('default', meta=test_meta) def hello(): return 'Hello' + result = hello.delay() self.assertEqual(result.meta, test_meta) @@ -153,16 +154,19 @@ class TestDecorator(RQTestCase): """Ensure that passing in on_failure function to the decorator sets the correct on_failure function on the job. """ + # Only functions and builtins are supported as callback @job('default', on_failure=Job.fetch) def foo(): return 'Foo' + with self.assertRaises(ValueError): result = foo.delay() @job('default', on_failure=print) def hello(): return 'Hello' + result = hello.delay() result_job = Job.fetch(id=result.id, connection=self.testconn) self.assertEqual(result_job.failure_callback, print) @@ -171,23 +175,26 @@ class TestDecorator(RQTestCase): """Ensure that passing in on_failure function to the decorator sets the correct on_success function on the job. """ + # Only functions and builtins are supported as callback @job('default', on_failure=Job.fetch) def foo(): return 'Foo' + with self.assertRaises(ValueError): result = foo.delay() @job('default', on_success=print) def hello(): return 'Hello' + result = hello.delay() result_job = Job.fetch(id=result.id, connection=self.testconn) self.assertEqual(result_job.success_callback, print) @mock.patch('rq.queue.resolve_connection') def test_decorator_connection_laziness(self, resolve_connection): - """Ensure that job decorator resolve connection in `lazy` way """ + """Ensure that job decorator resolve connection in `lazy` way""" resolve_connection.return_value = Redis() @@ -207,12 +214,11 @@ class TestDecorator(RQTestCase): def test_decorator_custom_queue_class(self): """Ensure that a custom queue class can be passed to the job decorator""" + class CustomQueue(Queue): pass - CustomQueue.enqueue_call = mock.MagicMock( - spec=lambda *args, **kwargs: None, - name='enqueue_call' - ) + + CustomQueue.enqueue_call = mock.MagicMock(spec=lambda *args, **kwargs: None, name='enqueue_call') custom_decorator = job(queue='default', queue_class=CustomQueue) self.assertIs(custom_decorator.queue_class, CustomQueue) @@ -226,12 +232,11 @@ class TestDecorator(RQTestCase): def test_decorate_custom_queue(self): """Ensure that a custom queue instance can be passed to the job decorator""" + class CustomQueue(Queue): pass - CustomQueue.enqueue_call = mock.MagicMock( - spec=lambda *args, **kwargs: None, - name='enqueue_call' - ) + + CustomQueue.enqueue_call = mock.MagicMock(spec=lambda *args, **kwargs: None, name='enqueue_call') queue = CustomQueue() @job(queue=queue) @@ -252,11 +257,12 @@ class TestDecorator(RQTestCase): @job('default', failure_ttl=10) def hello(): return 'Why hello' + result = hello.delay() self.assertEqual(result.failure_ttl, 10) def test_decorator_custom_retry(self): - """ Ensure that passing in retry to the decorator sets the + """Ensure that passing in retry to the decorator sets the retry on the job """ # Ensure default @@ -267,6 +273,7 @@ class TestDecorator(RQTestCase): @job('default', retry=Retry(3, [2])) def hello(): return 'Why hello' + result = hello.delay() self.assertEqual(result.retries_left, 3) self.assertEqual(result.retry_intervals, [2]) |