that-depends
that-depends copied to clipboard
feature request: Resource but for callables that return context manager
Resource
provider expects callable object that returns either Iterable
or AsyncIterable
that yields only one element (the result value for provider attribute in DI container).
I think it would be great to support python context managers too.
import typing
T = typing.TypeVar("T")
async def create_async_cm_resource() -> typing.AsyncContextManager[T]:
...
class DIContainer(BaseContainer):
async_resource = providers.Resource(create_async_resource)
async def main() -> None
await DIContainer.init_resources() # CM instance got from `create_async_cm_resource` and it's `__aenter__` invoked
cm_result = await DIContainer.async_resource() # result is T
...
await DIContainer.tear_down() # CM instance `__aexit__` invoked
P.S. I'm ready to make a PR