Production-ready async dependency injection. FastAPI-compatible patterns in ~220 lines.
Project description
tiny-fastapi-di
Production-ready async dependency injection in ~220 lines. FastAPI-compatible patterns, minimal code, no hidden complexity.
Installation
pip install tiny-fastapi-di
# With Pydantic validation
pip install tiny-fastapi-di[pydantic]
Quick Start
from tiny_fastapi_di import Depends, empty_di_ctx
def get_db():
return "database_connection"
async def get_user(db: str = Depends(get_db)):
return f"User from {db}"
# One call handles context and cleanup
result = await empty_di_ctx.call_fn(get_user)
Why tiny-fastapi-di?
- Production-ready: ~220 lines of auditable code. No hidden complexity.
- Familiar API: Same
Depends()pattern as FastAPI - Framework-agnostic: Use in CLI tools, workers, pipelines, anywhere
- Correct by default: Cleanup runs automatically, circular dependencies detected
Core Features
from typing import Annotated
from tiny_fastapi_di import Depends, empty_di_ctx
# Basic dependency
async def endpoint(db = Depends(get_db)): ...
# Annotated syntax
async def endpoint(db: Annotated[DB, Depends(get_db)]): ...
# Infer callable from type
async def endpoint(db: Annotated[DB, Depends()]): ...
# Disable caching
async def endpoint(db = Depends(get_db, use_cache=False)): ...
# Yield dependencies with automatic cleanup
def get_db():
db = connect()
try:
yield db
finally:
db.close()
Value Injection
result = await empty_di_ctx.call_fn(my_endpoint, request_id=123, user_id=456)
Dependency Substitution (Testing)
result = await empty_di_ctx.call_fn(
my_endpoint,
fn_map={real_db: mock_db}
)
Pydantic Validation
from tiny_fastapi_di.pydantic import pydantic_di_ctx
async def endpoint(user: User): # User is a Pydantic model
return user
result = await pydantic_di_ctx.call_fn(endpoint, user={"name": "Alice", "age": 30})
Feature Comparison
| Feature | FastAPI | tiny-fastapi-di |
|---|---|---|
Depends() |
✅ | ✅ |
Depends(use_cache=False) |
✅ | ✅ |
Annotated[T, Depends()] |
✅ | ✅ |
yield dependencies |
✅ | ✅ |
| Async dependencies | ✅ | ✅ |
| Circular detection | ❌ | ✅ |
| Value injection | ❌ | ✅ |
| Dependency substitution | partial | ✅ |
| Optional validation | ❌ | ✅ |
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
tiny_fastapi_di-0.2.1.tar.gz
(140.5 kB
view details)
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 tiny_fastapi_di-0.2.1.tar.gz.
File metadata
- Download URL: tiny_fastapi_di-0.2.1.tar.gz
- Upload date:
- Size: 140.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Pop!_OS","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
429bd24341396cb95937af9721ca952571a30141578f913f2d28c16d6fca088d
|
|
| MD5 |
a4f0b49c89c12896fed1b99270116bed
|
|
| BLAKE2b-256 |
92da4d49c4f5ca63d14abba078a457b8a0d2729f366f076427d32e8393acbd1f
|
File details
Details for the file tiny_fastapi_di-0.2.1-py3-none-any.whl.
File metadata
- Download URL: tiny_fastapi_di-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Pop!_OS","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
522a3bf7d66ad4162a301dd623363ed9bb84375cd69c4fbbe246aa0f30393a19
|
|
| MD5 |
bf38e0859864764e267e4b605c167c6e
|
|
| BLAKE2b-256 |
0bef7fc485113f8552397b69fee38df74a69aa268c1e6d01987b5229ca64020a
|