[Python] FastAPI Depends not resolving dependency properly — "TypeError: 'Depends' object is...

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to build a simple FastAPI endpoint that uses dependency injection to get the current user. However, I'm getting this confusing error when hitting the route:

    TypeError: 'Depends' object is not callablet

    I am using this code:

    from fastapi import FastAPI, Depends, HTTPException
    from fastapi.security import OAuth2PasswordBearer

    app = FastAPI()
    oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

    def get_current_user(token: str = Depends(oauth2_scheme)):
    if token != "valid-token":
    raise HTTPException(status_code=401, detail="Invalid token")
    return {"username": "john_doe"}

    @app.get("/users/me")
    def read_users_me(current_user = Depends(get_current_user)):
    return current_user


    I expected this to work, but I’m getting the error as soon as I hit /users/me. I've double-checked that Depends is imported from fastapi.

    What am I doing wrong?

    Continue reading...

Compartilhe esta Página