modern-di integration for Typer
Project description
Modern-DI integration for Typer.
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)
@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's request 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.Contextwithctx.obj["di_container"]for manual useFromDI(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's request container); open one per actionfetch_di_container(ctx)— returns the app-scoped container fromctx.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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modern_di_typer-2.1.1.tar.gz.
File metadata
- Download URL: modern_di_typer-2.1.1.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c6f4b8055580d35b10355d1173e77ab7dd2405ecff5b8d70f244ea0be8cb34a
|
|
| MD5 |
caba7cc927d26b5a63c7629558d2c8ff
|
|
| BLAKE2b-256 |
0fd0513d0e9834c5832142b9b819389ffe33fb11250787cf1bd810dec6b8295d
|
File details
Details for the file modern_di_typer-2.1.1-py3-none-any.whl.
File metadata
- Download URL: modern_di_typer-2.1.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53c208ddd87f688ba0f917159bdf76ba9dd070ea894a387fecbc7dd5d8561e48
|
|
| MD5 |
ebecffaea8a77384a7b21ab4c12e38bc
|
|
| BLAKE2b-256 |
09bbb1efb2890ae3da9e14e2b9f61d4d1ee98637dfc1dc2f37644541d9b6f2d6
|