Advanced Dependency Question for testing: Must it always be an instance of a class for dependency injection? #11690
Unanswered
Forsyth-Creations
asked this question in
Questions
Replies: 1 comment 2 replies
-
I'm a bit confused what you're asking. If you want to (i) use a class as a dependency and (ii) only use one instance of that class (i.e., never initialising more than one instance), you could use a singleton-pattern for your class to return the same class instance over and over. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
I'm trying to do dependency injection with an advanced dependency (so that my class can define access). Anyway, in the documentation here I see it needs to be an instance of the class to work. Is there a way to just override the class so I don't have to keep making instances?
Operating System
Linux
Operating System Details
Running Linux, but server is running in Docker with the python:3.10-slim image
FastAPI Version
0.111.0
Pydantic Version
2.7.3
Python Version
3.10
Additional Context
Testing uses a mongomock as another dependency for better testing
Beta Was this translation helpful? Give feedback.
All reactions