Skip to main content

Modern-DI integration for Typer

Project description

modern-di-typer

PyPI version Supported Python versions Downloads Coverage CI License GitHub stars uv Ruff ty

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

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 app
  • inject — decorator that resolves FromDI-annotated parameters before the command runs; also exposes typer.Context with ctx.obj["di_container"] for manual use
  • FromDI(provider) — marker used in Annotated[T, FromDI(...)]; accepts a provider instance or a type
  • fetch_di_container(ctx) — returns the app-scoped container from ctx.obj

Used by

  • semvertag — a CLI auto-tagger for GitLab/GitHub that wires its settings, API providers, and version-bump strategies through a modern_di container with modern-di-typer. See semvertag/ioc.py for a real-world setup_di + Group setup.

📦 PyPI

📝 License

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

modern_di_typer-2.0.3.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modern_di_typer-2.0.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file modern_di_typer-2.0.3.tar.gz.

File metadata

  • Download URL: modern_di_typer-2.0.3.tar.gz
  • Upload date:
  • Size: 4.0 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

Hashes for modern_di_typer-2.0.3.tar.gz
Algorithm Hash digest
SHA256 e6cb1a1737293b8f73a504b5737b76c56fde977d5920bd5f6e3135cf69800cb4
MD5 f561d24db8b497ad96a5441df5660247
BLAKE2b-256 125fa71bf5bb2d471220902141684bef1b63820504381e334ec5a06ad6015510

See more details on using hashes here.

File details

Details for the file modern_di_typer-2.0.3-py3-none-any.whl.

File metadata

  • Download URL: modern_di_typer-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.6 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

Hashes for modern_di_typer-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 31f9400b0650aa1b5eaee6747bf9abaf6f9993a369054cdd8cdddf7774c89de3
MD5 880d752cf35965b8ed3cdb30dec5da49
BLAKE2b-256 62dbbe44fbea51995ce737d1b1e120cf39618ea67b21f51482df1982f92af975

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page