Skip to main content

Dependency injection for Typer

Add dependency injection functionality to Typer library. Implement the same DI behavior as in FastAPI.

Description

With typer_di you can easely move common parts of your commands to dependency functions.

Any dependency function can depends on other functions and etc. All args and options will be merged to interface of each command that uses corresponding dependencies.

typer_di ensures to call each dependency callback only once.

Usage

Lets say you have a method that validates and parses a config:

def get_config(config_path: Path) -> Config:
    ...

In case of multiple commands you need to duplicate annotation code:

from typing import Annotated
from typer import Typer, Option

app = Typer()

@app.command()
def first(..., config_path: Annotated[Path, Option("--config")]):
    config = get_config(config_path)
    ...

@app.command()
def second(..., config_path: Annotated[Path, Option("--config")]):
    config = get_config(config_path)
    ...

With typer_di you can move annotations to parsing methods.

All you need is to use Depends annotation and replace the original Typer by TyperDI (it's a thin layer that transforms all passed functions unwrapping dependencies).

from typing import Annotated
from typer import Option
from typer_di import TyperDI, Depends

app = TyperDI()

def get_config(config_path: Annotated[Path, Option("--config")]) -> Config:
    ...

@app.command()
def first(..., config: Config = Depends(get_config)):
    ...

@app.command()
def second(..., config: Config = Depends(get_config)):
    ...

Release Notes

v0.1.5

  • update package meta info for python 3.14

v0.1.4

  • support python 3.14
  • drop python 3.7

v0.1.3

  • depend on typer-slim
  • use strict typing

v0.1.2

  • add py.typed marker to the package

v0.1.1

  • fix invalid validation for duplicated names
  • check for loops in dependency graph

v0.1.0

  • first public version
  • support py37+

Metadata

Release files for typer-di 0.1.5

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

Source distribution (sdist)

Source distribution for typer-di 0.1.5
File Size Uploaded Provenance Actions
typer_di-0.1.5.tar.gz 6.4 kB Without provenance Details for typer_di-0.1.5.tar.gz

Release files / typer_di-0.1.5.tar.gz

Download URL typer_di-0.1.5.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
daf4c36350d7ae055f1ca9584089110cddef3109d4c9ed36b2fd319cb099eba1
BLAKE2b-256 checksum
How to use checksums
b2c5252818853cfe303cef47d93fc87cae56ee8dce63b7a852b9e477225eaf76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.3

Security for typer-di 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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