Skip to main content

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

Metadata

Release files for injected 0.2.0

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

Source distribution (sdist)

Source distribution for injected 0.2.0
File Size Uploaded
injected-0.2.0.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for injected 0.2.0
File Interpreter ABI Platform
injected-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.3 kB

Release files / injected-0.2.0.tar.gz

Download URL injected-0.2.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
763e903d4ae97780664608d4e6b1dd69cffcfb2a1dc429de4190179bad026e91
BLAKE2b-256 checksum
How to use checksums
21866d2ae5678d1ebef2c61545c25c9cfac4167d9f1d9a8845a3c0ca2aa89c67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 27, 2025.

Transparency log

Release files / injected-0.2.0-py3-none-any.whl

Download URL injected-0.2.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d1c2f94ff2a22b4d6c17597c798b5fb76ae5ee12746a3291aa906026fcc54bec
BLAKE2b-256 checksum
How to use checksums
e6115eb973eaab0029a0ecd834dcb1a2dcb541bfef2fe138d6fddff7d649f720
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 27, 2025.

Transparency log

Release history Release notifications | RSS feed

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.0

2 release files

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