-
Notifications
You must be signed in to change notification settings - Fork 15
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
@dependency_definition doesn't work for generator functions with a Container parameter #179
Comments
This is an interesting issue. On the surface this is just a bug. Lagom has support for generators as a The possibly more subtle bug is when would you expect this ContextManager to exit? The lagom container itself has no concept of request lifetimes or anything like that so it currently would perform the Something like the following might work as a manual solution to this problem: @dependency_definition(container)
def _session_dependency(container: Container) -> ContextManagert[Session]:
read_only: bool = container[Request].scope["db_access"] == "READ_ONLY"
return get_db_session(read_only)
# Later in the request
def some_request(session_manager = deps. depends(ContextManagert[Session])):
with session_manager as session:
# do something with the session
pass |
Hmm that is indeed what I (perhaps naively) was expecting... Unfortunately I want the DB class FooRepository:
def __init__(self, session: Session):
self.session = session
def get_foo():
return self.session.query(Foo).one() I take it the same problem ( |
If the fastapi built in DI system supports generators being valid for the lifetime of a request then it should be possible to hook into it from lagom. I'll need to do a little reading. |
I've split this more complicated problem off as #180 |
One more issue to throw your way @meadsteve 😅
It seems
@dependency_definition
doesn't work for generator functions with thec: Container
parameter, e.g.:This gives the following error:
For comparison a simple generator function without the
c: Container
parameter works fine:I'm not sure if this is by design or not?
I may be being too ambitious here but I want to have each of my FastAPI endpoints determine (via a custom APIRoute +
Request.scope
attribute) which DB transaction / connection is used (read/write vs read-only). I suppose I could achieve this with two independent Lagom containers, but then I would have two of every dependency initialised...The text was updated successfully, but these errors were encountered: