-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Add support for using context managers as a dependency #10353
base: master
Are you sure you want to change the base?
Conversation
Interesting... |
I think I've figured it out. Both sync and async context manager functions are called synchronously, so both of them can be resolved with I've updated my branch with the above logic implemented. Here's an example app: https://gist.github.com/notpushkin/cfa23e4ab8a73ef9505e2b6e6b2dca5c Still need to figure out parameters and caching, and add some tests. |
Parameters (sub-dependencies) seem to work! Caching shouldn't be an issue, too (given “generator” dependencies are handled in the same way). Any ideas how to test this? |
Cool feature, thanks for tackling this.
Clever, I wasn't aware that there is a window where it's possible to disambiguate between functions and context managers without it already having been necessary to use context-manager-specifc syntax. |
I'm back! Let's get this sorted out. I've rebased on |
I've added a couple basic tests and updated docs to mention that you can now use context managers directly. I'm not sure how to run doc tests though: when I run |
📝 Docs preview for commit d93c701 at: https://683c36c4.fastapitiangolo.pages.dev |
📝 Docs preview for commit 736f544 at: https://87d81350.fastapitiangolo.pages.dev |
I've added an ignore rule for the docs, since Coverage is now failing at 99%, but the lines that aren't covered are in a completely different part of the codebase: https://smokeshow.helpmanual.io/4z4c2y1i1r2j495l4a0i/d_6de190ce15502a91_models_py.html |
@iudeen Would you happen to know what are next steps to get this reviewed and (hopefully!) merged? Sorry if you're not the right person to ask, I just see that you're a Contributor on this repo so I was hoping that you can point me in the right direction :-) |
📝 Docs preview for commit b9bb157 at: https://e99a602f.fastapitiangolo.pages.dev |
@tiangolo Would love to hear your thoughts about this! Let me know if you'd like any changes made (apart from resolving the conflicts). If this isn't something you think we should merge in at this time, please do let me know, too. 😊 |
Background
Currently, you can't pass a context manager to
Depends()
, as it would treat it as a function. This means that if you want to use a function both as a dependency in FastAPI and a context manager (e. g. in scripts or Alembic migrations etc), you need to define both versions:Unfortunately, as @a-recknagel pointed out in #8955 (comment), it's impossible to check whether given function is a context manager (defined with a decorator from contextlib) or not:
It is however possible to check that after we call the function:
This PR implements that check (for async context managers only for now – sync version would require doing some threadpool magic I'm still looking into :-)