modern-di integration for gRPC (grpcio).
Full guide: gRPC integration docs
Usage example: examples/
Installation
uv add modern-di-grpc # or: pip install modern-di-grpc
Usage
gRPC has no dependency-injection system of its own, so modern-di-grpc pairs an @inject decorator with inert FromDI markers. A DIInterceptor (sync) or DIAioInterceptor (async) opens one Scope.REQUEST child container per RPC and resolves the FromDI-marked parameters of @inject-decorated servicer methods. Constructing the interceptor registers the ServicerContext provider on the container automatically; there is no separate setup call.
import typing
from concurrent import futures
import grpc
from modern_di import Container, Group, Scope, providers
from modern_di_grpc import DIInterceptor, FromDI, inject
from myapp import greeter_pb2, greeter_pb2_grpc # your generated stubs
class Settings:
def __init__(self) -> None:
self.greeting = "hello"
class Greeter:
def __init__(self, settings: Settings) -> None: # auto-injected by type
self._settings = settings
def greet(self, name: str) -> str:
return f"{self._settings.greeting}, {name}"
class AppGroup(Group):
settings = providers.Factory(Settings, scope=Scope.APP, cache=True)
greeter = providers.Factory(Greeter, scope=Scope.REQUEST)
class GreeterService(greeter_pb2_grpc.GreeterServicer):
@inject
def SayHello(
self,
request: greeter_pb2.HelloRequest,
context: grpc.ServicerContext,
greeter: typing.Annotated[Greeter, FromDI(Greeter)], # resolve by type
) -> greeter_pb2.HelloReply:
return greeter_pb2.HelloReply(message=greeter.greet(request.name))
container = Container(groups=[AppGroup])
server = grpc.server(
futures.ThreadPoolExecutor(max_workers=10),
interceptors=[DIInterceptor(container)],
)
container.validate() # optional fail-fast; must come after DIInterceptor registers its providers
greeter_pb2_grpc.add_GreeterServicer_to_server(GreeterService(), server)
server.add_insecure_port("[::]:50051")
server.start()
try:
server.wait_for_termination()
finally:
server.stop(grace=5).wait() # let in-flight RPCs finish before APP finalizers run
container.close_sync()
For an async server, pass DIAioInterceptor(container) to grpc.aio.server(...) and write async def servicer methods; @inject adapts to sync, async, and async-generator (server-streaming) methods across all four RPC types. A container is open from construction, so there is no .open() call before constructing the interceptor, and neither interceptor opens the root itself. Validation is explicit: call .validate() after constructing the interceptor if you want a broken graph to fail at start-up rather than at the first RPC that touches it. gRPC has no server startup or shutdown hook, so closing the root container is up to you: call close_sync() (or await close_async() on grpc.aio) after the server stops, in a finally as above so Ctrl+C still runs the finalizers.
API
| Symbol | Description |
|---|---|
DIInterceptor(container) |
grpc.ServerInterceptor for the sync thread-pool server. Opens a Scope.REQUEST child per RPC (close_sync); auto-registers grpc_context_provider |
DIAioInterceptor(container) |
grpc.aio.ServerInterceptor for the async server. Same, with close_async; sync methods run on the server's migration_thread_pool and close their child with close_sync |
FromDI(dependency) |
Inert marker for Annotated[T, FromDI(...)] in servicer-method signatures; accepts a provider instance or a type |
inject(method) |
Decorates a servicer method to resolve its FromDI parameters from the current RPC's child container; adapts to sync / async / async-generator methods |
fetch_di_container() |
Returns the current RPC's child container (raises RuntimeError outside an RPC) |
grpc_context_provider |
ContextProvider exposing grpc.ServicerContext at Scope.REQUEST; auto-registered by the interceptor |
📦 PyPI
📝 License
Part of modern-python
Built on modern-di, a dependency-injection framework with an IoC container and scopes.
Browse the full list of templates and libraries in
modern-python; the org profile has the categorized index.
Metadata
Release files for modern-di-grpc 4.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| modern_di_grpc-4.1.2.tar.gz | 6.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_grpc-4.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.9 kB
Release files / modern_di_grpc-4.1.2.tar.gz
| Download URL | modern_di_grpc-4.1.2.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4a44f9bc6742cdb0d9edccf109e47ed1d093258e0675880e7562f7dbe82c1c87
|
|
BLAKE2b-256 checksum How to use checksums |
52e67daaedeea435c40e44ea942224a80bac20f838473dc86aef89bedddf6ed3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / modern_di_grpc-4.1.2-py3-none-any.whl
| Download URL | modern_di_grpc-4.1.2-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0bfe11b7675bb359df79c71c77dd13b6865c135f1f6472ab7e952c300945d6ed
|
|
BLAKE2b-256 checksum How to use checksums |
228966a8f7e9d55f284b5d57bf631d14bc8bfd506493f2f407cd7d1f673a84d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for modern-di-grpc 4.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.