Modern-DI integration for Typer
Project description
"Modern-DI-Typer"
Integration of Modern-DI to Typer
📚 Documentation
Installation
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
To resolve Scope.ACTION dependencies, inject modern_di.Container (the REQUEST-scoped container created by @inject) and build a child:
import modern_di
from modern_di import Scope, providers, Group
from modern_di_typer import FromDI, inject
class Dependencies(Group):
job = providers.Factory(scope=Scope.ACTION, creator=MyJob, bound_type=None)
@app.command()
@inject
def my_command(
container: typing.Annotated[modern_di.Container, FromDI(modern_di.Container)],
) -> None:
with container.build_child_container() as action_container:
job = action_container.resolve_provider(Dependencies.job)
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 typefetch_di_container(ctx)— returns the app-scoped container fromctx.obj
Part of modern-python
Browse the full list of templates and libraries in
modern-python — see the org profile for the
categorized index.
Project details
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.0.2.tar.gz.
File metadata
- Download URL: modern_di_typer-2.0.2.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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 |
2b934736b46ec98a42394c6e20502b6ad542a79dd8a84a1ade4859d5ff9dcdf8
|
|
| MD5 |
f99872e000bfef63976eb376ed073519
|
|
| BLAKE2b-256 |
b654c4d48273804a33cbeaf20904f91f9fd5bab7a457407ede75159054c6d043
|
File details
Details for the file modern_di_typer-2.0.2-py3-none-any.whl.
File metadata
- Download URL: modern_di_typer-2.0.2-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","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 |
5aa73c873dfaebdf4494f0c789905458cf69eff82edf8e4e2fe44a9db6296c16
|
|
| MD5 |
74c9529b206a230e2e53663b58d77d82
|
|
| BLAKE2b-256 |
d9fc14209eb4bb516059c949e144fd84d9474e7cf8d8c28b6a4edf397a63b3a8
|