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

Issues with latest fastapi connection example app (lifespan, missing type) #1361

Open
inktrap opened this issue Jun 20, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@inktrap
Copy link

inktrap commented Jun 20, 2024

Describe the bug

The latest ormar docs about connecting while using fastapi have two issues for me:

  • AsyncIterator is not imported from typing, but that is an easy fix
  • the bigger issue is that lifespan needs to be a decorator that handles the fastapi app object, not the ormar config:
ERROR:    Traceback (most recent call last):
  File "/home/foo/bar/api/.venv/lib/python3.11/site-packages/starlette/routing.py", line 732, in lifespan
    async with self.lifespan_context(app) as maybe_state:
TypeError: 'function' object does not support the asynchronous context manager protocol

I fixed this by returning the decorator with the config:

def get_lifespan(config):
    @asynccontextmanager
    async def lifespan(_: FastAPI) -> AsyncIterator[None]:
        if not config.database.is_connected:
            await config.database.connect()

        yield

        if config.database.is_connected:
            await config.database.disconnect()
    return lifespan


app = FastAPI(lifespan=get_lifespan(base_ormar_config))

And it works so far.

I'll just skip the other questions because it is a fairly clear documentation-only issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant