Fastapi dependencies internals
Skill X1nzhe/claude-fastapi-guide-plugin/skills/fastapi-dependencies-internals
Internal conventions, abstractions, and anti-patterns for FastAPI's dependency-injection core under fastapi/dependencies/. Use when reading or modifying files in fastapi/dependencies/, when working with Dependant / solve_dependencies / get_dependant / analyze_param, when extending the DI system with a new injectable kind, or when debugging dependency caching, scoping, or callable dispatch.From its SKILL.md
npx -y skills add X1nzhe/claude-fastapi-guide-plugin --skill fastapi-dependencies-internalsAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
5.0 KB, as published. Nobody here has run it
fastapi/dependencies — internals
The dependency-injection core. fastapi/dependencies/models.py defines Dependant — a dataclass representing one callable's injection points. fastapi/dependencies/utils.py holds get_dependant (build), solve_dependencies (evaluate), and the request-parameter parsers.
When to use this skill
Use this skill whenever the work touches the DI subsystem:
- Reading, modifying, or extending files under fastapi/dependencies/
- Working with
Dependant,solve_dependencies,get_dependant,analyze_param,request_params_to_args,request_body_to_args - Adding a new injectable parameter kind, security scope, or scope type
- Debugging dependency caching (
use_cache,cache_key), generator scopes (function/request), or sync/async dispatch
Do not use this skill for general FastAPI request-flow questions — those go to the trace-data-flow or explain-feature skills. This one is specific to DI internals.
Two-phase pattern (don't blur the phases)
- Build (once, at startup) —
get_dependantintrospects a callable viaget_typed_signature+analyze_paramand produces aDependanttree. Side-effect-free. - Solve (per request) —
solve_dependencieswalks the tree, recursively resolves sub-dependants, extracts path/query/header/cookie/body values, returns aSolvedDependency.
Building must be side-effect-free; solving is async. Don't move work between phases.
Key abstractions
Dependant(fastapi/dependencies/models.py:32) is effectively append-only after construction. Its@cached_propertyslots (is_coroutine_callable,is_gen_callable,is_async_gen_callable,cache_key,oauth_scopes) memoize on first access — mutating fields afterwards corrupts the cache. Build a newDependantinstead of editing one.- Callable classification uses
_unwrapped_call+_impartialto see throughfunctools.partialandfunctools.wraps. Always use these helpers; rawinspectmisses wrapped callables. DependencyCacheKey = (call, oauth_scopes, computed_scope)— per-request cache;Depends(..., use_cache=False)opts out.- Scopes:
"function"vs"request"(generators default to"request"). Cleanup runs viaAsyncExitStacks onrequest.scope(fastapi_function_astack/fastapi_inner_astack).
Execution dispatch (in solve_dependencies)
Per sub-dependant, the branch is chosen from Dependant's cached_property flags:
| Call shape | Branch |
|---|---|
| async generator | asynccontextmanager(call) → stack.enter_async_context |
| sync generator | contextmanager_in_threadpool(contextmanager(call)) → enter_async_context |
| coroutine function | await call(**values) |
| plain sync callable | await run_in_threadpool(call, **values) |
Every sync path goes through anyio — never call user code directly on the event loop.
Conventions
- Pydantic access goes through
fastapi._compat(ModelField,Undefined, …). No directpydanticimports here — breaks v1 support. - Errors are accumulated, not raised. Parsers return
(values, errors);solve_dependenciesaggregates them into one 422. Don'traisemid-parse for validation errors. - Dependency overrides rebuild the sub-dependant with
get_dependant; preserve that path when adding fields toDependant.
Don't
- Don't mutate a
Dependantafter construction —cached_propertywill hand back stale data. - Don't bypass
analyze_paramwhen adding a new injectable kind; classification lives in one place on purpose. - Don't remove the
async_exit_stackparameter ofsolve_dependenciesdespite the TODO — it's kept for external monkey-patching (the comment says so). - Don't add a sync variant of
solve_dependencies— async-first; bridge sync user code viarun_in_threadpool/contextmanager_in_threadpool. - Don't recurse into
solve_dependencieswithout forwardingdependency_cache— losing it silently double-executes deps and breaksuse_cache=True.
See also
- fastapi/dependencies/utils.py —
solve_dependencies,get_dependant,analyze_param - fastapi/dependencies/models.py —
Dependantdataclass - fastapi/_compat/ — Pydantic v1/v2 shims (don't bypass)
skills/trace-data-flow/SKILL.md— sibling skill for end-to-end request tracesskills/explain-feature/SKILL.md— sibling skill for static feature explanations