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")
Both
ServicerContext.abort()
andServicerContext.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.