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


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})

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


assert asyncio.run(get_sum_async()) == 53
assert get_sum_sync() == 53

Metadata

Release files for injected 0.2.2

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

Built distribution (wheel)

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

Total release size: 14.6 kB

Release files / injected-0.2.2.tar.gz

Download URL injected-0.2.2.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
675ac38cadb0f59e3b5d6bd17ff1832ea9659e1c14bb6dce0c362e1d42d3bc66
BLAKE2b-256 checksum
How to use checksums
1c730f440ee5c5601379e18abb37badd771ee00ea932d1d48fe5ab87d6dd0bfe
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 May 31, 2025.

Transparency log

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

Download URL injected-0.2.2-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b442efe51dbddeb348bbfaa7166201dfe17551fa696b96f01e88b5beae44f3ca
BLAKE2b-256 checksum
How to use checksums
c7228662ba3928f2d5b928e5ef0cad2ea121146c312fe2a55df6c31e548cc241
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 May 31, 2025.

Transparency log

Release history Release notifications | RSS feed

0.3.0

2 release files

This release

0.2.2 This release

2 release files

0.2.1

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