API Reference

Warning

Work in progress: The full API documentation under active development. There is likely missing coverage of methods.

class flexdi.FlexGraph

The FlexGraph is used to keep track of dependencies and invoke callables.

When determining dependencies for a callable, flexdi will examine the type annotations of the arguments, and populate the graph with dependencies which can satisfy the callable. A callable can be anything from a class (as seen with the type annotations), to functions, class methods, generators, etc.

For complex types, flexdi allows binding helper functions that can map a type definition to an instance. These bindings can themselves be injected with dependencies. Bindings can also be defined as generators which allows supplying custom teardown logic for dependencies.

When using the FlexGraph directly to resolve dependencies, it creates scopes which ensures the proper caching, startup and teardown of any dependencies it creates during dependency resolution.

graph = FlexGraph()

@graph.bind()
def my_provider(...) -> ...:
    ...

result = await graph.resolve(func)
override() Iterator[FlexGraph]

This context manager is primarily intended for testing use-cases.

An override allow users to override dependency bindings a temporary way which will be reverted when the override is closed. This prevents making permanent changes to a graph which is usually defined directly in the module.

The previous rules for the graph are preserved by the context manager and a clone of them is placed into the active graph instance. When the context manager exits, the previous un-edited rules are re-applied to the graph.

Returns:

ContextManager[FlexGraph].