Skip to main content

Production-ready async dependency injection. FastAPI-compatible patterns in ~190 lines.

Project description

tiny-fastapi-di

Production-ready async dependency injection in ~190 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: ~190 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.1.2.tar.gz (130.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tiny_fastapi_di-0.1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file tiny_fastapi_di-0.1.2.tar.gz.

File metadata

  • Download URL: tiny_fastapi_di-0.1.2.tar.gz
  • Upload date:
  • Size: 130.2 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

Hashes for tiny_fastapi_di-0.1.2.tar.gz
Algorithm Hash digest
SHA256 57c10884688ee084590700d812fe708f4cb248d4c3a85ab574886a68a3cbb558
MD5 ff9345cf9ccc52722ef1b8ce7cf10f3d
BLAKE2b-256 e0dd6b657794accd334e65245b14eb7f870ffd75bee12f151fcc5d014a95c984

See more details on using hashes here.

File details

Details for the file tiny_fastapi_di-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tiny_fastapi_di-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 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

Hashes for tiny_fastapi_di-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6dffc1df0b33747ccda7dc0e426fbb9978997c97d9e6eebdf4693a83ea247ae0
MD5 4bb6a12b0fe577c88624327d578cdf21
BLAKE2b-256 483cf655c3dbbd5c816f07e08ff54cfef9847fbfe03f04113a543bed963624a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page