Registry / type-stubs / grpc-stubs

grpc-stubs

JSON →
library1.53.0.6pypypi✓ verified 23d ago

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.

pip install grpc-stubs
INSTALL
IMPORT
SIG · GRPC-STUBS
G
grpc-stubs
type-stubspythonv1.53.0.6
Install
2.6s avg
Import
253ms
Disk
35MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.272s · 38.5MB
glibc
py 3.103.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")
Debug
Known issues
gotchaEnsure 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.
fix
Always install `grpc-stubs` with the same major.minor version as your `grpcio` (e.g., if `grpcio==1.53.0`, use `grpc-stubs==1.53.0.*`). Use `pip install grpcio==X.Y.Z grpc-stubs==X.Y.Z.*` to enforce this.
affects: All versions.
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.
fix
Understand that `grpc-stubs` is a developer tool. Integrate `mypy` or similar static analysis tools into your development workflow and CI/CD pipeline to leverage its benefits.
affects: All versions.
gotchaIf 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.
fix
For type hints related to your own generated stubs (from `.proto` files), rely on `grpcio-tools` to produce accurate type hints. `grpc-stubs` is most valuable for the core `grpc` library components. If conflicts occur, you might need to exclude specific paths from `mypy` configuration or carefully manage your stub generation process.
affects: All versions.
gotchaTo 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.
fix
Integrate `mypy` into your development workflow and CI/CD. Use `mypy your_script.py` or configure it in your `pyproject.toml` or `mypy.ini`.
affects: All versions.
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.
fix
Ensure 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).
fix
Manually 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.
fix
Ensure 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.
fix
Install 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.
fix
When 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.
Agent activity
93 hits · last 30 days
node
84
Bingbot
1
OpenAI (training)
1
Resources