-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement resource provider, async injections, fix mypy errors, add f…
…actory provider (#25)
- Loading branch information
1 parent
6fe360d
commit 26a1210
Showing
45 changed files
with
1,260 additions
and
357 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
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
This file was deleted.
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 |
---|---|---|
@@ -1,25 +1,28 @@ | ||
# Coroutine | ||
|
||
**Coroutine** provider creates a coroutine. | ||
Can be resolved only with using the `async_resolve` method. | ||
|
||
## Example | ||
|
||
```python3 | ||
import asyncio | ||
import asyncio | ||
from typing import Tuple | ||
|
||
from injection import DeclarativeContainer, providers | ||
from injection import DeclarativeContainer, providers | ||
|
||
async def coroutine(arg1: int, arg2: int) -> Tuple[int, int]: | ||
return arg1, arg2 | ||
|
||
async def coroutine(arg1, arg2): | ||
await asyncio.sleep(0.1) | ||
return arg1, arg2 | ||
class DIContainer(DeclarativeContainer): | ||
provider = providers.Coroutine(coroutine, arg1=1, arg2=2) | ||
|
||
arg1, arg2 = asyncio.run(DIContainer.provider.async_resolve()) | ||
assert (arg1, arg2) == (1, 2) | ||
|
||
class DIContainer(DeclarativeContainer): | ||
provider = providers.Coroutine(coroutine, arg1=1, arg2=2) | ||
|
||
async def main() -> None: | ||
arg1, arg2 = await DIContainer.provider.async_resolve(arg1=500, arg2=600) | ||
assert (arg1, arg2) == (500, 600) | ||
|
||
if __name__ == "__main__": | ||
arg1, arg2 = asyncio.run(DIContainer.provider()) | ||
assert (arg1, arg2) == (1, 2) | ||
asyncio.run(main()) | ||
``` |
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,42 @@ | ||
# Factory | ||
|
||
**Factory** works exactly same as **Transient** provider. | ||
|
||
Also supports **asynchronous** dependencies. | ||
|
||
## Example | ||
|
||
```python3 | ||
import asyncio | ||
from dataclasses import dataclass | ||
|
||
from injection import DeclarativeContainer, providers | ||
|
||
|
||
@dataclass | ||
class SomeClass: | ||
field: Tuple[int, int] | ||
|
||
|
||
async def coroutine_func(arg1: int, arg2: int) -> Tuple[int, int]: | ||
return arg1, arg2 | ||
|
||
|
||
class DIContainer(DeclarativeContainer): | ||
coroutine = providers.Coroutine(coroutine_func, arg1=1, arg2=2) | ||
sync_factory = providers.Factory(SomeClass, field=(10, 20)) | ||
async_factory = providers.Factory(SomeClass, field=coroutine) | ||
|
||
|
||
async def main() -> None: | ||
instance = await DIContainer.async_factory.async_resolve() | ||
assert instance.field == (1, 2) | ||
|
||
|
||
instance1 = DIContainer.sync_factory() | ||
instance2 = DIContainer.sync_factory() | ||
|
||
assert instance1 is not instance2 | ||
|
||
asyncio.run(main()) | ||
``` |
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,3 @@ | ||
# Resource | ||
|
||
soon... |
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 |
---|---|---|
@@ -1,27 +1,43 @@ | ||
# Transient | ||
|
||
**Transient** provider creates and returns a new object for each call. | ||
**Transient** provider creates and returns a **new object for each call**. | ||
You can pass any **callable** object as the first parameter. | ||
|
||
Also supports **asynchronous** dependencies. | ||
|
||
## Example | ||
|
||
```python3 | ||
import asyncio | ||
from dataclasses import dataclass | ||
|
||
from injection import DeclarativeContainer, providers | ||
|
||
|
||
@dataclass | ||
class SomeClass: | ||
field: str | ||
field: Tuple[int, int] | ||
|
||
|
||
async def coroutine_func(arg1: int, arg2: int) -> Tuple[int, int]: | ||
return arg1, arg2 | ||
|
||
|
||
class DIContainer(DeclarativeContainer): | ||
provider = providers.Transient(SomeClass, field="str_value") | ||
coroutine = providers.Coroutine(coroutine_func, arg1=1, arg2=2) | ||
sync_transient = providers.Transient(SomeClass, field=(10, 20)) | ||
async_transient = providers.Transient(SomeClass, field=coroutine) | ||
|
||
|
||
async def main() -> None: | ||
instance = await DIContainer.async_transient.async_resolve() | ||
assert instance.field == (1, 2) | ||
|
||
|
||
instance1 = DIContainer.sync_transient() | ||
instance2 = DIContainer.sync_transient() | ||
|
||
if __name__ == "__main__": | ||
instance1 = DIContainer.provider() | ||
instance2 = DIContainer.provider() | ||
assert instance1 is not instance2 | ||
|
||
assert instance1 is not instance2 | ||
asyncio.run(main()) | ||
``` |
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
Oops, something went wrong.