-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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_overrides
requires unnecessary arguments with Protocol/ABC in Swagger
#4118
Comments
👌 |
Is there any plans to fix this? Dependency injection feature is totally unusable because of this:
There is no way to use types as a dependencies, only possible solution is to create stub functions which looks very dirty. |
This library probably solves this problem |
It's not a quite great solution of this issue due to what this lib actually does. Patching |
would this also allow to override dependencies taking arguments? `@app.get("/hello/world") app.dependency_overrides[BaseRepo] = lambda: ConcreteRepo() #override even without giving an argument` |
Hey there! I see that in your examples Using things in dependencies is normally for when there's a reason to use them, because otherwise, it's simpler and more explicit to call the code directly in your function. I want to understand your use case to see why it would make sense to support this, to achieve what, and see if there's another way to do it. |
Hi, @tiangolo! Thank you for your response! Example with I believe, usage of DI using
I want to emphasize this:
In practice approach with using Maybe you will ask why it's better than something like the def get_db():
session = session_maker()
try:
yield session
finally:
session.close()
@app.get("/")
async def handler(db: Session = Depends(get_db)):
return {"message": "Hello World"} In this example, the handler knows about the factory of dependency and strongly tied to it(the module with handler must import it). |
But why is that? To re-quote what you wanted to emphasise:
Having a signature of I can see this making sense when you, on runtime, want to determine what implementation of It is an interesting idea though! |
Hello, I've found related issue #617 with the same problem but with a more complicated solution. With For instance this: from fastapi import FastAPI, Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
app = FastAPI()
engine = create_engine("sqlite://")
session_maker = sessionmaker()
def get_db_session():
session = session_maker()
try:
yield session
finally:
session.close()
@app.get("/")
async def root(session: Session = Depends(get_db_session)):
pass Can be written this way with import uvicorn
from fastapi import FastAPI, Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
def provide_session(session_maker: sessionmaker):
def inner():
session = session_maker()
try:
yield session
finally:
session.close()
return inner
def create_app():
app = FastAPI()
engine = create_engine("sqlite://", echo=True, echo_pool=True)
session_maker = sessionmaker(engine)
app.dependency_overrides[Session] = provide_session(session_maker)
return app
app = create_app()
@app.get("/")
async def root(session: Session = Depends(Session)):
print(session)
uvicorn.run(app) No global variables - fewer problems Another related issue: #4183 |
I'm having the same issue as @GLEF1X and I can confirm the fix in #4119 solves this issue. The above solution is how I supposed FastAPI would work:
@GLEF1X 's solution is rather elegant and I believe is similar to several dependency injection approaches in other languages. Roughly, I'm following a clean architecture / hexagonal architecture approach and only rely on interfaces (Protocol) for specifying dependencies. edit: sorry, I hit shift+enter too quickly |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.70.0
Python Version
3.9.7
Additional Context
No response
The text was updated successfully, but these errors were encountered: