Skip to main content

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.0.tar.gz (138.1 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.2.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tiny_fastapi_di-0.2.0.tar.gz
  • Upload date:
  • Size: 138.1 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.2.0.tar.gz
Algorithm Hash digest
SHA256 2dd3c76b344bc8f70260acabf892fd9885ce2d0fae29bd0222f60e6a554f9432
MD5 ee71d202477f6fe8c630b97bd4431d37
BLAKE2b-256 1163925d6d5dcb1e3785e17056d666bc9c0cc1ec6b6cc25b7f57b0836d1c8648

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tiny_fastapi_di-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a640c2b362f275e961df3286b2ecbeb10bc098d8c730d577a8fca03469c559e
MD5 c86128ffbbcbe711983a0b17e5805fb8
BLAKE2b-256 00c91d4267dc1d4f4810d095756f38b9fb38ce6bb723d392ed68a7dd7e12ee58

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