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 modern_di
import typer
from modern_di import Group, Scope, providers
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 check of the provider graph


@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, for example one per item in a batch.

import typer
from modern_di import Group, Scope, providers
from modern_di_typer import 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(job_name)

API

Symbol Description
setup_di(app, container) Registers the container with a Typer app
inject Decorator that resolves FromDI-annotated parameters before the command runs; also exposes typer.Context, from which fetch_di_container(ctx) returns the app container. Raises RuntimeError naming setup_di when a command reaches it without setup_di called
FromDI(dependency) 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 container registered by setup_di, from any command of the app, including those of nested add_typer sub-apps; it does not read ctx.obj, so the container stays reachable after a callback assigns ctx.obj

Used by

semvertag uses modern-di-typer to wire its settings, API providers and version-bump strategies; see semvertag/ioc.py.

📦 PyPI

📝 License

Part of modern-python

Built on modern-di, a dependency-injection framework with an IoC container and scopes.

Browse the full list of templates and libraries in modern-python; the org profile has the categorized index.

Metadata

Release files for modern-di-typer 3.2.0

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.2.0
File Size Uploaded
modern_di_typer-3.2.0.tar.gz 4.8 kB Details

Built distribution (wheel)

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

Total release size: 10.0 kB

Release files / modern_di_typer-3.2.0.tar.gz

Download URL modern_di_typer-3.2.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d2a7364c357b29d7c27837728d0ab1ec844aa82483da0a7c30edc76fb3bc3168
BLAKE2b-256 checksum
How to use checksums
9b1e762bbf84187879a1d9a0c8d6b2c5daf4b05d94abe89649fdd5166604b9e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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.2.0-py3-none-any.whl

Download URL modern_di_typer-3.2.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac795effff37d76a4a2e618560bca83b8eec4eeef62c5af13ff65c4a7066ce31
BLAKE2b-256 checksum
How to use checksums
7762e11c91958ca8c891cf476d0a07d4fa2aea21072da2e5f2ecee73f384d1da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","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

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

This release

3.2.0 This release

2 release files

3.1.0

2 release files

3.0.3

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