Skip to content

Commit

Permalink
add List provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnik512 committed Mar 20, 2024
1 parent b443420 commit 0573f3a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "that-depends"
version = "1.1.0"
version = "1.2.0"
description = "Simple Dependency Injection framework"
authors = ["Artur Shiriev <[email protected]>"]
readme = "README.md"
Expand Down
1 change: 1 addition & 0 deletions tests/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class AsyncDependentFactory:
class DIContainer(BaseContainer):
sync_resource = providers.Resource[str](create_sync_resource)
async_resource = providers.AsyncResource[str](create_async_resource)
sequence = providers.List[str](sync_resource, async_resource)

independent_factory = providers.Factory(IndependentFactory, dep1="text", dep2=123)
sync_dependent_factory = providers.Factory(
Expand Down
2 changes: 2 additions & 0 deletions tests/test_di.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ async def test_di() -> None:
independent_factory = await DIContainer.independent_factory()
sync_dependent_factory = await DIContainer.sync_dependent_factory()
async_dependent_factory = await DIContainer.async_dependent_factory()
sequence = await DIContainer.sequence()
assert sync_dependent_factory.independent_factory is not independent_factory
assert sync_dependent_factory.sync_resource == "sync resource"
assert async_dependent_factory.async_resource == "async resource"
assert sequence == ["sync resource", "async resource"]


def test_wrong_providers_init() -> None:
Expand Down
11 changes: 11 additions & 0 deletions that_depends/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,14 @@ async def resolve(self) -> T:
*[await x() if isinstance(x, AbstractProvider) else x for x in self._args],
**{k: await v() if isinstance(v, AbstractProvider) else v for k, v in self._kwargs.items()},
)


class List(AbstractProvider[T]):
def __init__(self, *providers: AbstractProvider[typing.Any]) -> None:
self._providers = providers

async def resolve(self) -> list[T]: # type: ignore[override]
return [await x.resolve() for x in self._providers]

async def __call__(self) -> list[T]: # type: ignore[override]
return await self.resolve()

0 comments on commit 0573f3a

Please sign in to comment.