Skip to main content

Dependency Injection library

Project description

AnyDI

Modern, lightweight Dependency Injection library using type annotations.

CI Coverage Documentation


Documentation

http://anydi.readthedocs.io/


AnyDI is a modern, lightweight Dependency Injection library suitable for any synchronous or asynchronous applications with Python 3.8+, based on type annotations (PEP 484).

The key features are:

  • Type-safe: Resolves dependencies using type annotations.
  • Async Support: Compatible with both synchronous and asynchronous providers and injections.
  • Scoping: Supports singleton, transient, and request scopes.
  • Easy to Use: Designed for simplicity and minimal boilerplate.
  • Named Dependencies: Supports named dependencies using Annotated type.
  • Resource Management: Manages resources using context managers.
  • Modular: Facilitates a modular design with support for multiple modules.
  • Scanning: Automatically scans for injectable functions and classes.
  • Integrations: Provides easy integration with popular frameworks and libraries.
  • Testing: Simplifies testing by allowing provider overrides.

Installation

pip install anydi

Quick Example

app.py

from anydi import auto, Container

container = Container()


@container.provider(scope="singleton")
def message() -> str:
    return "Hello, world!"


@container.inject
def say_hello(message: str = auto) -> None:
    print(message)


if __name__ == "__main__":
    say_hello()

FastAPI Example

app.py

from fastapi import FastAPI

import anydi.ext.fastapi
from anydi import Container
from anydi.ext.fastapi import Inject

container = Container()


@container.provider(scope="singleton")
def message() -> str:
    return "Hello, World!"


app = FastAPI()


@app.get("/hello")
def say_hello(message: str = Inject()) -> dict[str, str]:
    return {"message": message}


# Install the container into the FastAPI app
anydi.ext.fastapi.install(app, container)

Django Ninja Example

container.py

from anydi import Container


def get_container() -> Container:
    container = Container()

    @container.provider(scope="singleton")
    def message() -> str:
        return "Hello, World!"

    return container

settings.py

INSTALLED_APPS = [
    ...
    "anydi.ext.django",
]

ANYDI = {
    "CONTAINER_FACTORY": "myapp.container.get_container",
    "PATCH_NINJA": True,
}

urls.py

from django.http import HttpRequest
from django.urls import path
from ninja import NinjaAPI

from anydi import auto

api = NinjaAPI()


@api.get("/hello")
def say_hello(request: HttpRequest, message: str = auto) -> dict[str, str]:
    return {"message": message}


urlpatterns = [
    path("api/", api.urls),
]

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

anydi-0.25.1.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

anydi-0.25.1-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file anydi-0.25.1.tar.gz.

File metadata

  • Download URL: anydi-0.25.1.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.2 Darwin/23.2.0

File hashes

Hashes for anydi-0.25.1.tar.gz
Algorithm Hash digest
SHA256 c9878b4a5f46fa0f241599cb939d9ba60fbf90c29162a3258a463e27f05df07c
MD5 4c446cd1f4ee699269e3ca22515299e8
BLAKE2b-256 6b8850e2a292a9297ecf1966511f120f278acc09749e101e054104ec0518f423

See more details on using hashes here.

Provenance

File details

Details for the file anydi-0.25.1-py3-none-any.whl.

File metadata

  • Download URL: anydi-0.25.1-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.2 Darwin/23.2.0

File hashes

Hashes for anydi-0.25.1-py3-none-any.whl
Algorithm Hash digest
SHA256 41431f4cf1f8482ad5a65a8457f24364f728d27a75b2670fe4998c031b97c78a
MD5 86e4dc7bb1d8464978e4637c2f8f8993
BLAKE2b-256 f71370197c01e94d1c3cbd24ee377c82e5f69c16bce926fd297cfd9b45a86048

See more details on using hashes here.

Provenance

Supported by

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