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 4.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 4.0.3
File Size Uploaded
modern_di_typer-4.0.3.tar.gz 5.3 kB Details

Built distribution (wheel)

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

Total release size: 10.7 kB

Release files / modern_di_typer-4.0.3.tar.gz

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

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

This release

4.0.3 This release

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.2.0

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