Skip to main content

Modern-DI integration for Typer

Project description

"Modern-DI-Typer"

Test Coverage Supported versions downloads GitHub stars

Integration of Modern-DI to Typer

📚 Documentation

Installation

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__":
    app()
    container.close_sync()

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
  • build_command_container(ctx) — context manager yielding a Scope.REQUEST child container; use inside @inject commands for Scope.ACTION work
  • fetch_di_container(ctx) — returns the app-scoped container from ctx.obj

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.0.tar.gz (4.3 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.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_typer-2.0.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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.0.tar.gz
Algorithm Hash digest
SHA256 1a328a67d169c12f796291369a4e134c699ad911f136d5097015fd412a14a257
MD5 d498bea68648cdb944e9e73de825b241
BLAKE2b-256 606de30e3fdc87485c1e8a03684d77ff593da8e6688008fd27f7e4d71d45ee8a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_typer-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36d4d8eb66249577afbf0fa56b3855ef77402f67a42dee3104f3f9144ec06bcd
MD5 6698c4655f14ef9922923d1ba12f3c6e
BLAKE2b-256 c845e0ed2bc2ba02cc2cb9b221fa66db3ff2d9617d4325a3f21bd6cde65d5d9c

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