Skip to content

Add worker-level automatic task dependencies#393

Merged
chrisguidry merged 6 commits intomainfrom
worker-dependencies
Apr 15, 2026
Merged

Add worker-level automatic task dependencies#393
chrisguidry merged 6 commits intomainfrom
worker-dependencies

Conversation

@chrisguidry
Copy link
Copy Markdown
Owner

Summary

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.

See the new "Worker-level dependencies" section in
docs/dependency-injection.md for the
common use cases (tracing span, DB transaction, audit log).

🤖 Generated with Claude Code

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>
@chrisguidry chrisguidry marked this pull request as ready for review April 15, 2026 18:38
@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented Apr 15, 2026

Documentation build overview

📚 docket | 🛠️ Build #32278965 | 📁 Comparing b58faf5 against latest (5ce957d)

  🔍 Preview build  

2 files changed
± api-reference/index.html
± dependency-injection/index.html

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/docket/worker.py
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.
@chrisguidry chrisguidry merged commit 64131a0 into main Apr 15, 2026
50 checks passed
@chrisguidry chrisguidry deleted the worker-dependencies branch April 15, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant