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 #470: Prevent error AsyncToSync.main_wrap() got multiple values for argument '<kwarg>' #471

Merged
merged 5 commits into from
Sep 30, 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: 9 additions & 6 deletions asgiref/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,12 @@ def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
sys.exc_info(),
task_context,
context,
*args,
**kwargs,
# prepare an awaitable which can be passed as is to self.main_wrap,
# so that `args` and `kwargs` don't need to be
# destructured when passed to self.main_wrap
# (which is required by `ParamSpec`)
# as that may cause overlapping arguments
self.awaitable(*args, **kwargs),
)

if not (self.main_event_loop and self.main_event_loop.is_running()):
Expand Down Expand Up @@ -302,8 +306,7 @@ async def main_wrap(
exc_info: "OptExcInfo",
task_context: "Optional[List[asyncio.Task[Any]]]",
context: List[contextvars.Context],
*args: _P.args,
**kwargs: _P.kwargs,
awaitable: Union[Coroutine[Any, Any, _R], Awaitable[_R]],
) -> None:
"""
Wraps the awaitable with something that puts the result into the
Expand All @@ -326,9 +329,9 @@ async def main_wrap(
try:
raise exc_info[1]
except BaseException:
result = await self.awaitable(*args, **kwargs)
result = await awaitable
else:
result = await self.awaitable(*args, **kwargs)
result = await awaitable
except BaseException as e:
call_result.set_exception(e)
else:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import warnings
from concurrent.futures import ThreadPoolExecutor
from functools import wraps
from typing import Any
from unittest import TestCase

import pytest
Expand Down Expand Up @@ -1174,3 +1175,36 @@ async def async_task():
assert task_complete

assert task_executed


def test_async_to_sync_overlapping_kwargs() -> None:
"""
Tests that AsyncToSync correctly passes through kwargs to the wrapped function,
particularly in the case where the wrapped function uses same names for the parameters
as the wrapper.
"""

@async_to_sync
async def test_function(**kwargs: Any) -> None:
assert kwargs

# AsyncToSync.main_wrap has a param named `context`.
# So we pass the same argument here to test for the error
# "AsyncToSync.main_wrap() got multiple values for argument '<kwarg>'"
test_function(context=1)


@pytest.mark.asyncio
async def test_sync_to_async_overlapping_kwargs() -> None:
"""
Tests that SyncToAsync correctly passes through kwargs to the wrapped function,
particularly in the case where the wrapped function uses same names for the parameters
as the wrapper.
"""

@sync_to_async
def test_function(**kwargs: Any) -> None:
assert kwargs

# SyncToAsync.__call__.loop.run_in_executor has a param named `task_context`.
await test_function(task_context=1)
Loading