Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error when implementing additional responses #252

Closed
ryanditjia opened this issue Aug 21, 2020 · 3 comments
Closed

Type error when implementing additional responses #252

ryanditjia opened this issue Aug 21, 2020 · 3 comments

Comments

@ryanditjia
Copy link

Following the example from the additional responses guide:

class Message(BaseModel):
    message: str


responses = {
    401: {"model": Message}
}


@router.put("/me/password", responses=responses, status_code=status.HTTP_204_NO_CONTENT)

I’m getting this mypy error on responses argument:

Argument "responses" to "put" of "APIRouter" has incompatible type "Dict[int, Dict[str, Type[Message]]]"; expected "Optional[Dict[Union[int, str], Dict[str, Any]]]"

When I change the argument to responses={**responses}, the error is still there, albeit a little bit different:

List item 0 has incompatible type "Dict[int, Dict[str, Type[Message]]]"; expected "Mapping[Union[int, str], Dict[str, Any]]"

Does anyone know why this is happening?

@vrodochenko
Copy link

I would assume that mypy cannot derive the type of your dictionary the way you want it to, which leads to this obscure error. Consider adding an annotation:

from typing import Dict

responses: Dict = {
    401: {"model": Message}
}

to make things pass the validation.

Also you can go through mypy issues (e.g this one) to learn more.

@ryanditjia
Copy link
Author

Thanks so much, your answer worked.

@tiangolo
Copy link
Member

tiangolo commented Nov 9, 2022

Thanks for the help @vrodochenko ! 🍰

And thanks for coming back to close the issue @ryanditjia

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants