'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.
Solution 1:[1]
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),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kosciej16 |
