Open cofin opened 1 month ago
I don't think we can reasonably support this without requiring to add some abstractions that make it rather useless as a feature. The issue here is that we cannot distinguish between a dependency that is meant to be used as a contextmanager, and a dependency that just happens to be a contextmanager.
E.g.
from sqlalchemy.orm import Session
def provide_session() -> Session:
session = get_session() # gets session from somewhere
return session
app = Litestar(depenencies={"session": provide_session})
this wouldn't work, because Session
is a context manager, and Litestar would try to call its __enter__
and __exit__
methods, causing unintended side-effects.
We could solve this by providing some wrapper that indicates that this dependency provider should be treated as a context manager, but then again you can achieve the same thing with
async def provide_connection() -> Union[PoolConnectionProxy, Connection]:
async with thing.provide_connection() as conn:
yield conn
Summary
We currently support generator based dependencies, but not context managers. For instance the following works:
But adding an context manager decorator like this:
produces
Basic Example
No response
Drawbacks and Impact
No response
Unresolved questions
No response