简体   繁体   中英

How to throw types from Depends for mypy to Fastapi?

I have this exxxampe code.

def get_test(q: str) -> str:
    return 'str str str' + q


@router_user.put(
    "/", response_model=schemas.UpdateUserRequest, tags=["User and Authentication"]
)
async def update_user(
        data: schemas.UpdateUserRequest,
        q: str = Depends(logic.get_test),
        db: AsyncSession = Depends(get_db),
        user: models.User = Depends(logic.get_curr_user_by_token),
):
    print(q)

when checking it with mypy, I always get an error


app/api/user/router.py:74: error: Incompatible default for argument "q" (default has type "Depends", argument has type "str")  [assignment]

why??? and how do I fix it?

types are the worst thing that could be dragged into python.

Looking at your code it looks like your get_curr_user_by_token returns models.User . Depends itself doesn't change the type of result, so you just use the one that is returned by thing inside Depends .

q: models.User = Depends(logic.get_curr_user_by_token),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM