# AnyDI
<p align="center">
<i>Modern, lightweight Dependency Injection library using type annotations.</i>
</p>
<p align="center">
<a href="https://github.com/antonrh/anydi/actions/workflows/ci.yml" target="_blank">
<img src="https://github.com/antonrh/anydi/actions/workflows/ci.yml/badge.svg" alt="CI">
</a>
<a href="https://codecov.io/gh/antonrh/anydi" target="_blank">
<img src="https://codecov.io/gh/antonrh/anydi/branch/main/graph/badge.svg?token=67CLD19I0C" alt="Coverage">
</a>
<a href="https://anydi.readthedocs.io/en/latest/?badge=latest" target="_blank">
<img src="https://readthedocs.org/projects/anydi/badge/?version=latest" alt="Documentation">
</a>
</p>
---
Documentation
http://anydi.readthedocs.io/
---
`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](https://peps.python.org/pep-0484/)).
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
```shell
pip install anydi
```
## Quick Example
*app.py*
```python
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*
```python
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*
```python
from anydi import Container
def get_container() -> Container:
container = Container()
@container.provider(scope="singleton")
def message() -> str:
return "Hello, World!"
return container
```
*settings.py*
```python
INSTALLED_APPS = [
...
"anydi.ext.django",
]
ANYDI = {
"CONTAINER_FACTORY": "myapp.container.get_container",
"PATCH_NINJA": True,
}
```
*urls.py*
```python
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),
]
```
Raw data
{
"_id": null,
"home_page": "https://github.com/antonrh/anydi",
"name": "anydi",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "dependency injection, dependencies, di, async, asyncio, application",
"author": "Anton Ruhlov",
"author_email": "antonruhlov@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/a7/3e/7fc7a50f0d6d6f7470b7116c67bfd353dcef7f4857096e3792bca8ce9430/anydi-0.32.2.tar.gz",
"platform": null,
"description": "# AnyDI\n\n<p align=\"center\">\n <i>Modern, lightweight Dependency Injection library using type annotations.</i>\n</p>\n\n<p align=\"center\">\n <a href=\"https://github.com/antonrh/anydi/actions/workflows/ci.yml\" target=\"_blank\">\n <img src=\"https://github.com/antonrh/anydi/actions/workflows/ci.yml/badge.svg\" alt=\"CI\">\n </a>\n <a href=\"https://codecov.io/gh/antonrh/anydi\" target=\"_blank\">\n <img src=\"https://codecov.io/gh/antonrh/anydi/branch/main/graph/badge.svg?token=67CLD19I0C\" alt=\"Coverage\">\n </a>\n <a href=\"https://anydi.readthedocs.io/en/latest/?badge=latest\" target=\"_blank\">\n <img src=\"https://readthedocs.org/projects/anydi/badge/?version=latest\" alt=\"Documentation\">\n </a>\n</p>\n\n---\nDocumentation\n\nhttp://anydi.readthedocs.io/\n\n---\n\n`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](https://peps.python.org/pep-0484/)).\n\nThe key features are:\n\n* **Type-safe**: Resolves dependencies using type annotations.\n* **Async Support**: Compatible with both synchronous and asynchronous providers and injections.\n* **Scoping**: Supports singleton, transient, and request scopes.\n* **Easy to Use**: Designed for simplicity and minimal boilerplate.\n* **Named Dependencies**: Supports named dependencies using `Annotated` type.\n* **Resource Management**: Manages resources using context managers.\n* **Modular: Facilitates** a modular design with support for multiple modules.\n* **Scanning**: Automatically scans for injectable functions and classes.\n* **Integrations**: Provides easy integration with popular frameworks and libraries.\n* **Testing**: Simplifies testing by allowing provider overrides.\n\n## Installation\n\n```shell\npip install anydi\n```\n\n## Quick Example\n\n*app.py*\n\n```python\nfrom anydi import auto, Container\n\ncontainer = Container()\n\n\n@container.provider(scope=\"singleton\")\ndef message() -> str:\n return \"Hello, world!\"\n\n\n@container.inject\ndef say_hello(message: str = auto) -> None:\n print(message)\n\n\nif __name__ == \"__main__\":\n say_hello()\n```\n\n## FastAPI Example\n\n*app.py*\n\n```python\nfrom fastapi import FastAPI\n\nimport anydi.ext.fastapi\nfrom anydi import Container\nfrom anydi.ext.fastapi import Inject\n\ncontainer = Container()\n\n\n@container.provider(scope=\"singleton\")\ndef message() -> str:\n return \"Hello, World!\"\n\n\napp = FastAPI()\n\n\n@app.get(\"/hello\")\ndef say_hello(message: str = Inject()) -> dict[str, str]:\n return {\"message\": message}\n\n\n# Install the container into the FastAPI app\nanydi.ext.fastapi.install(app, container)\n```\n\n\n\n## Django Ninja Example\n\n*container.py*\n\n```python\nfrom anydi import Container\n\n\ndef get_container() -> Container:\n container = Container()\n\n @container.provider(scope=\"singleton\")\n def message() -> str:\n return \"Hello, World!\"\n\n return container\n```\n\n*settings.py*\n\n```python\nINSTALLED_APPS = [\n ...\n \"anydi.ext.django\",\n]\n\nANYDI = {\n \"CONTAINER_FACTORY\": \"myapp.container.get_container\",\n \"PATCH_NINJA\": True,\n}\n```\n\n*urls.py*\n\n```python\nfrom django.http import HttpRequest\nfrom django.urls import path\nfrom ninja import NinjaAPI\n\nfrom anydi import auto\n\napi = NinjaAPI()\n\n\n@api.get(\"/hello\")\ndef say_hello(request: HttpRequest, message: str = auto) -> dict[str, str]:\n return {\"message\": message}\n\n\nurlpatterns = [\n path(\"api/\", api.urls),\n]\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Dependency Injection library",
"version": "0.32.2",
"project_urls": {
"Homepage": "https://github.com/antonrh/anydi",
"Repository": "https://github.com/antonrh/anydi"
},
"split_keywords": [
"dependency injection",
" dependencies",
" di",
" async",
" asyncio",
" application"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e80c334ba719c3920d79878664975ca9eaa15118096f25ce5aa1cc2550676ccc",
"md5": "facc51d7c50caa1b9e7353b2dc653cf3",
"sha256": "2384a69f0bef5f1b1a9b0fec315b55a5f54317683885cd2362e68315f892a11c"
},
"downloads": -1,
"filename": "anydi-0.32.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "facc51d7c50caa1b9e7353b2dc653cf3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 30150,
"upload_time": "2024-11-11T10:56:13",
"upload_time_iso_8601": "2024-11-11T10:56:13.990031Z",
"url": "https://files.pythonhosted.org/packages/e8/0c/334ba719c3920d79878664975ca9eaa15118096f25ce5aa1cc2550676ccc/anydi-0.32.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a73e7fc7a50f0d6d6f7470b7116c67bfd353dcef7f4857096e3792bca8ce9430",
"md5": "6952e685ad5f7aa27083613787e7b9b6",
"sha256": "dfb5c6e174198340b7a6d691197434f3343570da3a2ca6a605ebf5493988c66f"
},
"downloads": -1,
"filename": "anydi-0.32.2.tar.gz",
"has_sig": false,
"md5_digest": "6952e685ad5f7aa27083613787e7b9b6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 22270,
"upload_time": "2024-11-11T10:56:15",
"upload_time_iso_8601": "2024-11-11T10:56:15.281983Z",
"url": "https://files.pythonhosted.org/packages/a7/3e/7fc7a50f0d6d6f7470b7116c67bfd353dcef7f4857096e3792bca8ce9430/anydi-0.32.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-11 10:56:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "antonrh",
"github_project": "anydi",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "anydi"
}