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 and context managers

Dependencies can be any combination of async and non-async functions and context managers. Async dependencies are resolved concurrently, and are scheduled at the optimal time, as soon as their own dependencies are resolved.

Context managers are torn down upon exiting the entry-point function.

import asyncio
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from injected import depends, resolver


async def get_a() -> int:
    return 13


def get_b() -> int:
    return 17


@asynccontextmanager
async def get_c() -> AsyncIterator[int]:
    yield 23


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


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


def test_resolves_dependencies():
    assert asyncio.run(get_sum_async()) == 53
    assert get_sum_sync() == 53

Metadata

Release files for injected 0.2.1

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.1
File Size Uploaded
injected-0.2.1.tar.gz 8.0 kB Details

Built distribution (wheel)

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

Total release size: 14.6 kB

Release files / injected-0.2.1.tar.gz

Download URL injected-0.2.1.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c39f2f3fca0844ecfd37f9075185b5a1ef2b5c48229b25ebfa35e746cdf822d3
BLAKE2b-256 checksum
How to use checksums
3c9ea24c181744b99db70d1327ecbe0b3c54791b11a6ebc8409b6cb918c77e6a
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.1-py3-none-any.whl

Download URL injected-0.2.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca9068171c6955d09bf08120d804a6f8dd3eb50f6119bb8f6b1a1169385f7321
BLAKE2b-256 checksum
How to use checksums
9feca307b28a96ebff22dd136dfc9e663d1881de537f8f46632d8e74f80e805c
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

This release

0.2.1 This release

2 release files

0.2.0

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