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

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

Open
q0w opened this issue Jul 24, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@q0w
Copy link
Contributor

q0w commented Jul 24, 2022

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 q0w added the bug Something isn't working label Jul 24, 2022
@q0w q0w changed the title BaseSerializer.is_valid should return NoReturn when raise_exception = True BaseSerializer.is_valid should return bool | NoReturn when raise_exception = True Jul 24, 2022
@sobolevn
Copy link
Member

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

@q0w
Copy link
Contributor Author

q0w commented Jul 24, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants