From b8014715cacfebd0ef76305e76247304cfef7a0b Mon Sep 17 00:00:00 2001 From: artemkucher Date: Sat, 7 Feb 2026 19:25:04 +0500 Subject: [PATCH] perf: Remove is_async_callable check in request_response --- fastapi/routing.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index ea82ab14a..31f3530da 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -79,18 +79,15 @@ from starlette.websockets import WebSocket from typing_extensions import deprecated -# Copy of starlette.routing.request_response modified to include the -# dependencies' AsyncExitStack +# Copy of starlette.routing.request_response, modified to include the +# dependencies' AsyncExitStack and to remove support for non-awaitable +# functions. def request_response( - func: Callable[[Request], Awaitable[Response] | Response], + func: Callable[[Request], Awaitable[Response]], ) -> ASGIApp: """ - Takes a function or coroutine `func(request) -> response`, - and returns an ASGI application. + Takes a coroutine `func(request) -> response` and returns an ASGI application. """ - f: Callable[[Request], Awaitable[Response]] = ( - func if is_async_callable(func) else functools.partial(run_in_threadpool, func) # type:ignore - ) async def app(scope: Scope, receive: Receive, send: Send) -> None: request = Request(scope, receive, send) @@ -102,7 +99,7 @@ def request_response( scope["fastapi_inner_astack"] = request_stack async with AsyncExitStack() as function_stack: scope["fastapi_function_astack"] = function_stack - response = await f(request) + response = await func(request) await response(scope, receive, send) # Continues customization response_awaited = True