python type_error ai_generated true

类型错误:缺少1个必需的位置参数:'db'

TypeError: missing 1 required positional argument: 'db'

ID: python/fastapi-depends-missing-parameter

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-06-25首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

依赖函数期望一个参数,但调用方或依赖注入系统未提供。

English

A dependency function expects a parameter that is not provided by the caller or the dependency injection system.

generic

解决方案

  1. 95% 成功率 Ensure the dependency is properly injected using Depends
    from fastapi import Depends
    async def get_db():
        return db
    @app.get('/items')
    async def read_items(db=Depends(get_db)):
  2. 85% 成功率 Define the dependency as a class with __call__
    class DBSession:
        def __call__(self):
            return db
    @app.get('/items')
    async def read_items(db=Depends(DBSession())):

无效尝试

常见但无效的做法:

  1. Adding a default value to the parameter 50% 失败

    May not solve the underlying issue if the dependency is required.

  2. Removing the dependency from the route 80% 失败

    Breaks the functionality that requires the dependency.