Skip to main content

Simple, type-safe dependency injection

Project description

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

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

injected-0.1.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

injected-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file injected-0.1.1.tar.gz.

File metadata

  • Download URL: injected-0.1.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for injected-0.1.1.tar.gz
Algorithm Hash digest
SHA256 de15700e04faa96796a41b335c0c8c350ee45f525fe808c67336d5561a8c51c5
MD5 a5eef4956aa3e760c8718a4012711a33
BLAKE2b-256 da7bae6442aabdecdb2c88d58cde6104297c6487df20c53e3e00261c71080c03

See more details on using hashes here.

File details

Details for the file injected-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: injected-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for injected-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b78513b37c4b7b0951a1891d9b9456716cb924f4e44d45f6bc2fda8b18e45deb
MD5 225a9635dce68588666d2e7a35b1057d
BLAKE2b-256 566be69630604eda2198374e445c8b3291f76249b037819ebd58d80224d9b2f7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page