# 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.8+, 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.8",
"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/58/b5/f97f69e913600c9f9fc3f7dbbb4f9b078ea12394f89df7a8b114ecdea815/anydi-0.25.0.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.8+, 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.25.0",
"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": "579b6d797a9fccd8405fc4b7076a63f4dd55c5de0f8ebd0b0371ce725a6dbfcb",
"md5": "4d8e3909a73e4954261e946b6a6ce47e",
"sha256": "4df4bd706ef46a63c5ba1d643ec575b6a4e0b93b2ca1b11403b0db3e79b0ac99"
},
"downloads": -1,
"filename": "anydi-0.25.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4d8e3909a73e4954261e946b6a6ce47e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 27655,
"upload_time": "2024-05-08T13:40:40",
"upload_time_iso_8601": "2024-05-08T13:40:40.149901Z",
"url": "https://files.pythonhosted.org/packages/57/9b/6d797a9fccd8405fc4b7076a63f4dd55c5de0f8ebd0b0371ce725a6dbfcb/anydi-0.25.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "58b5f97f69e913600c9f9fc3f7dbbb4f9b078ea12394f89df7a8b114ecdea815",
"md5": "dc064b29b9604f3b5543b3e9941c3f6f",
"sha256": "2efd34f53b57f94ba78555073a9d2d8d80c2c8288b5fd2920f3521f46bea3743"
},
"downloads": -1,
"filename": "anydi-0.25.0.tar.gz",
"has_sig": false,
"md5_digest": "dc064b29b9604f3b5543b3e9941c3f6f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 21855,
"upload_time": "2024-05-08T13:40:42",
"upload_time_iso_8601": "2024-05-08T13:40:42.447284Z",
"url": "https://files.pythonhosted.org/packages/58/b5/f97f69e913600c9f9fc3f7dbbb4f9b078ea12394f89df7a8b114ecdea815/anydi-0.25.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-08 13:40:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "antonrh",
"github_project": "anydi",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "anydi"
}