python
type_error
ai_generated
true
类型错误:'Depends' 对象不可调用
TypeError: 'Depends' object is not callable
ID: python/fastapi-dependency-error-callable
80%修复率
85%置信度
0证据数
2025-02-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
错误地将Depends用作函数而不是依赖注入。
English
Using Depends incorrectly as a function instead of a dependency injection.
解决方案
-
95% 成功率 Use Depends as a parameter without calling it.
from fastapi import Depends async def read_items(db: Session = Depends(get_db)):
-
90% 成功率 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)):
无效尝试
常见但无效的做法:
-
Calling Depends() with parentheses inside a function parameter.
80% 失败
Depends should be used without calling it; it's a class.
-
Importing Depends from the wrong module.
60% 失败
Depends is from fastapi, not from fastapi.params.