python
type_error
ai_generated
true
TypeError: 'Depends' object is not callable
ID: python/fastapi-dependency-error-callable
80%Fix Rate
85%Confidence
0Evidence
2025-02-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Using Depends incorrectly as a function instead of a dependency injection.
generic中文
错误地将Depends用作函数而不是依赖注入。
Workarounds
-
95% success Use Depends as a parameter without calling it.
from fastapi import Depends async def read_items(db: Session = Depends(get_db)):
-
90% success If using a callable dependency, define it as a function and pass it to Depends.
def common_parameters(q: str = None): return {'q': q} @app.get('/items') async def read_items(commons: dict = Depends(common_parameters)):
Dead Ends
Common approaches that don't work:
-
Calling Depends() with parentheses inside a function parameter.
80% fail
Depends should be used without calling it; it's a class.
-
Importing Depends from the wrong module.
60% fail
Depends is from fastapi, not from fastapi.params.