Dependency Injection library
Project description
AnyDI
Modern, lightweight Dependency Injection library using type annotations.
Documentation
AnyDI
is a modern, lightweight Dependency Injection library suitable for any synchronous or asynchronous applications with Python 3.9+, 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
Release history Release notifications | RSS feed
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.32.0.tar.gz
(22.1 kB
view details)
Built Distribution
anydi-0.32.0-py3-none-any.whl
(30.0 kB
view details)
File details
Details for the file anydi-0.32.0.tar.gz
.
File metadata
- Download URL: anydi-0.32.0.tar.gz
- Upload date:
- Size: 22.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.2 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29860bbffe9424cdf497ffc95c8cf87f3711e09002299ec1403695c5bf6e46ff |
|
MD5 | fc7327c01ded92f1fc6476ef15819ffd |
|
BLAKE2b-256 | 40f7c1dd9093a4471d09a4a8e07a5316d1747a33a4d997d4c74aace270a1c0e0 |
Provenance
File details
Details for the file anydi-0.32.0-py3-none-any.whl
.
File metadata
- Download URL: anydi-0.32.0-py3-none-any.whl
- Upload date:
- Size: 30.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.2 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea48d209926dcb0600d52a646a866f4405755de4756e2bc29a38f596397fbc2e |
|
MD5 | a57007314d30d9aa8c3e953bfe68de9c |
|
BLAKE2b-256 | 7a98eae8034628dd6ee506ca1481408dfd462bfe03636903c1a08ce1cc113662 |