Skip to main content

FastDI — Rust-powered Dependency Injection for Python

Project description

FastDI — Rust-powered Dependency Injection for Python

FastDI wraps a Rust core (PyO3) in a friendly Python API so you can wire services quickly without sacrificing performance. It supports sync and async providers, request-scoped caches, layered overrides, and observability hooks.

Project homepage: https://aliev.me/fastdi

Highlights

  • Rust-backed plan compilation with cycle detection
  • Sync (@inject) and async (@ainject) decorators with minimal boilerplate
  • Caching scopes: transient, singleton, and per-request (async task)
  • Overrides for tests and temporary wiring changes
  • Hooks that report provider timings and cache hits

Requirements

  • Python 3.9+
  • Rust toolchain (stable)

Quick Start

pip install fastdi-core
# or set up a dev env with uv
uv venv .venv
. .venv/bin/activate
uv sync --dev
uv run maturin develop -r -q
uv run python -m examples.basic

Minimal Usage

from typing import Annotated, Protocol
from fastdi import Container, Depends, provide, inject

container = Container()

class Service(Protocol):
    def ping(self) -> dict: ...

@provide(container, singleton=True)
def get_db() -> dict:
    return {"db": "connection"}

@provide(container)
def get_service(db: Annotated[dict, Depends(get_db)]) -> Service:
    class ServiceImpl:
        def __init__(self, db):
            self._db = db
        def ping(self) -> dict:
            return {"ok": True, "via": self._db["db"]}
    return ServiceImpl(db)

@inject(container)
def handler(service: Annotated[Service, Depends(get_service)]):
    return service.ping()

print(handler())

Async + Request Scope

import asyncio
from typing import Annotated
from fastdi import Container, Depends, provide, ainject

container = Container()

@provide(container, scope="request")
async def request_id() -> object:
    return object()

@ainject(container)
async def within_task(
    first: Annotated[object, Depends(request_id)],
    second: Annotated[object, Depends(request_id)],
) -> bool:
    return first is second

print(asyncio.run(within_task()))  # True: value is cached within a task

Observability Hooks

from fastdi import Container

container = Container()
container.add_hook(lambda event, payload: print(event, payload))

Development Tasks

uv run python -m pytest -q    # tests
uv run ruff check .           # lint
uv run mypy .                 # type check
uv run python -m mkdocs serve # docs preview

Documentation

Full guides live under docs/ and the published site (see mkdocs.yml). Key entries:

  • Getting started walkthrough
  • Usage guide covering providers, scopes, and overrides
  • Observability hooks and performance notes

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

fastdi_core-0.1.4.tar.gz (72.2 kB view details)

Uploaded Source

Built Distributions

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

fastdi_core-0.1.4-cp39-abi3-musllinux_1_2_x86_64.whl (362.9 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

fastdi_core-0.1.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (308.0 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

fastdi_core-0.1.4-cp39-abi3-macosx_10_13_universal2.whl (534.5 kB view details)

Uploaded CPython 3.9+macOS 10.13+ universal2 (ARM64, x86-64)

File details

Details for the file fastdi_core-0.1.4.tar.gz.

File metadata

  • Download URL: fastdi_core-0.1.4.tar.gz
  • Upload date:
  • Size: 72.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastdi_core-0.1.4.tar.gz
Algorithm Hash digest
SHA256 fa29864bf410f77a0ead6f707464183c42b31fac209d268beca733bc46bba8b3
MD5 efbf2d7c92a1d6b2406fdb783ccfda46
BLAKE2b-256 09c0fff4bdcab272a6a4945335f310fd94f096b4ba431a3e0a52b967455f9047

See more details on using hashes here.

File details

Details for the file fastdi_core-0.1.4-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for fastdi_core-0.1.4-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d7d3730172239df64c60582d9056cef47dbc39a8c5ccfba14c9843ae79590dea
MD5 cb0a07c3b1a895da96338a5586a28ab1
BLAKE2b-256 43950728303a442aee8380e0d0bc937c826832ec21eabca63419112744b101bd

See more details on using hashes here.

File details

Details for the file fastdi_core-0.1.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastdi_core-0.1.4-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fdadc2b16201b0ec0069c2341fb118e71ebf722b91974cd21ae2983656d3be96
MD5 6834d02628a42ea75a2daa154eb3014e
BLAKE2b-256 06f2b055e8b09c035dc14bd01b558d91a8de5b6b8aba3fee665c1e6e311fe1d2

See more details on using hashes here.

File details

Details for the file fastdi_core-0.1.4-cp39-abi3-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for fastdi_core-0.1.4-cp39-abi3-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 388f129110e9394369c24555f6471daec0e3fc484683db815acf67c872dc418b
MD5 c2ab90245dde2970d4c0043f9d346251
BLAKE2b-256 d6aafc092bea0be8dea00974f28ca1b4db2caa1bec110e4fba7c0221ba4d64be

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