djangorestframework-stubs icon indicating copy to clipboard operation
djangorestframework-stubs copied to clipboard

BaseSerializer.is_valid should return bool | NoReturn when raise_exception = True

Open q0w opened this issue 3 years ago • 2 comments

Bug report

What's wrong

BaseSerializer.is_valid should return NoReturn when takes raise_exception=True Now mypy fails with

 error: Missing return statement  [return]

How is that should be

    @overload
    def is_valid(self, raise_exception: Literal[False] = ...) -> bool: ...
    @overload
    def is_valid(self, raise_exception: Literal[True] = ...) -> Literal[True] | NoReturn: ...

System information

  • OS:
  • python version: 3.10.5
  • django version: 4.0.6
  • mypy version: 0.971
  • django-stubs version: 1.12.0

q0w avatar Jul 24 '22 05:07 q0w

I don't think it is NoReturn, but rather None

sobolevn avatar Jul 24 '22 07:07 sobolevn

Its common to use smth like this in views:

def req(request: AuthenticatedRequest) -> Response:
    serializer = self.get_serializer_class()(data=request.data)
        if serializer.is_valid(raise_exception=True):
            ...
            return Response(
                self.serializer_class(
                    request.user,
                    context={'request': request},
                ).data,
            )

If serializer is not valid, ValueError would be raised

q0w avatar Jul 24 '22 08:07 q0w