modern-di integration for Typer.
Full guide: Typer integration docs
Usage example: examples/
Installation
uv add modern-di-typer # or: pip install modern-di-typer
Usage
import typing
import modern_di
import typer
from modern_di import Group, Scope, providers
from modern_di_typer import FromDI, inject, setup_di
class Dependencies(Group):
settings = providers.Factory(creator=lambda: {"debug": True})
service = providers.Factory(scope=Scope.REQUEST, creator=MyService, bound_type=None)
app = typer.Typer()
container = modern_di.Container(groups=[Dependencies])
setup_di(app, container)
container.validate() # optional fail-fast check of the provider graph
@app.command()
@inject
def my_command(
name: typing.Annotated[str, typer.Argument()],
service: typing.Annotated[MyService, FromDI(Dependencies.service)],
) -> None:
service.run(name)
if __name__ == "__main__":
with container:
app()
Action scope
Scope.ACTION dependencies live below the per-command Scope.REQUEST container.
Open one with action_scope(ctx): each with block yields a fresh action-scoped
container (a child of the command container), so you can open as many as
you need within a single command, for example one per item in a batch.
import typer
from modern_di import Group, Scope, providers
from modern_di_typer import action_scope, inject
class Dependencies(Group):
job = providers.Factory(scope=Scope.ACTION, creator=MyJob, bound_type=None)
@app.command()
@inject
def my_command(ctx: typer.Context) -> None:
for job_name in job_names:
with action_scope(ctx) as action:
action.resolve_provider(Dependencies.job).run(job_name)
API
| Symbol | Description |
|---|---|
setup_di(app, container) |
Registers the container with a Typer app |
inject |
Decorator that resolves FromDI-annotated parameters before the command runs; also exposes typer.Context, from which fetch_di_container(ctx) returns the app container. Raises RuntimeError naming setup_di when a command reaches it without setup_di called |
FromDI(dependency) |
Marker used in Annotated[T, FromDI(...)]; accepts a provider instance or a type |
action_scope(ctx) |
Context manager yielding a fresh Scope.ACTION container (a child of the command container); open one per action. Raises RuntimeError naming @inject when called from a command without it |
fetch_di_container(ctx) |
Returns the app container registered by setup_di, from any command of the app, including those of nested add_typer sub-apps; it does not read ctx.obj, so the container stays reachable after a callback assigns ctx.obj |
Used by
semvertag uses modern-di-typer to wire its settings, API providers and version-bump strategies; see semvertag/ioc.py.
Links
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-typer 4.0.4
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 | Actions |
|---|---|---|---|---|
| modern_di_typer-4.0.4.tar.gz | 5.3 kB | Without provenance | Details for modern_di_typer-4.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| modern_di_typer-4.0.4-py3-none-any.whl | Python 3 | none | any | Details for modern_di_typer-4.0.4-py3-none-any.whl |
Total release size: 10.6 kB
Release files / modern_di_typer-4.0.4.tar.gz
| Download URL | modern_di_typer-4.0.4.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eeefbe6829ed6e8f5c10e75a721cf0bf965b83269295466421acd32ccec89913
|
|
BLAKE2b-256 checksum How to use checksums |
8b6e5e0446bd6fc059489fed863aad64201587565a4823d52235e81de4750538
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","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_typer-4.0.4-py3-none-any.whl
| Download URL | modern_di_typer-4.0.4-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3f0160f7b79d6fa5f5be64b33fb0de32e4a3a23d2f380e258bc64b9db74867d7
|
|
BLAKE2b-256 checksum How to use checksums |
ed003e6109db902fa8e64775ba74e4934412332b194c67c8950538b64bf0a58f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","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-typer 4.0.4
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.