Add worker-level automatic task dependencies#393
Merged
chrisguidry merged 6 commits intomainfrom Apr 15, 2026
Merged
Conversation
Adds an analogue of FastAPI's router dependencies: factories registered
on `Worker(dependencies={...})` that run around every task the worker
executes. Same factory shapes as `Depends(...)` (sync, async, context
managers), and they can declare their own parameters (`TaskKey`,
`TaskArgument`, `CurrentWorker`, nested `Depends(...)`) that resolve
recursively against the same per-task cache as task-level deps.
Worker deps that return a `single=True` `Dependency` instance
(`Timeout`, `Retry`, `Perpetual`, `ConcurrencyLimit`, `Debounce`) act as
defaults for every task. When a task declares the same single type, we
raise `ValueError` at resolution time naming both sources rather than
silently picking one. The conflict scan is memoized per
`(worker, function)` so it runs at most once per task function.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9cfd4da17f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Drops the factory-wrapping convention so the API reads more naturally:
`{"db": Depends(get_db), "retry": Retry(attempts=3)}` instead of
`{"db": get_db, "retry": lambda: Retry(attempts=3)}`. The built-in
stateful dependencies (Retry, Perpetual, Timeout, ConcurrencyLimit,
Debounce) already construct a fresh internal instance in __aenter__, so
a single instance passed at worker construction is safe to reuse across
executions.
The dependencies kwarg is typed Mapping[str, Any] to match how Depends
itself is typed (return R for ergonomic parameter defaults), with
validation at Worker construction.
Replaces the hardcoded SINGLE_DEPENDENCY_TYPES tuple with dynamic detection that mirrors uncalled_for.validate_dependencies: concrete-type duplicates first (so the error names the exact class, e.g. "Only one Retry dependency is allowed"), then cross-subclass conflicts under a shared single-bearing base. Lets users add new single=True dependencies without touching docket.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an analogue of FastAPI's router dependencies: factories registered
on
Worker(dependencies={...})that run around every task the workerexecutes. Same factory shapes as
Depends(...)(sync, async, contextmanagers), and they can declare their own parameters (
TaskKey,TaskArgument,CurrentWorker, nestedDepends(...)) that resolverecursively against the same per-task cache as task-level deps.
Worker deps that return a
single=TrueDependencyinstance(
Timeout,Retry,Perpetual,ConcurrencyLimit,Debounce) act asdefaults for every task. When a task declares the same single type, we
raise
ValueErrorat resolution time naming both sources rather thansilently picking one. The conflict scan is memoized per
(worker, function)so it runs at most once per task function.See the new "Worker-level dependencies" section in
docs/dependency-injection.mdfor thecommon use cases (tracing span, DB transaction, audit log).
🤖 Generated with Claude Code