Skip to content

Commit

Permalink
fix provide markers instantiation in litestar docs and code example
Browse files Browse the repository at this point in the history
  • Loading branch information
nightblure committed Nov 18, 2024
1 parent f1da7b5 commit 6ed2b5f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ pip install deps-injection
```

## Compatibility between web frameworks and injection features
| Framework | Dependency injection with or without @inject | Dependency injection with @autoinject (_experimental_) | Overriding providers |
|--------------------------------------------------------------------------|:--------------------------------------------:|:------------------------------------------------------:|:--------------------:|
| [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) | | | |
| Framework | Dependency injection with @inject | Overriding providers | Dependency injection with @autoinject (_experimental_) |
|--------------------------------------------------------------------------|:----------------------------------------:|:--------------------:|:------------------------------------------------------:|
| [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) || | |


## Using example with FastAPI
Expand Down
15 changes: 9 additions & 6 deletions docs/integration-with-web-frameworks/litestar.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Litestar

By now you should have a container with providers.
Now all you have to do is define request handlers according to the following rules:
In order to successfully inject dependencies into Litestar request handlers,
make sure that the following points are completed:
1. use **@inject** decorator **before** http-method Litestar decorator;

2. added for each injected parameter to the request handler a typing of the form
`Union[<your type>, Any]` for Python versions below 3.10 or `<your type> | Any`,
as well as the `Provide` marker from the `injection` package indicating the provider

3. use the `Provide` marker from the `injection` (not from Litestar) package indicating the provider

---

## Example

Expand Down Expand Up @@ -42,8 +45,8 @@ class Container(DeclarativeContainer):
)
@inject
async def litestar_endpoint(
redis: Union[Redis, Any] = Provide(Container.redis),
num: Union[int, Any] = Provide(Container.num),
redis: Union[Redis, Any] = Provide[Container.redis],
num: Union[int, Any] = Provide[Container.num],
) -> dict:
value = redis.get(800)
return {"detail": value, "num2": num}
Expand All @@ -55,7 +58,7 @@ async def litestar_endpoint(
)
@inject
async def litestar_endpoint_object_provider(
num: Union[int, Any] = Provide(Container.num),
num: Union[int, Any] = Provide[Container.num],
) -> dict:
return {"detail": num}

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_litestar/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
)
@inject
async def litestar_endpoint(
redis: Union[Redis, Any] = Provide(Container.redis), # noqa: B008
num: Union[int, Any] = Provide(Container.num2), # noqa: B008
redis: Union[Redis, Any] = Provide[Container.redis],
num: Union[int, Any] = Provide[Container.num2],
) -> dict:
value = redis.get(800)
return {"detail": value, "num2": num}
Expand All @@ -27,7 +27,7 @@ async def litestar_endpoint(
)
@inject
async def litestar_endpoint_object_provider(
num: Union[int, Any] = Provide(Container.num2), # noqa: B008
num: Union[int, Any] = Provide[Container.num2],
) -> dict:
return {"detail": num}

Expand Down

0 comments on commit 6ed2b5f

Please sign in to comment.