Dependency Injection API Reference¶
Depends resolution, caching, and cycle detection.
DependencyCycleError
¶
Depends
¶
Dependency injection marker.
Use it either as a default value or inside Annotated (recommended)::
def handler(svc: Service = Depends(get_service)): ...
def handler(svc: Annotated[Service, Depends(get_service)]): ...
:param dependency: The callable to resolve (plain, async, or a sync/async
generator for setup/teardown via yield).
:param use_cache: If True (default), the result is cached per-request so the
same dependency resolves once even if declared by several parameters.
Source code in src/istos/di/depends.py
extract_depends(param)
¶
Return the Depends marker for a parameter, from either its default value
or an Annotated[..., Depends(...)] annotation.
Source code in src/istos/di/depends.py
has_dependencies(func)
¶
True if any parameter of func declares a Depends (default or Annotated).
Source code in src/istos/di/depends.py
inject_and_run(func, context=None, overrides=None)
async
¶
High-level entrypoint: resolve func's dependencies and call it.
Manages the lifecycle of all yield dependencies via an AsyncExitStack,
so they tear down cleanly even if the call raises.
Source code in src/istos/di/depends.py
invoke_with_dependencies(func, *, args=(), context=None, skip_names=(), overrides=None)
async
¶
Resolve func's dependencies and call it, driving yield-dep teardown.
args are positional values passed through unchanged (e.g. an instance and
a message payload); skip_names are the parameter names those positional
values fill, so they are not treated as dependencies. Sync callables run in a
worker thread.
Source code in src/istos/di/depends.py
positional_param_names(func)
¶
Ordered names of the parameters a framework supplies positionally.
Excludes self, Depends(...) parameters, and args/*kwargs — i.e. the
slots a wrapper fills with a payload (message data, query result, ...).
Source code in src/istos/di/depends.py
resolve_dependencies(func, existing_kwargs, exit_stack, cache=None, overrides=None, _chain=(), skip=None)
async
¶
Recursively resolve a callable's dependencies into a kwargs dict.
Supports Depends via default value or Annotated, sub-dependencies,
per-request caching, testing overrides, and yield dependencies whose
teardown is registered on exit_stack. Sync dependencies (plain or
generator) are offloaded to a worker thread so they never block the loop.
skip names parameters the caller supplies positionally (e.g. a message
payload); they are neither resolved nor validated here.
Source code in src/istos/di/depends.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |