python type_error ai_generated true

TypeError: 'Depends' object is not callable

ID: python/fastapi-dependency-error-callable

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Using Depends incorrectly as a function instead of a dependency injection.

generic

中文

错误地将Depends用作函数而不是依赖注入。

Workarounds

  1. 95% success Use Depends as a parameter without calling it.
    from fastapi import Depends
    async def read_items(db: Session = Depends(get_db)):
  2. 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:

  1. Calling Depends() with parentheses inside a function parameter. 80% fail

    Depends should be used without calling it; it's a class.

  2. Importing Depends from the wrong module. 60% fail

    Depends is from fastapi, not from fastapi.params.