Skip to main content

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 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. Raises RuntimeError naming setup_di when a command reaches it without setup_di called
  • 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 container); open one per action. Raises RuntimeError naming @inject when called from a command without it
  • 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.

Release files for modern-di-typer 3.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modern-di-typer 3.0.3
File Size Uploaded
modern_di_typer-3.0.3.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for modern-di-typer 3.0.3
File Interpreter ABI Platform
modern_di_typer-3.0.3-py3-none-any.whl Python 3 none any Details

Total release size:9.9 kB

Release files / modern_di_typer-3.0.3.tar.gz

Download URL modern_di_typer-3.0.3.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
106d2e068e20252318005be7cc292c261392b846e31817fde1f8471392840474
BLAKE2b-256 checksum
How to use checksums
7161c9d97e8f7a1acca4242a71b8ed85f95517fcfca2b0890fb3f0e2bdd30068
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.0.3-py3-none-any.whl

Download URL modern_di_typer-3.0.3-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22769a1d305155f11717a3aa8332ae865ab78831352a3b5af4bb637221723dd3
BLAKE2b-256 checksum
How to use checksums
af8ba837d2b79e038a66205f791889da1499bdd00ff4cf3102076a87ffd7117e
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 history Release notifications | RSS feed

3.1.0

2 release files

This release

3.0.3 This release

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page