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.2.tar.gz (74.3 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.2-cp39-abi3-musllinux_1_2_x86_64.whl (363.3 kB view details)

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

fastdi_core-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (308.3 kB view details)

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

fastdi_core-0.1.2-cp39-abi3-macosx_10_13_universal2.whl (534.9 kB view details)

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

File details

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

File metadata

  • Download URL: fastdi_core-0.1.2.tar.gz
  • Upload date:
  • Size: 74.3 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.2.tar.gz
Algorithm Hash digest
SHA256 ca5c3e6264e567704a3ead709beab38a6dd5ce93bb242f277e3d3310b670dde8
MD5 06e115107b0cddb729d827bfcdbd78be
BLAKE2b-256 b34ab8fbc4bddfab68e6ce9b08b6e469a19d2d0353af73d08ad3bc7368217ae3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastdi_core-0.1.2-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9a3301bf6e863733d4ada9db6cce216dc635eba91ae27c0466a01e469de697c9
MD5 ab33b1d2cd531828d38f7a7791f7a9fa
BLAKE2b-256 e484c35799f5471361bd8c657a0f73ca62daffe4241f0969466a204cc6c87df7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastdi_core-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3f64fc859a784a4a52f97d61d8c21de263397a33b0310c986db6c0231714b4a4
MD5 f82d22c318abdcac95136335b3caf713
BLAKE2b-256 5a19d01dfa13df5ccf17cc2465878e6d5bb8432b974ac95614722792047c7552

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastdi_core-0.1.2-cp39-abi3-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 504acf51172eead29dc5bd05379361ae577e143c295f640e8d152a08e95268c3
MD5 45604df039b79da14823466050cca7f1
BLAKE2b-256 0f812746d29993e31252d41aa82db11d59ca9d7603fcc3e1698a9e374f268f1d

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