gRPC Mypy Stubs
grpc-stubs provides high-quality Mypy stubs for the gRPC Python library (`grpcio`). It enables static type checking for gRPC client and server code, significantly improving code reliability and maintainability. The current version is 1.53.0.6, which tracks `grpcio` releases, with updates typically coinciding with `grpcio` major versions.
Warnings
- gotcha Ensure your `grpc-stubs` version matches your installed `grpcio` runtime library version. Mismatched versions can lead to incorrect or missing type hints, as `grpc-stubs` tracks the `grpcio` API.
- gotcha `grpc-stubs` provides type hints for static analysis (e.g., Mypy) only. It does not affect runtime behavior or introduce new runtime APIs. Your code will execute the same whether `grpc-stubs` is installed or not.
- gotcha If you use `grpcio-tools` to generate your own Python stubs from `.proto` files, `grpc-stubs` might conflict or duplicate type definitions, especially for `_pb2.py` and `_pb2_grpc.py` modules. The `grpc-stubs` package primarily targets the core `grpc` runtime module.
- gotcha To benefit from `grpc-stubs`, you *must* explicitly run `mypy` or another compatible static type checker. Simply installing the package has no effect if the type checker is not invoked.
Install
-
pip install grpc-stubs
Imports
- grpc
import grpc
- grpc.aio
import grpc.aio
Quickstart
import grpc
# In a real project, these would be generated from your .proto files by grpcio-tools:
# from your_project import greeter_pb2 as pb2
# from your_project import greeter_pb2_grpc as pb2_grpc
# For this example, we'll use placeholder types to make it runnable.
class HelloRequest:
def __init__(self, name: str):
self.name = name
class HelloReply:
def __init__(self, message: str):
self.message = message
class GreeterStub:
# This __init__ signature and method signature are what grpc-stubs would type.
def __init__(self, channel: grpc.Channel):
self._channel = channel
def SayHello(self, request: HelloRequest, timeout: float = None) -> HelloReply:
# Simulate a network call
print(f"Simulating gRPC call for: {request.name}")
return HelloReply(message=f"Hello, {request.name}!")
def run_client():
# grpc-stubs provides types for the `grpc` module itself, like `insecure_channel`
with grpc.insecure_channel('localhost:50051') as channel:
# And also for generated stubs like GreeterStub
stub: GreeterStub = GreeterStub(channel)
# This interaction is now type-checked by mypy thanks to grpc-stubs
request = HelloRequest(name="Mypy User")
response: HelloReply = stub.SayHello(request, timeout=5.0)
print(f"Client received: {response.message}")
# Example of a type error that mypy would catch:
# stub.SayHello(HelloRequest(name=123)) # Mypy would flag `name` as int, expected str
if __name__ == '__main__':
run_client()
print("\nTo enable type checking for this code:")
print("1. Ensure you have `grpcio`, `mypy`, and `grpc-stubs` installed:")
print(" pip install grpcio mypy grpc-stubs")
print("2. Run mypy on your script:")
print(" mypy your_script_name.py")