shabbyrobe / grpc-stubs

gRPC typing stubs for Python
MIT License
35 stars 21 forks source link

Update ServicerContext abort methods to `NoReturn` #9

Closed mintchkin closed 3 years ago

mintchkin commented 3 years ago

Both ServicerContext.abort() and ServicerContext.abort_with_status() are documented to always raise an exception rather than returning.

Annotating it as NoReturn allows mypy to figure out the branching correctly.

class EchoService(EchoServicer):
    # This currently fails with `Missing return statement`, but typechecks correctly after the patch
    def Echo(self, request: EchoRequest, context: grpc.ServicerContext) -> EchoPayload:
        try:
            return EchoPayload(value=request.value)
        except:
            context.abort(grpc.StatusCode.ABORTED, "Uh Oh")
shabbyrobe commented 3 years ago

This is great! Thank you! I will cut a new release tonight.