Skip to content

Commit

Permalink
Add docs for auto injection
Browse files Browse the repository at this point in the history
  • Loading branch information
nightblure committed Dec 17, 2024
1 parent f54eb45 commit 3befb0e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
:maxdepth: 1
:caption: Dependency injection
injection/injection.md
injection/auto_injection.md
.. toctree::
:maxdepth: 1
Expand Down
51 changes: 51 additions & 0 deletions docs/injection/auto_injection.md
Original file line number Diff line number Diff line change
@@ -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()
```
6 changes: 0 additions & 6 deletions docs/injection/injection.md

This file was deleted.

0 comments on commit 3befb0e

Please sign in to comment.