Skip to content

dynamic container: tests and docs #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
introduction/ioc-container
introduction/fastapi
introduction/litestar
introduction/multiple-containers
introduction/inject-factories
introduction/multiple-containers
introduction/dynamic-container

.. toctree::
:maxdepth: 1
Expand Down
25 changes: 25 additions & 0 deletions docs/introduction/dynamic-container.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Dynamic container

You can dynamically assign providers to container:
```python
import datetime

from tests import container
from that_depends import BaseContainer, providers


class DIContainer(BaseContainer):
sync_resource: providers.Resource[datetime.datetime]
async_resource: providers.AsyncResource[datetime.datetime]


DIContainer.sync_resource = providers.Resource(container.create_sync_resource)
DIContainer.async_resource = providers.AsyncResource(container.create_async_resource)
```

And than you can use these providers as usual:

```python
sync_resource = await DIContainer.sync_resource()
async_resource = await DIContainer.async_resource()
```
48 changes: 24 additions & 24 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/test_dynamic_container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import datetime

from tests import container
from that_depends import BaseContainer, providers


class DIContainer(BaseContainer):
sync_resource: providers.Resource[datetime.datetime]
async_resource: providers.AsyncResource[datetime.datetime]


DIContainer.sync_resource = providers.Resource(container.create_sync_resource)
DIContainer.async_resource = providers.AsyncResource(container.create_async_resource)


async def test_dynamic_container() -> None:
sync_resource = await DIContainer.sync_resource()
async_resource = await DIContainer.async_resource()

assert isinstance(sync_resource, datetime.datetime)
assert isinstance(async_resource, datetime.datetime)
Loading