djangorestframework-stubs
djangorestframework-stubs copied to clipboard
BaseSerializer.is_valid should return bool | NoReturn when raise_exception = True
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:
pythonversion: 3.10.5djangoversion: 4.0.6mypyversion: 0.971django-stubsversion: 1.12.0
I don't think it is NoReturn, but rather None
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