provide#

class starlite.datastructures.provide.Provide#

Bases: object

Wrapper class for dependency injection

__init__(dependency: AnyCallable, use_cache: bool = False, sync_to_thread: bool = False) None#

Initialize Provide

Parameters:
  • dependency – Callable to inject, can be a function, method or class.

  • use_cache – Cache the dependency return value. Defaults to False.

  • sync_to_thread – Run sync code in an async thread. Defaults to False.

async __call__(**kwargs: Any) Any#

Proxy a call to self.proxy.

class starlite.datastructures.provide.DependencyCleanupGroup#

Bases: object

Wrapper for generator based dependencies.

Simplify cleanup by wrapping next() / anext() calls and providing facilities to throw / athrow into all generators consecutively. An instance of this class can be used as a contextmanager, which will automatically throw any exceptions into its generators. All exceptions caught in this manner will be re-raised after they have been thrown in the generators.

__init__(generators: Optional[List[AnyGenerator]] = None) None#

Initialize DependencyCleanupGroup.

Parameters:

generators – An optional list of generators to be called at cleanup

add(generator: Union[Generator[Any, None, None], AsyncGenerator[Any, None]]) None#

Add a new generator to the group.

Parameters:

generator – The generator to add

Returns:

None

async cleanup() None#

Execute cleanup by calling next() / anext() on all generators.

If there are multiple generators to be called, they will be executed in a anyio.TaskGroup.

Returns:

None

async __aenter__() None#

Support the async contextmanager protocol to allow for easier catching and throwing of exceptions into the generators.

async __aexit__(exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[Traceback]) None#

If an exception was raised within the contextmanager block, throw it into all generators.

async throw(exc: BaseException) None#

Throw an exception in all generators sequentially.

Parameters:

exc – Exception to throw