Disable async functions as a app_factory #1595
-
Look at this code: async def make_server_app() -> FastAPI:
return FastAPI()
async def main() -> None:
server_app = make_servep_app()
config = uvicorn.Config(server_app, port=settings.APP_PORT)
server = uvicorn.Server(config)
await server.serve()
if __name__ == "__main__":
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main()) This is a simplified version of a program I'm working on. If you will try to run it - you will not encountered any errors: ^^/C/t/Contigo-Integration >>> PYTHONPATH=. poetry run python main/server.py (*master) 21:35:59
INFO: Started server process [1755017]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) But (!) any request to this API will fail: ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/dmitry/.cache/pypoetry/virtualenvs/contigo-integration-r0O_5Ty1-py3.9/lib/python3.9/site-p
ackages/uvicorn/protocols/http/httptools_impl.py", line 401, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/dmitry/.cache/pypoetry/virtualenvs/contigo-integration-r0O_5Ty1-py3.9/lib/python3.9/site-p
ackages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
TypeError: 'coroutine' object is not callable
INFO: 127.0.0.1:44782 - "GET /docs HTTP/1.1" 500 Internal Server Error I probably spend half an hour trying to find what's wrong here. There is no warnings from Uvicorn, there is no mypy errors.
app: Union["ASGIApplication", Callable, str], So app receives any Callable, which is obviously incorrect. My first proposal is to set here type
There you can see why it behaves as I described above. Instead of What do you think about those proposals ? I hope you will agree with me and in this case I will raise an issue and send a PR |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
PR welcome to improve type annotation. |
Beta Was this translation helpful? Give feedback.
PR welcome to improve type annotation.