Install & Compatibility
Where this runs
tested against v1.53.0.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.272s · 38.5MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 2.6s · import 0.234s · 36MB
35MB installed
● package 35MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
grpc
✓ import grpc
grpc-stubs provides type hints for symbols imported from the `grpcio` library (e.g., `grpc`, `grpc.aio`), allowing static analysis tools like Mypy to type-check gRPC code. It does not introduce new runtime imports from `grpc_stubs` itself.
grpc.aio
✓ import grpc.aio
Specifically for asynchronous gRPC code, `grpc-stubs` also provides type hints for the `grpc.aio` module.
This quickstart demonstrates a simplified gRPC client using placeholder types to represent code typically generated by `grpcio-tools`. `grpc-stubs` provides the necessary type hints for `grpc.Channel`, generated `_grpc.py` stubs, and other `grpcio` components, enabling tools like Mypy to perform static analysis on your gRPC client and server code. To run this, install `grpcio`, `mypy`, and `grpc-stubs` and then execute `mypy your_script_name.py`.
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")
Errors
Common errors & fixes
error: Call to untyped function "ServiceStub" in typed context [no-untyped-call]
This Mypy error occurs when a gRPC client stub, like `ServiceStub`, is instantiated without sufficient type information available to Mypy, often because the generated `_grpc.pyi` stubs are missing or not correctly picked up, or the stub itself lacks full annotations.
fixEnsure that `grpc-stubs` is installed and that `mypy-protobuf` is used to generate `.pyi` files for your `.proto` definitions, which provide the necessary type hints for client stubs. The generated stubs should be in a location Mypy can find, typically alongside the `.py` files.
Return type "Coroutine[Any, Any, Empty]" of "Segment" incompatible with return type "Empty" in supertype "MyServicer"
This Mypy error typically arises when implementing an asynchronous gRPC servicer (`grpc.aio`) where the abstract base class (often generated by `mypy-protobuf` for synchronous services) expects a direct return type, but the async implementation returns an `Awaitable` (which wraps the expected type).
fixManually adjust the generated `_grpc.pyi` file for async servicers to include `typing.Awaitable` in the return type annotations for async methods, or use type ignores (`# type: ignore`) for the conflicting lines if the runtime behavior is correct. This issue often requires specific handling for `grpc.aio` due to differences in how types are generated versus consumed.
AttributeError: 'Channel' object has no attribute 'unary_unary'
This runtime error indicates that a `grpc.Channel` object is being used incorrectly, specifically when trying to call a method like `unary_unary` directly on the channel. This method is typically used internally by generated gRPC stub classes to define RPC methods, not meant for direct invocation on the channel by user code. This can also happen if the channel is not properly initialized before creating the stub.
fixEnsure that you are creating and using the gRPC stub correctly, typically by passing an initialized `grpc.Channel` to the constructor of a generated stub class (e.g., `MyServiceStub(channel)`). The stub instance then exposes the RPC methods (e.g., `stub.MyMethod(...)`).
ModuleNotFoundError: No module named 'grpc'
`grpc-stubs` provides type hints for the `grpcio` library, but does not include the `grpcio` runtime itself. This error occurs when the core `grpcio` library is not installed in your Python environment.
fixInstall the `grpcio` package using pip: `pip install grpcio`. If you also need the gRPC tools for generating code from `.proto` files, install `grpcio-tools`: `pip install grpcio grpcio-tools`.
Missing type parameters for generic type "UnaryUnaryClientInterceptor" [type-arg]
This Mypy error occurs when using gRPC interceptor classes (e.g., `UnaryUnaryClientInterceptor`) without specifying the generic type parameters, especially when Mypy is run with strict or `--disallow-any-generics` flags. The `grpcio` library and `grpc-stubs` use generic types for these interceptors.
fixWhen subclassing or using generic gRPC interceptor types, explicitly provide the type parameters for the request and response, for example: `class MyInterceptor(grpc.UnaryUnaryClientInterceptor[MyRequestType, MyResponseType]):`.
Upgrade
Version history
1.53.0.6latest on PyPI · released Apr 28, 2025
Audit
Dependencies
grpciorequiredgrpc-stubs provides type hints for this library's runtime components.
mypyrequiredRequired to perform static type checking and utilize the stubs.
grpcio-toolsoptionalOften used for generating gRPC Python code from .proto files, though grpc-stubs focuses on the core `grpc` library.