Reference

fastapi-injectable

exception fastapi_injectable.DependencyResolveError[source]

Custom error for dependency resolve issues.

async fastapi_injectable.cleanup_all_exit_stacks(*, raise_exception=False)[source]

Clean up all active exit stacks.

Parameters:

raise_exception (bool) – Whether to raise exceptions during cleanup. If False, exceptions are logged as warnings. Defaults to False.

Return type:

None

Notes

  • This method iterates through all registered exit stacks and ensures they are properly closed.

  • Typically used during application shutdown to release all managed resources.

Raises:

DependencyCleanupError – When cleanup fails and raise_exception is True

Parameters:

raise_exception (bool)

Return type:

None

async fastapi_injectable.cleanup_exit_stack_of_func(func, *, raise_exception=False)[source]

Clean up the exit stack associated with a specific function.

Parameters:
  • func (Callable[[...], Any]) – The function whose exit stack should be cleaned up.

  • raise_exception (bool) – Whether to raise exceptions during cleanup. If False, exceptions are logged as warnings. Defaults to False.

Return type:

None

Notes

  • This ensures that resources such as context managers or other async cleanup routines are properly closed for the given function.

Raises:

DependencyCleanupError – When cleanup fails and raise_exception is True

Parameters:
Return type:

None

async fastapi_injectable.clear_dependency_cache()[source]

Clear the dependency resolution cache.

Notes

  • This is useful to free up memory or reset state in scenarios where dependencies might have changed dynamically.

Return type:

None

fastapi_injectable.get_injected_obj(func, *, use_cache=True, raise_exception=False)[source]

Get an injected object from a dependency function with FastAPI’s dependency injection.

This function handles different types of callables (sync/async functions and generators) and returns the first yielded/returned value after resolving dependencies.

Parameters:
  • func (Callable[[~P], T] | Callable[[~P], Awaitable[T]] | Callable[[~P], Generator[T, Any, Any]] | Callable[[~P], AsyncGenerator[T, Any]]) – The dependency function to inject. Can be: - A regular synchronous function - An async function (coroutine) - A synchronous generator - An async generator

  • use_cache (bool) – Whether to cache resolved dependencies. Defaults to True.

  • raise_exception (bool) – Whether to raise exceptions during dependency resolution. If False, exceptions are logged as warnings. Defaults to False.

Returns:

The first value yielded/returned by the dependency function after injection.

Return type:

T

Examples

```python # With a regular function def get_service() -> Service:

return Service()

service = get_injected_obj(get_service)

# With an async function async def get_async_service() -> Service:

return await create_service()

service = get_injected_obj(get_async_service)

# With a generator (for cleanup) def get_db() -> Generator[Database, None, None]:

db = Database() yield db db.cleanup()

db = get_injected_obj(get_db) ```

Notes

  • For generator functions, only the first yielded value is returned

  • Cleanup code in generators will be executed when calling cleanup functions

  • Uses FastAPI’s dependency injection system under the hood

fastapi_injectable.injectable(func=None, *, use_cache=True, raise_exception=False)[source]

Decorator to inject dependencies into any callable, sync or async.

Parameters:
Return type:

Callable[[~P], T] | Callable[[~P], Awaitable[T]] | Callable[[Callable[[~P], T] | Callable[[~P], Awaitable[T]]], Callable[[~P], T] | Callable[[~P], Awaitable[T]]]

async fastapi_injectable.resolve_dependencies(func, *, use_cache=True, raise_exception=False)[source]

Resolve dependencies for the given function using FastAPI’s dependency injection system.

This function resolves dependencies defined via FastAPI’s dependency mechanism and returns a dictionary of resolved arguments for the given function.

Parameters:
  • func (Callable[[~P], T] | Callable[[~P], Awaitable[T]]) – The function for which dependencies need to be resolved. It can be a synchronous or asynchronous callable.

  • use_cache (bool) – Whether to use a cache for dependency resolution. Defaults to True.

  • raise_exception (bool) – Whether to raise an exception when errors occur during dependency resolution. If False, errors are logged as warnings. Defaults to False.

Returns:

A dictionary mapping argument names to resolved dependency values.

Raises:

DependencyResolveError – If raise_exception is True and errors occur during dependency resolution.

Return type:

dict[str, Any]

Notes

  • A fake HTTP request is created to mimic FastAPI’s request-based dependency resolution.

  • Dependency resolution errors are either logged or raised as exceptions based on raise_exception.

fastapi_injectable.setup_graceful_shutdown(signals=None, *, raise_exception=False)[source]

Register handlers to perform cleanup during application shutdown.

Parameters:
  • signals (list[Signals] | None) – A list of OS signals that should trigger the cleanup process. Defaults to [SIGINT, SIGTERM].

  • raise_exception (bool) – Whether to raise exceptions during cleanup. If False, exceptions are logged as warnings. Defaults to False.

Return type:

None

Notes

  • When a registered signal is received, this function ensures that all resources (e.g., exit stacks) are properly released before the application exits.

  • Also registers a cleanup routine via atexit to handle unexpected shutdown scenarios.

Raises:

DependencyCleanupError – When cleanup fails and raise_exception is True

Parameters:
Return type:

None