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.3.tar.gz (71.8 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.3-cp39-abi3-musllinux_1_2_x86_64.whl (362.7 kB view details)

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

fastdi_core-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (308.1 kB view details)

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

fastdi_core-0.1.3-cp39-abi3-macosx_10_13_universal2.whl (534.4 kB view details)

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

File details

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

File metadata

  • Download URL: fastdi_core-0.1.3.tar.gz
  • Upload date:
  • Size: 71.8 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.3.tar.gz
Algorithm Hash digest
SHA256 7b079af868bc4303b47d0c8d8b2fac21eeee810d8a65be1f9ae1b0885f4f8fc0
MD5 09faa348f4cfa7055eb1754da577d070
BLAKE2b-256 f842e418e194f9751f2bcfe3440754e98c697ea3c43eb8adb81d3d560bededd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastdi_core-0.1.3-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ad3509a72dba8eedd172ca8732ba980a1e8fe37f9670c598e253bc08d4979e9d
MD5 c19afc3c3c861bcb9562a4308c72ae0a
BLAKE2b-256 b361d870a2f60a2f1baef8f72cc7add0ec78203867bb17102428d021b2d3353e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastdi_core-0.1.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1925dc30dca6e2d40ad3e4d5c0daee7f53906da97ccb14d49c04f522069127ca
MD5 930fb13abb6d3e641413f3b15a8f85fd
BLAKE2b-256 7da1abf2111986de121cdca0e5750a5bf5944b6792a30db97679dc190be0fc0a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastdi_core-0.1.3-cp39-abi3-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 7d7a483994cfd518cd97d5868ab883d661a69b781223af86df3c9a8777b25b23
MD5 c13aedbfdb2044f97c5b7d3e9517789e
BLAKE2b-256 c06d896f52a42a255ba29f26e0a3aae2585b9ae3e9436f9c3001e4af72123290

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