Best way to lazily instantiate Settings #59
Unanswered
andersbogsnes
asked this question in
Q&A
Replies: 1 comment
-
Isn't that pretty much what @madjar wrote in #34 (comment) or am I missing something? Seems like another vote for #34? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In FastAPI, I generally adopt a lazy-loading pattern, starting from my Settings object (using the wonderful pydantic-settings), and then using Depends to pass my settings to my factory functions.
So I'd do something like:
And now my dependencies are nicely setup so that I can replace them at test time without having to worry about creating the necessary environment variables / .env file for testing purposes
I wanted to try out svcs to be able to have a single dependency being injected into my routes, since the function signature was getting really long. My question is, what's the best way to inject the Settings object down the line?
For async, there's an extra wrinkle, since the engine needs to have
dispose
be called, so I would need access to the engine object in the factory. For now I've got this:But like this I replace the settings object at test-time, and I'm back to having to set a bunch of ENVs or add a
.env
file.Any ideas on how to best handle this scenario?
Beta Was this translation helpful? Give feedback.
All reactions