Skip to content

fix: keep sync handlers sync under @inject - #48

Merged
lesnik512 merged 1 commit into
mainfrom
fix/inject-sync-handler
Oct 17, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
fix/inject-sync-handler

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Closes #46

inject always returned an async def wrapper. aiohttp checks inspect.iscoroutinefunction(handler) when a route is added, and only wraps a handler that is not a coroutine function in its own adapter. Under @inject it saw a coroutine function, awaited the wrapper, and the wrapper then awaited the Response returned by the sync body. The request failed with a 500 and TypeError: 'Response' object can't be awaited.

inject now returns an async wrapper for a coroutine function and a sync wrapper otherwise, the same split modern-di-starlette (#52) and modern-di-flask use. aiohttp sees a plain function, emits its usual "Bare functions are deprecated" warning, and wraps it exactly as it would the undecorated handler. Marker resolution is synchronous in modern-di, so both wrappers share one resolve helper.

The signature changes from Callable[..., Awaitable[T]] -> Callable[..., Awaitable[T]] to Callable[..., T] -> Callable[..., T], so the return type follows the input without overloads. aiohttp's own Handler type only admits async handlers, so registering a sync handler still fails type checking in user code, with or without @inject. The new test suppresses that one diagnostic.

web.View methods are unchanged. aiohttp awaits view methods directly, so a sync view method fails without @inject too.

Tests in tests/test_sync_handlers.py cover a sync handler served end to end (asserting aiohttp's deprecation warning fires, which shows aiohttp treats the decorated handler as sync), the wrapper kind for sync and async input, and the two error paths for sync handlers. Existing tests cover async handlers. The README now says @inject accepts sync handlers.

@lesnik512
lesnik512 merged commit 0a45725 into main Oct 17, 2026
12 checks passed
@lesnik512
lesnik512 deleted the fix/inject-sync-handler branch October 17, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@inject on a sync handler returns 500

1 participant