Skip to main content

⚠️ This project is no longer maintained

This package will not receive future updates. It remains usable as-is.

The author's current dependency injection work is in semifun, which is a different library with a broader scope — not a drop-in replacement.


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

Metadata

Release files for tiny-fastapi-di 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 tiny-fastapi-di 0.2.2
File Size Uploaded Provenance Actions
tiny_fastapi_di-0.2.2.tar.gz 65.3 kB Without provenance Details for tiny_fastapi_di-0.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tiny-fastapi-di 0.2.2
File Interpreter ABI Platform Actions
tiny_fastapi_di-0.2.2-py3-none-any.whl Python 3 none any Details for tiny_fastapi_di-0.2.2-py3-none-any.whl

Total release size: 72.5 kB

Release files / tiny_fastapi_di-0.2.2.tar.gz

Download URL tiny_fastapi_di-0.2.2.tar.gz
Size 65.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d3a67cfa1ce687021d482198366ab6a1b626be0dbdc312df94bcd56f24d2e1d5
BLAKE2b-256 checksum
How to use checksums
fa1fd0751c200dea330aa6001ef66d8a1484b7e3ddb4d180f4e597a5d9fd84ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

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

Download URL tiny_fastapi_di-0.2.2-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
888b150c46863ae449e5bcc0695e079aa73a75d6a4894d56835fed8d6655d006
BLAKE2b-256 checksum
How to use checksums
05726b6dc79f913f2cefe234a6aed316c047de78c7f001d7a93e23416793ffa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Security for tiny-fastapi-di 0.2.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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