API Reference

fundi.scan(call: Callable[[...], R], caching: bool = True, async_: bool | None = None, generator: bool | None = None, context: bool | None = None, use_return_annotation: bool = True, side_effects: tuple[Callable[[...], Any], ...] = ()) CallableInfo[R][source]

Get callable information

Parameters:
  • call – callable to get information from

  • caching – whether to use cached result of this callable or not

  • async – Override “async_” attribute value

  • generator – Override “generator” attribute value

  • context – Override “context” attribute value

  • use_return_annotation – Whether to use call’s return annotation to define it’s type

  • side_effects – functions that will be injected before this dependant

Returns:

callable information

fundi.from_(dependency: type | Callable[[...], Any], caching: bool = True, async_: bool | None = None, generator: bool | None = None, context: bool | None = None, use_return_annotation: bool = True) TypeResolver | CallableInfo[Any][source]

Use callable or type as dependency for parameter of function

if dependency parameter is callable the fundi.scan.scan is used

if dependency parameter is type the fundi.types.TypeResolver is returned (unless that type is a subclass of AbstractContextManager or AbstractAsyncContextManager)

Parameters:
  • dependency – function dependency

  • caching – Whether to use cached result of this callable or not

  • async – Override “async_” attriubute value

  • generator – Override “generator” attriubute value

  • context – Override “context” attriubute value

  • use_return_annotation – Whether to use dependency’s return annotation to define it’s type

Returns:

callable information

fundi.inject(scope: Mapping[str, Any] | Scope, info: CallableInfo[Any], stack: ExitStack | None = None, cache: MutableMapping[CacheKey, Any] | None = None, override: Mapping[Callable[[...], Any], Any] | None = None) Any[source]

Synchronously inject dependencies into callable.

If exit stack is not provided - it will be created and closed after injection

Parameters:
  • scope – container with contextual values

  • info – callable information

  • stack – exit stack to properly handle generator dependencies

  • cache – dependency cache

  • override – override dependencies

Returns:

result of callable

async fundi.ainject(scope: Mapping[str, Any] | Scope, info: CallableInfo[Any], stack: AsyncExitStack | None = None, cache: MutableMapping[CacheKey, Any] | None = None, override: Mapping[Callable[[...], Any], Any] | None = None) Any[source]

Asynchronously inject dependencies into callable.

If exit stack is not provided - it will be created and closed after injection

Parameters:
  • scope – container with contextual values

  • info – callable information

  • stack – exit stack to properly handle generator dependencies

  • cache – dependency cache

  • override – override dependencies

Returns:

result of callable

fundi.resolve(scope: Scope, info: CallableInfo[Any], cache: Mapping[CacheKey, Any], override: Mapping[Callable[[...], Any], Any] | None = None) Generator[ParameterResult, None, None][source]

Try to resolve values from cache or scope for callable parameters

Recommended use case:

values = {}
cache = {}
for result in resolve(scope, info, cache):
    value = result.value
    name = result.parameter_name

    if not result.resolved:
        value = inject(scope, info, stack, cache)
        cache[name] = value

    values[name] = value
Parameters:
  • scope – container with contextual values

  • info – callable information

  • cache – solvation cache(modify it if necessary while resolving)

  • override – override dependencies

Returns:

generator with solvation results

fundi.configurable_dependency(configurator: Callable[[P], Callable[[InnerP], R]]) DependencyConfiguratorProtocol[P, InnerP, R][source]

Create dependency configurator that caches configured dependencies. This helps FunDI cache resolver understand that dependency already executed, if it was.

Note: Calls with mutable arguments will not be stored in cache and warning would be shown

Parameters:

configurator – Original dependency configurator

Returns:

cache aware dependency configurator

fundi.virtual_context(function: Callable[[P], Generator[T, None, None]]) VirtualContextProvider[T, P][source]
fundi.virtual_context(function: Callable[[P], AsyncGenerator[T]]) AsyncVirtualContextProvider[T, P]

Define virtual context manager using decorator

Example:

@virtual_context
def file(name: str):
    file_ = open(name, "r")
    try:
        yield file_
    finally:
        file_.close()


with file("dontreadthis.txt") as f:
    print(f.read())


@virtual_context
async def lock(name: str):
    lock_ = locks[name]
    lock_.acquire()
    try:
        yield
    finally:
        lock_.release()


async with lock("socket-send"):
    await socket.send("wtf")
fundi.FromType

Tell resolver to resolve parameter’s value by its type, not name

alias of Annotated[R, TypeResolver]