How to fix MyPy [return-value] error when type is actually correct

I am using Django-Ninja + MyPy and I get this error that type it received tuple[int, dict[str, str]]" is not correct as it doesn't match one of the tuple[int, None] | tuple[int, DetailType] types but it is exactly as second type. Because in other parts of code it can figure out correctly it has "detail" as key. But as soon as I use Union[...] it somehow cannot look recursively?

apps/users/views.py:106: error: Incompatible return value type (got "tuple[int, Message]", expected "tuple[int, None] | tuple[int, DetailType]")  [return-value]

This is how my types look like:

DetailType = TypedDict("DetailType", {"detail": str})
MessageType = tuple[int, DetailType]
NoContentType = tuple[int, None]

And this is complete code where mypy is returning error:

@post("me/password/change/", response={204: None, 401: Message}, url_name="users_me_password_change")
def change_password(request: HttpRequest, data: ChangePasswordRequest) -> Union[NoContentType, MessageType]: <<< THIS IS RETURN TYPE
    user = request.user
    if not user.check_password(data.password):
        return 401, {"detail": "Password is incorrect."} <<< IN THIS LINE IS MYPY ERROR
    user.set_password(data.new_password)
    user.save()
    return 204, None
Back to Top