Giter VIP home page Giter VIP logo

typer_di's Introduction

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.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+

typer_di's People

Contributors

greendwin avatar

Stargazers

Stéphane Wirtel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.