[QUESTION] Dependency Injection - Inject child class
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Description
I there a way to specify to inject a child class instead of the class declared as a dependency ?
In my example I want to specify that all my endpoints must be authenticated, but I don’t want to have to specify how the authentication must be done at the endpoint declaration level.
I would like to be able to specify that when Auth
is indicated as a dependency then BasicAuth
must be injected.
This way changing the authentication medium would need to change a single configuration line and not all the endpoints.
Example Code
from starlette.requests import Request
class Auth:
"""
Base class that all authenticator will extend
"""
def __init__(self, request: Request):
self.perform_authentication(request)
def perform_authentication(self, request: Request):
raise NotImplementedError()
class BasicAuth(Auth):
def perform_authentication(self, request: Request):
# Perform auth based on the HTTP Basic header
# I want my endpoint to be authenticated but I don't care about the way here
@app.get("/items/", dependencies=[Depends(Auth))
async def read_items():
pass
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.67
Python Version
3.10
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Dependency Injection in child class - Stack Overflow
I was under the impression that if you register the class to be constructed, all the classes you want to pass in, then...
Read more >Handling Dependency Injection in Inherited Classes
In this post I have described a simple method for managing dependency injection in inherited classes, by creating a dependency aggregate class ......
Read more >3 Exciting Methods for Dependency Injection With Inheritance ...
Dependency injection is a way to inject dependencies into a class for use in its methods. With dependency injection, the class becomes loosely...
Read more >When extending a class, use an injected object of that class
That's how dependency injection work. The problem in your second snippet is that the child should not extend but implement the parent interface....
Read more >Dependency injection in Android
Classes often require references to other classes. For example, a Car class might need a reference to an Engine class. These required classes...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is a normal use case for DI. What you want is to declare a dependency on an interface/abstraction and then bind a concrete implementation for deployment or testing. I think #3641 takes care of this use case since you can
.bind(Auth, BasicAuth)
Yes this is typically what I would like ! From what I understood it is not possible as of now, but I will watch closely your PR 😃