Minimal async dependency injection, FastAPI-compatible in ~160 lines
Project description
tiny-fastapi-di
Minimal async dependency injection in ~160 lines. FastAPI-compatible patterns without the framework.
Installation
pip install tiny-fastapi-di
# With Pydantic validation support
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}"
async def main():
async with empty_di_ctx.with_maps() as ctx:
user = await ctx.call_fn(get_user)
print(user) # "User from database_connection"
Features
All FastAPI DI patterns work:
from typing import Annotated
from tiny_fastapi_di import Depends, Security, empty_di_ctx
# Basic dependency
async def endpoint(db = Depends(get_db)): ...
# Annotated syntax
async def endpoint(db: Annotated[DB, Depends(get_db)]): ...
# Infer from type annotation
async def endpoint(db: Annotated[DB, Depends()]): ...
# Disable caching
async def endpoint(db = Depends(get_db, use_cache=False)): ...
# Security (for OpenAPI metadata)
async def endpoint(user = Security(get_user, scopes=["read"])): ...
# Yield dependencies (cleanup)
def get_db():
db = connect()
try:
yield db
finally:
db.close()
Value Injection
Inject values by parameter name:
ctx = empty_di_ctx.with_maps(request_id=123, user_id=456)
result = await ctx.call_fn(my_endpoint)
Dependency Substitution (Testing)
ctx = empty_di_ctx.with_maps(fn_map={real_db: mock_db})
result = await ctx.call_fn(my_endpoint) # Uses 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
ctx = pydantic_di_ctx.with_maps(user={"name": "Alice", "age": 30})
result = await ctx.call_fn(endpoint) # Returns User instance
Feature Comparison with FastAPI
| Feature | FastAPI | tiny-fastapi-di |
|---|---|---|
Depends() |
✅ | ✅ |
Depends(use_cache=False) |
✅ | ✅ |
Annotated[T, Depends()] |
✅ | ✅ |
yield dependencies |
✅ | ✅ |
| Async dependencies | ✅ | ✅ |
Security(scopes=[...]) |
✅ | ✅ |
| Circular detection | ❌ | ✅ |
| Value injection by name | ❌ | ✅ |
| 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.1.0.tar.gz
(37.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.1.0.tar.gz.
File metadata
- Download URL: tiny_fastapi_di-0.1.0.tar.gz
- Upload date:
- Size: 37.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 |
df1a441803fa3c753f6d5e983f26b9062e1f88a24eccf2c344792bab9d6bf2b8
|
|
| MD5 |
771d70573a8c34e7edb7789b9dfee6bf
|
|
| BLAKE2b-256 |
b353cbea69fc8650ad1f4ca91d1074e75d1037df166100926cdbdff83cb0f1c7
|
File details
Details for the file tiny_fastapi_di-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tiny_fastapi_di-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 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 |
83f7f5a1ab693184299f7fe7a26bc54c07e3e2dfc180e7edad3297da447cc5f7
|
|
| MD5 |
9ce26355b857cfdd137eff8222cf95ee
|
|
| BLAKE2b-256 |
9dacf9307eb74be6fd0e7264410519968587b7e462d5cc47acd2704a6782ff1f
|