From 798ee700feab687100b841d49eb49f50b5a2a504 Mon Sep 17 00:00:00 2001 From: Robert Halter Date: Fri, 20 Mar 2026 18:35:54 +0100 Subject: [PATCH] fix bug 1 : Bug: `NameError: name 'Response' is not defined` when starting server --- src/apiup/server.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/apiup/server.py b/src/apiup/server.py index 60ae73d..4c16508 100644 --- a/src/apiup/server.py +++ b/src/apiup/server.py @@ -10,14 +10,19 @@ from apiup.mock import extract_mock_response from apiup.spec import Route +try: + from litestar import Litestar + from litestar.handlers import HTTPRouteHandler + from litestar.response import Response as LitestarResponse +except ImportError: # pragma: no cover + Litestar = None # type: ignore[assignment,misc] + HTTPRouteHandler = None # type: ignore[assignment,misc] + LitestarResponse = None # type: ignore[assignment] + def build_mock_app(routes: list[Route], spec: dict[str, Any]) -> Any: """Return a Litestar ASGI app with one handler per spec route.""" - try: - from litestar import Litestar - from litestar.handlers import HTTPRouteHandler - from litestar.response import Response - except ImportError: + if Litestar is None: print( "ERROR: litestar not installed.\nRun: pip install 'litestar[standard]'", file=sys.stderr, @@ -33,9 +38,10 @@ def build_mock_app(routes: list[Route], spec: dict[str, Any]) -> Any: _body = body _status = status_code - # Litestar uses the same {param} syntax as OpenAPI — no conversion needed. - async def _handler(b: Any = _body, s: int = _status) -> Response: # noqa: B023 - return Response(content=b, status_code=s, media_type="application/json") + # Return type is Any — avoids NameError when Litestar inspects + # the handler signature in a scope where Response is not global. + async def _handler(b: Any = _body, s: int = _status) -> Any: # noqa: B023 + return LitestarResponse(content=b, status_code=s, media_type="application/json") handler = HTTPRouteHandler( path=_path,