Simple, type-safe dependency injection
Project description
injected
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file injected-0.3.0.tar.gz.
File metadata
- Download URL: injected-0.3.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f9bd1c56c20f71fcf8288a198845aecc93649fbe613c9a564a0000b3c606346
|
|
| MD5 |
af1439300cb10a923695ec63702e5944
|
|
| BLAKE2b-256 |
12f609ddab4be919d7a50dbf3cb69e320bac86f9f918861189ad83dd824cb20d
|
Provenance
The following attestation bundles were made for injected-0.3.0.tar.gz:
Publisher:
release.yaml on antonagestam/injected
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
injected-0.3.0.tar.gz -
Subject digest:
8f9bd1c56c20f71fcf8288a198845aecc93649fbe613c9a564a0000b3c606346 - Sigstore transparency entry: 721995857
- Sigstore integration time:
-
Permalink:
antonagestam/injected@ac00acc144b1743d0e5517dcb47f5157880551c5 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/antonagestam
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@ac00acc144b1743d0e5517dcb47f5157880551c5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file injected-0.3.0-py3-none-any.whl.
File metadata
- Download URL: injected-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb5b50ae37adf00211f2cfad31a15d3f70219b6cd4adba9a86bedf3c1aa8b2ac
|
|
| MD5 |
b582017bf19e4aa83f147bbcf9d708c3
|
|
| BLAKE2b-256 |
e3c10174fd435131d0368a90bed2d5886d9b9fdf0e07fe1f976c0ca424853159
|
Provenance
The following attestation bundles were made for injected-0.3.0-py3-none-any.whl:
Publisher:
release.yaml on antonagestam/injected
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
injected-0.3.0-py3-none-any.whl -
Subject digest:
fb5b50ae37adf00211f2cfad31a15d3f70219b6cd4adba9a86bedf3c1aa8b2ac - Sigstore transparency entry: 721995940
- Sigstore integration time:
-
Permalink:
antonagestam/injected@ac00acc144b1743d0e5517dcb47f5157880551c5 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/antonagestam
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@ac00acc144b1743d0e5517dcb47f5157880551c5 -
Trigger Event:
release
-
Statement type: