# 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 typing import Annotated
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: Annotated[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": null,
"name": "anydi",
"maintainer": null,
"docs_url": null,
"requires_python": "~=3.9",
"maintainer_email": null,
"keywords": "dependency injection, dependencies, di, async, asyncio, application",
"author": "Anton Ruhlov",
"author_email": "Anton Ruhlov <antonruhlov@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/59/4b/722ad94b21a09549fb3544e2d04b3a6d37de2e75d4b1dcdde920d161e9cb/anydi-0.48.1.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 typing import Annotated\n\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: Annotated[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": null,
"summary": "Dependency Injection library",
"version": "0.48.1",
"project_urls": {
"Repository": "https://github.com/antonrh/anydi"
},
"split_keywords": [
"dependency injection",
" dependencies",
" di",
" async",
" asyncio",
" application"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "2fdaa3221cd76eb3194727d5f1d95ea9803c62cd7838529911c3711c725b4dde",
"md5": "ab5a287c8e16ba2c93b08b816dc86c35",
"sha256": "7681d1d4231272c97609cc6d7228335b0dfa558e4def99c730387aaebde52bc7"
},
"downloads": -1,
"filename": "anydi-0.48.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ab5a287c8e16ba2c93b08b816dc86c35",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "~=3.9",
"size": 30554,
"upload_time": "2025-09-12T06:38:06",
"upload_time_iso_8601": "2025-09-12T06:38:06.496715Z",
"url": "https://files.pythonhosted.org/packages/2f/da/a3221cd76eb3194727d5f1d95ea9803c62cd7838529911c3711c725b4dde/anydi-0.48.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "594b722ad94b21a09549fb3544e2d04b3a6d37de2e75d4b1dcdde920d161e9cb",
"md5": "f0eed77fdfa39bfa0090df0ebf44a23e",
"sha256": "e14be14447ee7dbac2b5174d132451c0893c35ec264b7b8a2b3abed4cb046c56"
},
"downloads": -1,
"filename": "anydi-0.48.1.tar.gz",
"has_sig": false,
"md5_digest": "f0eed77fdfa39bfa0090df0ebf44a23e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "~=3.9",
"size": 21912,
"upload_time": "2025-09-12T06:38:08",
"upload_time_iso_8601": "2025-09-12T06:38:08.572082Z",
"url": "https://files.pythonhosted.org/packages/59/4b/722ad94b21a09549fb3544e2d04b3a6d37de2e75d4b1dcdde920d161e9cb/anydi-0.48.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-09-12 06:38:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "antonrh",
"github_project": "anydi",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "anydi"
}