From 3befb0e7dd6b27f45424cc52204fe6e1698bb360 Mon Sep 17 00:00:00 2001 From: Ivan Belyaev Date: Tue, 17 Dec 2024 22:05:37 +0300 Subject: [PATCH] Add docs for auto injection --- README.md | 3 +- docs/index.md | 2 +- docs/injection/auto_injection.md | 51 ++++++++++++++++++++++++++++++++ docs/injection/injection.md | 6 ---- 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 docs/injection/auto_injection.md delete mode 100644 docs/injection/injection.md diff --git a/README.md b/README.md index 7f9cafb..bfd8ca0 100644 --- a/README.md +++ b/README.md @@ -28,12 +28,13 @@ Easy dependency injection for all, works with Python 3.8-3.12. Main features and * support **Python 3.8-3.12**; * works with **FastAPI, **Litestar**, Flask** and **Django REST Framework**; * support **dependency** **injection** via `Annotated` in `FastAPI`; -* the code is fully **typed** and checked with [mypy](https://github.com/python/mypy); * support **async injections**; +* [**autoinject**]() **feature**; * **resources** 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 diff --git a/docs/index.md b/docs/index.md index fc3b7b4..1a0c386 100644 --- a/docs/index.md +++ b/docs/index.md @@ -33,7 +33,7 @@ :maxdepth: 1 :caption: Dependency injection - injection/injection.md + injection/auto_injection.md .. toctree:: :maxdepth: 1 diff --git a/docs/injection/auto_injection.md b/docs/injection/auto_injection.md new file mode 100644 index 0000000..ba1c782 --- /dev/null +++ b/docs/injection/auto_injection.md @@ -0,0 +1,51 @@ +# Auto injection + +**Auto injection** allows you to **not specify markers and providers** +in the parameters of the function for which you want to inject dependencies. + +Here it is enough to **specify** the **types** for which the corresponding providers +have already been created in the container. +`@autoinject` decorator will try to find providers according to the types +of the function parameters and inject the dependencies. + +### Example + +```python +from injection import DeclarativeContainer, auto_inject, providers + + +class SomeClass: + def __init__(self, a: int, b: str) -> None: + self.a = a + self.b = b + + def func(self) -> str: + return "sync func" + + async def async_func(self) -> str: + return "async func" + + +class DIContainer(DeclarativeContainer): + some_provider = providers.Factory(SomeClass, a=1, b="b") + + +def test_auto_inject_to_sync_function() -> None: + @auto_inject(target_container=DIContainer) + def _sync_func(obj: SomeClass) -> None: + assert obj.func() == "sync func" + assert obj.a == 1 + assert obj.b == "b" + + _sync_func() + + +async def test_auto_inject_to_async_function() -> None: + @auto_inject(target_container=DIContainer) + async def _async_func(obj: SomeClass) -> None: + assert await obj.async_func() == "async func" + assert obj.a == 1 + assert obj.b == "b" + + await _async_func() +``` diff --git a/docs/injection/injection.md b/docs/injection/injection.md deleted file mode 100644 index 02eb8c4..0000000 --- a/docs/injection/injection.md +++ /dev/null @@ -1,6 +0,0 @@ -# Dependency injection - -soon... - -## Auto injection -