Since a while, a huge qualm I had with the official Protobuf/gRPC tooling for Python has been resolved. There wasn’t any IDE support for the stubs generated by the Protobuf compiler, making development tedious and error-prone.
For this reason, I had switched to betterproto, which creates significantly prettier, pythonic and type-hinted code. Unfortunately, it is also lacking a few features, and comes at the expense of performance which in the official tooling is achieved by using C(++) modules (which cause the ugly generated code).
Fortunately, type hinting for the official tooling is possible using types-protobuf.
All you have to do is install it alongside gPRC/Protobuf:
pip install grpcio-tools mypy-protobuf types-protobuf
And then you can instruct protoc
to generate mypy
type hints alongside the generated code:
python -m grpc_tools.protoc -I./lib/protos --python_out=./lib/grpc --grpc_python_out=./lib/grpc --mypy_out=./lib/grpc lib/protos/*
For example in PyCharm these will be picked up and used automatically, making development a lot easier.
One thing that it cannot do is add type hints to the parameters of the gRPC stub methods, so when you implement those, I would recommend adding the appropriate type hint right away so
async def SomeRpcCall(self, request, context):
...
becomes:
async def SomeRpcCall(self, request: SomeRpcCallRequest, context) -> SomeRpcCallResponse:
Hope this helps!