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 typer
import modern_di
from modern_di import Scope, providers, Group
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; must come after setup_di registers its providers
@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 — one per item in a batch, for example.
import typer
from modern_di import Scope, providers, Group
from modern_di_typer import FromDI, 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()
API
setup_di(app, container)— register the container with a Typer appinject— decorator that resolvesFromDI-annotated parameters before the command runs; also exposestyper.Context, from whichfetch_di_container(ctx)returns the app container. RaisesRuntimeErrornamingsetup_diwhen a command reaches it withoutsetup_dicalledFromDI(provider)— marker used inAnnotated[T, FromDI(...)]; accepts a provider instance or a typeaction_scope(ctx)— context manager yielding a freshScope.ACTIONcontainer (a child of the command container); open one per action. RaisesRuntimeErrornaming@injectwhen called from a command without itfetch_di_container(ctx)— returns the app container registered bysetup_di, from any command of the app, including those of nestedadd_typersub-apps; it does not readctx.obj, so the container stays reachable after a callback assignsctx.obj
Used by
- semvertag — a CLI
auto-tagger for GitLab/GitHub that wires its settings, API providers, and
version-bump strategies through a
modern_dicontainer withmodern-di-typer. Seesemvertag/ioc.pyfor a real-worldsetup_di+Groupsetup.
📦 PyPI
📝 License
Part of modern-python
Built on modern-di, a dependency-injection framework with IoC container and scopes.
Browse the full list of templates and libraries in
modern-python — see the org profile for the categorized index.
Release files for modern-di-typer 3.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modern_di_typer-3.1.0.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_typer-3.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size:10.1 kB
Release files / modern_di_typer-3.1.0.tar.gz
| Download URL | modern_di_typer-3.1.0.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b5a1701387f3dfa39dafc59491250d322206084a4b109419456aac6d2c45a346
|
|
BLAKE2b-256 checksum How to use checksums |
32fb74c242d95a2d89e250e74989a375c1c0558ce76a98ac7ba4e610a6523b93
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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-3.1.0-py3-none-any.whl
| Download URL | modern_di_typer-3.1.0-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
235df84e4a997d9b67f79d1820a3bba945ef627553102cc20d41466822936e13
|
|
BLAKE2b-256 checksum How to use checksums |
c45ddbe697c7abfe6b5b6959a690463a958e1a190ef0616de073389763909dd9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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}
|