Coder Social home page Coder Social logo

antonagestam / injected Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 1.0 32 KB

Simple, type-safe dependency injection in idiomatic Python.

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
dependency-injection idiomatic-python mypy python python3 static-typing type-safety

injected's Introduction

injected

CI Build Status Test coverage report
PyPI Package Python versions

Simple, type-safe dependency injection in idiomatic Python, inspired by FastAPI.

Injecting dependencies

from injected import depends, resolver


def get_a() -> int:
    return 13


def get_b() -> int:
    return 17


@resolver
def get_sum(
    a: int = depends(get_a),
    b: int = depends(get_b),
) -> int:
    return a + b


def test_resolves_dependencies():
    assert get_sum() == 30

Seeding the context of a resolver

It's sometimes useful to be able to provide an already resolved value, making it available throughout the dependency graph. The canonical example of this is how FastAPI makes things like requests and headers available to all dependencies.

To use this pattern, you specify a sentinel function, get_global_value in the example below, and then map it to a resolved value in a context passed to seed_context().

from injected import depends, resolver, seed_context


def get_global_value() -> int:
    ...


@resolver
def calculate_value(a: int = depends(get_global_value)) -> int:
    return a + 13


seeded = seed_context(calculate_value, {get_global_value: 31})


def test_can_seed_resolver_context():
    assert seeded() == 44

Async dependencies

The @resolver decorator works with both async and non-async functions, with the restriction that async dependencies can only be used with an async resolver. An async resolver however, can resolve both async and vanilla dependencies.

import asyncio
from injected import depends, resolver


async def get_a() -> int:
    return 13


def get_b() -> int:
    return 17


@resolver
async def get_sum(
    a: int = depends(get_a),
    b: int = depends(get_b),
) -> int:
    return a + b


def test_resolves_dependencies():
    assert asyncio.run(get_sum()) == 30

injected's People

Contributors

aiven-anton avatar antonagestam avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

aiven-anton

injected's Issues

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.