Yet another library to reuse fastapi dependency injection
Project description
fastapi-injected
Yet another attempt to reuse FastAPI's dependency injection outside of request handlers.
This is an opinionated library: it takes the DI machinery you already know from FastAPI (Depends, generator dependencies with teardown, dependency caching) and makes it usable in plain async functions — background jobs, CLI commands, workers, scripts — without a Request in sight.
Installation
pip install fastapi-injected
Requires Python 3.12+.
Usage
Declare dependencies as regular classes and annotate fields with Dep[...]:
from dataclasses import dataclass
from typing import AsyncIterator
from fastapi_injected import Dep, DepFactory, Inejected, inject
@dataclass
class Session:
closed: bool = False
async def session_dep() -> AsyncIterator[Session]:
session = Session()
try:
yield session
finally:
session.closed = True
@dataclass
class Repository:
session: DepFactory[Session, session_dep]
@dataclass
class Service:
repo: Dep[Repository]
@inject
async def handler(*, service: Dep[Service] = Inejected) -> None:
... # service is built and injected, session is closed on exit
await handler()
Dep[T]— resolveTby calling it, same as FastAPI'sAnnotated[T, Depends()].DepFactory[T, factory]— resolveTvia a factory, same asAnnotated[T, Depends(factory)]. Generator factories get proper teardown.Inejected— a sentinel default that exists purely to make type checkers happy: without it they would complain about a missing argument at call sites. At runtime the parameter is always filled in by@inject.
Injected parameters mix freely with regular ones — pass your own arguments as usual and the rest is injected:
@inject
async def add(a: int, b: int, *, service: Dep[Service] = Inejected) -> int:
...
result = await add(1, 2)
Solving a type directly
No decorator needed — resolve a dependency graph on demand:
from fastapi_injected import solve
service = await solve(Service)
Scopes and caching
By default every call to an injected function gets its own scope: dependencies are built, cached within the call, and torn down when it returns. Wrap several calls in push_inject_scope() to share one cache (and defer teardown to the end of the scope):
from fastapi_injected import push_inject_scope
async with push_inject_scope():
a = await handler() # dependencies built here
b = await handler() # same instances reused
# generator dependencies are torn down here
Use @inject(new_scope=True) to opt a function out of the surrounding scope and always get fresh dependencies.
License
MIT
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
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 fastapi_injected-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_injected-0.1.0.tar.gz
- Upload date:
- Size: 36.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13cfb8db9449ed8166c60f6ea33aa26da6e800902efcc50e38c80ff943fd4159
|
|
| MD5 |
866ec96c7e91fbadc9a760e23fbf57a6
|
|
| BLAKE2b-256 |
71b1d132249bfffaf9f50a9cff2ba935f2b1f9230b0de7f175e86ca00dc313e9
|
File details
Details for the file fastapi_injected-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_injected-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce6b5c2fc0f8b53d7646901e38c88f315f15ec13649b92df64dc80f183f5cca9
|
|
| MD5 |
0a9234f3982fc788af949eb55bf65522
|
|
| BLAKE2b-256 |
b718c40e1e9d112d87e9489ac5d827632b2583eb92278a55dc87cfe3e70b8730
|