Name | deps-injection JSON |
Version |
1.8.0
JSON |
| download |
home_page | None |
Summary | Easy dependency injection without wiring |
upload_time | 2025-02-09 12:44:22 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | MIT |
keywords |
dependency
di
injection
injector
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Injection



[](https://github.com/nightblure/injection/actions/workflows/publish.yml)
[](https://injection.readthedocs.io/en/latest/?badge=latest)
[](https://github.com/nightblure/injection/actions/workflows/ci.yml)
[](https://codecov.io/gh/nightblure/injection)
[](https://github.com/astral-sh/ruff)
[](https://github.com/pypa/hatch)
[](https://github.com/astral-sh/uv)
[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
[](https://codeclimate.com/github/nightblure/injection/maintainability)



---
Easy dependency injection for all, works with Python 3.8-3.13. Main features and advantages:
* support **Python 3.8-3.13**;
* works with **FastAPI, **Litestar**, Flask** and **Django REST Framework**;
* support **dependency** **injection** via `Annotated` in `FastAPI`;
* support **async injections**;
* support [**auto injection by types**](https://injection.readthedocs.io/latest/injection/auto_injection.html);
* [**resources**](https://injection.readthedocs.io/latest/providers/resource.html) with **function scope**;
* no **wiring**;
* **overriding** dependencies for testing;
* **100%** code coverage;
* the code is fully **typed** and checked with [mypy](https://github.com/python/mypy);
* good [documentation](https://injection.readthedocs.io/latest/);
* intuitive and almost identical api with [dependency-injector](https://github.com/ets-labs/python-dependency-injector),
which will allow you to easily migrate to injection
(see [migration from dependency injector](https://injection.readthedocs.io/latest/dev/migration-from-dependency-injector.html));
---
## Installation
```shell
pip install deps-injection
```
## Compatibility between web frameworks and injection features
| Framework | Dependency injection with @inject | Overriding providers | Dependency injection with @autoinject |
|--------------------------------------------------------------------------|:---------------------------------:|:--------------------:|:-------------------------------------------:|
| [FastAPI](https://github.com/fastapi/fastapi) | ✅ | ✅ | ➖ |
| [Flask](https://github.com/pallets/flask) | ✅ | ✅ | ✅ |
| [Django REST Framework](https://github.com/encode/django-rest-framework) | ✅ | ✅ | ✅ |
| [Litestar](https://github.com/litestar-org/litestar) | ✅ | ✅ | ➖ | ➖ |
## Quickstart with FastAPI, SQLAlchemy and pytest
```python3
from contextlib import contextmanager
from random import Random
from typing import Annotated, Any, Callable, Dict, Iterator
import pytest
from fastapi import Depends, FastAPI
from sqlalchemy import create_engine, text
from sqlalchemy.orm import Session, sessionmaker
from starlette.testclient import TestClient
from injection import DeclarativeContainer, Provide, inject, providers
@contextmanager
def db_session_resource(session_factory: Callable[..., Session]) -> Iterator[Session]:
session = session_factory()
try:
yield session
except Exception:
session.rollback()
finally:
session.close()
class SomeDAO:
def __init__(self, db_session: Session) -> None:
self.db_session = db_session
def get_some_data(self, num: int) -> int:
stmt = text("SELECT :num").bindparams(num=num)
data: int = self.db_session.execute(stmt).scalar_one()
return data
class DIContainer(DeclarativeContainer):
db_engine = providers.Singleton(
create_engine,
url="sqlite:///db.db",
pool_size=20,
max_overflow=0,
pool_pre_ping=False,
)
session_factory = providers.Singleton(
sessionmaker,
db_engine.cast,
autoflush=False,
autocommit=False,
)
db_session = providers.Resource(
db_session_resource,
session_factory=session_factory.cast,
function_scope=True,
)
some_dao = providers.Factory(SomeDAO, db_session=db_session.cast)
SomeDAODependency = Annotated[SomeDAO, Depends(Provide[DIContainer.some_dao])]
app = FastAPI()
@app.get("/values/{value}")
@inject
async def sqla_resource_handler_async(
value: int,
some_dao: SomeDAODependency,
) -> Dict[str, Any]:
value = some_dao.get_some_data(num=value)
return {"detail": value}
@pytest.fixture(scope="session")
def test_client() -> TestClient:
client = TestClient(app)
return client
def test_sqla_resource(test_client: TestClient) -> None:
rnd = Random()
random_int = rnd.randint(-(10**6), 10**6)
response = test_client.get(f"/values/{random_int}")
assert response.status_code == 200
assert not DIContainer.db_session.initialized
body = response.json()
assert body["detail"] == random_int
```
Raw data
{
"_id": null,
"home_page": null,
"name": "deps-injection",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "dependency, di, injection, injector",
"author": null,
"author_email": "Ivan Belyaev <vanobel159@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/00/42/1319ee89a8b19be83c257a206553e9900fa9e361befab4597771ce869ccb/deps_injection-1.8.0.tar.gz",
"platform": null,
"description": "# Injection\n\n\n\n\n\n\n[](https://github.com/nightblure/injection/actions/workflows/publish.yml)\n[](https://injection.readthedocs.io/en/latest/?badge=latest)\n\n[](https://github.com/nightblure/injection/actions/workflows/ci.yml)\n[](https://codecov.io/gh/nightblure/injection)\n\n[](https://github.com/astral-sh/ruff)\n[](https://github.com/pypa/hatch)\n[](https://github.com/astral-sh/uv)\n[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)\n\n[](https://codeclimate.com/github/nightblure/injection/maintainability)\n\n\n\n\n\n---\n\nEasy dependency injection for all, works with Python 3.8-3.13. Main features and advantages:\n* support **Python 3.8-3.13**;\n* works with **FastAPI, **Litestar**, Flask** and **Django REST Framework**;\n* support **dependency** **injection** via `Annotated` in `FastAPI`;\n* support **async injections**;\n* support [**auto injection by types**](https://injection.readthedocs.io/latest/injection/auto_injection.html); \n* [**resources**](https://injection.readthedocs.io/latest/providers/resource.html) with **function scope**;\n* no **wiring**;\n* **overriding** dependencies for testing;\n* **100%** code coverage;\n* the code is fully **typed** and checked with [mypy](https://github.com/python/mypy);\n* good [documentation](https://injection.readthedocs.io/latest/);\n* intuitive and almost identical api with [dependency-injector](https://github.com/ets-labs/python-dependency-injector),\nwhich will allow you to easily migrate to injection\n(see [migration from dependency injector](https://injection.readthedocs.io/latest/dev/migration-from-dependency-injector.html));\n\n---\n\n## Installation\n```shell\npip install deps-injection\n```\n\n## Compatibility between web frameworks and injection features\n| Framework | Dependency injection with @inject | Overriding providers | Dependency injection with @autoinject |\n|--------------------------------------------------------------------------|:---------------------------------:|:--------------------:|:-------------------------------------------:|\n| [FastAPI](https://github.com/fastapi/fastapi) | \u2705 | \u2705 | \u2796 |\n| [Flask](https://github.com/pallets/flask) | \u2705 | \u2705 | \u2705 |\n| [Django REST Framework](https://github.com/encode/django-rest-framework) | \u2705 | \u2705 | \u2705 |\n| [Litestar](https://github.com/litestar-org/litestar) | \u2705 | \u2705 | \u2796 | \u2796 |\n\n\n## Quickstart with FastAPI, SQLAlchemy and pytest\n```python3\nfrom contextlib import contextmanager\nfrom random import Random\nfrom typing import Annotated, Any, Callable, Dict, Iterator\n\nimport pytest\nfrom fastapi import Depends, FastAPI\nfrom sqlalchemy import create_engine, text\nfrom sqlalchemy.orm import Session, sessionmaker\nfrom starlette.testclient import TestClient\n\nfrom injection import DeclarativeContainer, Provide, inject, providers\n\n\n@contextmanager\ndef db_session_resource(session_factory: Callable[..., Session]) -> Iterator[Session]:\n session = session_factory()\n try:\n yield session\n except Exception:\n session.rollback()\n finally:\n session.close()\n\n\nclass SomeDAO:\n def __init__(self, db_session: Session) -> None:\n self.db_session = db_session\n\n def get_some_data(self, num: int) -> int:\n stmt = text(\"SELECT :num\").bindparams(num=num)\n data: int = self.db_session.execute(stmt).scalar_one()\n return data\n\n\nclass DIContainer(DeclarativeContainer):\n db_engine = providers.Singleton(\n create_engine,\n url=\"sqlite:///db.db\",\n pool_size=20,\n max_overflow=0,\n pool_pre_ping=False,\n )\n\n session_factory = providers.Singleton(\n sessionmaker,\n db_engine.cast,\n autoflush=False,\n autocommit=False,\n )\n\n db_session = providers.Resource(\n db_session_resource,\n session_factory=session_factory.cast,\n function_scope=True,\n )\n\n some_dao = providers.Factory(SomeDAO, db_session=db_session.cast)\n\n\nSomeDAODependency = Annotated[SomeDAO, Depends(Provide[DIContainer.some_dao])]\n\napp = FastAPI()\n\n\n@app.get(\"/values/{value}\")\n@inject\nasync def sqla_resource_handler_async(\n value: int,\n some_dao: SomeDAODependency,\n) -> Dict[str, Any]:\n value = some_dao.get_some_data(num=value)\n return {\"detail\": value}\n\n\n@pytest.fixture(scope=\"session\")\ndef test_client() -> TestClient:\n client = TestClient(app)\n return client\n\n\ndef test_sqla_resource(test_client: TestClient) -> None:\n rnd = Random()\n random_int = rnd.randint(-(10**6), 10**6)\n\n response = test_client.get(f\"/values/{random_int}\")\n\n assert response.status_code == 200\n assert not DIContainer.db_session.initialized\n body = response.json()\n assert body[\"detail\"] == random_int\n \n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Easy dependency injection without wiring",
"version": "1.8.0",
"project_urls": {
"Changelog": "https://github.com/nightblure/injection/releases",
"Documentation": "https://injection.readthedocs.io/latest/",
"Homepage": "https://pypi.org/project/deps-injection/",
"Issues": "https://github.com/nightblure/injection/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc",
"Repository": "https://github.com/nightblure/injection.git"
},
"split_keywords": [
"dependency",
" di",
" injection",
" injector"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "7e23a2ae27057fff9ae43c4895aa91749b87d09a268fe1bc405cb6a4e3d2b756",
"md5": "8dee5d69f3840b363f2fec661d3b2093",
"sha256": "5d12bbb09bf9f333d808bc92d0175e58a49194a1f7924e73b4a9357c138f9511"
},
"downloads": -1,
"filename": "deps_injection-1.8.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8dee5d69f3840b363f2fec661d3b2093",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 16752,
"upload_time": "2025-02-09T12:44:21",
"upload_time_iso_8601": "2025-02-09T12:44:21.301849Z",
"url": "https://files.pythonhosted.org/packages/7e/23/a2ae27057fff9ae43c4895aa91749b87d09a268fe1bc405cb6a4e3d2b756/deps_injection-1.8.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "00421319ee89a8b19be83c257a206553e9900fa9e361befab4597771ce869ccb",
"md5": "d20ecea5ec09b6d523696e99254bd437",
"sha256": "2d1eb65896fdecf6d482fb36f556bc7d85b9d5e5f6164365b5260d1582e74877"
},
"downloads": -1,
"filename": "deps_injection-1.8.0.tar.gz",
"has_sig": false,
"md5_digest": "d20ecea5ec09b6d523696e99254bd437",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 12663,
"upload_time": "2025-02-09T12:44:22",
"upload_time_iso_8601": "2025-02-09T12:44:22.852064Z",
"url": "https://files.pythonhosted.org/packages/00/42/1319ee89a8b19be83c257a206553e9900fa9e361befab4597771ce869ccb/deps_injection-1.8.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-09 12:44:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "nightblure",
"github_project": "injection",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "deps-injection"
}