Skip to content

Commit

Permalink
Fix AsyncAndSyncPairDecorator.__init__()
Browse files Browse the repository at this point in the history
AsyncAndSyncPairDecorator wasn't forwarding asyncio_fn to the superclass. That caused an issue where asyncio_fn was not available when the sync_fn paremeter was provided together.
  • Loading branch information
SoulTch authored and dkang-quora committed Mar 26, 2024
1 parent 7296aba commit c61a61e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
18 changes: 12 additions & 6 deletions asynq/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,13 @@ def __call__(self, *args, **kwargs):
class AsyncAndSyncPairDecorator(AsyncDecorator):
binder_cls = AsyncAndSyncPairDecoratorBinder

def __init__(self, fn, cls, sync_fn, kwargs={}):
AsyncDecorator.__init__(self, fn, cls, kwargs)
def __init__(self, fn, cls, sync_fn, kwargs={}, asyncio_fn=None):
AsyncDecorator.__init__(self, fn, cls, kwargs, asyncio_fn)
self.sync_fn = sync_fn

def __call__(self, *args, **kwargs):
if is_asyncio_mode():
raise RuntimeError("asyncio mode does not support synchronous calls")
return self.sync_fn(*args, **kwargs)

def __get__(self, owner, cls):
Expand All @@ -253,7 +255,11 @@ def __get__(self, owner, cls):
if self.type in (staticmethod, classmethod):
fn = self.type(fn)
new_self = qcore.decorators.decorate(
AsyncAndSyncPairDecorator, self.task_cls, sync_fn, self.kwargs
AsyncAndSyncPairDecorator,
self.task_cls,
sync_fn,
self.kwargs,
self.asyncio_fn,
)(fn)
return AsyncDecorator.__get__(new_self, owner, cls)

Expand Down Expand Up @@ -315,9 +321,9 @@ def decorate(fn):
)(fn)
return decorated
else:
return qcore.decorators.decorate(AsyncAndSyncPairDecorator, cls, sync_fn)(
fn
)
return qcore.decorators.decorate(
AsyncAndSyncPairDecorator, cls, sync_fn, kwargs, asyncio_fn
)(fn)

return decorate

Expand Down
12 changes: 11 additions & 1 deletion asynq/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ def sync_fn():
return "sync_fn"


@asynq(sync_fn=sync_fn)
async def asyncio_fn():
return "asyncio_fn"


@asynq(sync_fn=sync_fn, asyncio_fn=asyncio_fn)
def async_fn():
return "async_fn"

Expand Down Expand Up @@ -239,6 +243,12 @@ def test_method_sync_fn():
assert_eq("async_method", instance.async_method.asynq().value())


def test_function_sync_asyncio_fn():
assert_eq("sync_fn", async_fn())
assert_eq("async_fn", async_fn.asynq().value())
assert_eq("asyncio_fn", asyncio.run(async_fn.asyncio()))


def test_pickling():
pickled = pickle.dumps(async_fn)
unpickled = pickle.loads(pickled)
Expand Down

0 comments on commit c61a61e

Please sign in to comment.