How nested dependencies and caching work
mainDependencies in FastDepends can contain their own dependencies. You simply declare Depends requirements within the dependency function itself.
Caching Behavior:
By default, FastDepends caches dependency responses for the duration of one @inject call task. If multiple nested dependencies require the same sub-dependency, that sub-dependency is executed only once and its result is shared. However, different @inject calls will have different caches.
To disable caching for a specific dependency and force it to execute every time it is requested, use Depends(..., cache=False).
from fast_depends import inject, Depends
def dependency_a():
return "A"
def dependency_b(a: str = Depends(dependency_a)):
return f"B with {a}"
@inject
def main_function(b: str = Depends(dependency_b)):
return b
# To disable caching for dependency_a:
# def dependency_b(a: str = Depends(dependency_a, cache=False)):