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.

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'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 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
  • action_scope(ctx) — context manager yielding a fresh Scope.ACTION container (a child of the command's request container); open one per action
  • 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

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


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-3.0.1.tar.gz (4.4 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-3.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_typer-3.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.33 {"installer":{"name":"uv","version":"0.11.33","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-3.0.1.tar.gz
Algorithm Hash digest
SHA256 5b6dea64d3d65b3e43037e98a55a6f53292a3f665f75af5886b9f58bb1511163
MD5 6ca61054526cf29f2166f31e56f540f9
BLAKE2b-256 a29b8cd84a4325a251dd34197f7cd35a26a8602461b00208c67c5c0d16c201d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_typer-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.33 {"installer":{"name":"uv","version":"0.11.33","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-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37794073f73eb211c103fc43423799599ed3818c020c31300961cd77a5b30877
MD5 1098bfcc52a44acfae1df295d79f0e8b
BLAKE2b-256 9a35ed4678533ad0c3617484cd6e1e7c21a2e9fe67e71642c2ae5b94564eff96

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