-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dynamic container: tests and docs (#42)
- Loading branch information
Showing
4 changed files
with
72 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |