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.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 Container, dep
container = Container()
@container.provider(scope="singleton")
def message() -> str:
return "Hello, world!"
@container.inject
def say_hello(message: str = dep()) -> 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)
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.24.0.tar.gz
(17.7 kB
view hashes)
Built Distribution
anydi-0.24.0-py3-none-any.whl
(21.1 kB
view hashes)