feat: support Annotated dependencies in dependency extraction#346
feat: support Annotated dependencies in dependency extraction#346omikader wants to merge 3 commits intochrisguidry:mainfrom
Annotated dependencies in dependency extraction#346Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a1a7d8e792
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| metadata = getattr(param.annotation, "__metadata__", ()) | ||
| return next((item for item in metadata if isinstance(item, Dependency)), None) |
There was a problem hiding this comment.
Resolve postponed annotations before scanning metadata
When tasks are defined in modules using from __future__ import annotations (common on Python 3.10), inspect.signature() exposes param.annotation as a string, so __metadata__ is missing and this helper returns None. That means Annotated[..., Depends(...)] parameters are silently skipped during dependency extraction, and workers later invoke the task without required injected args, raising runtime TypeError for missing parameters instead of resolving dependencies.
Useful? React with 👍 / 👎.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #346 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 99 100 +1
Lines 3098 3144 +46
Branches 27 27
=========================================
+ Hits 3098 3144 +46
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
Hey @omikader did you have any thoughts about the idea of using |
|
Hi @chrisguidry. I explored this a bit and the implementation is doable — a I'm a bit wary that supporting If there's broader demand for |
|
Sounds good, thank you for taking a swing at this, I think you're right, we should skip this until there's a clearer use case. It would also likely migrate over to the http://github.com/chrisguidry/uncalled-for library too (where I look forward to your first contribution!). I'll close this and the issue for now. |
Summary
Extract dependencies from
Annotated[T, Depends(fn)]metadata in addition to the existing default-value style(x: T = Depends(fn)).Details
Add
annotated_dependency()helper that finds the firstDependencyinstance in a parameter'sAnnotatedmetadata. Used byget_dependency_parameters()for DI resolution andcompact_signature()for startup log display.Note 1: while the runtime extraction works fully,
Annotated-style deps interact poorly with pyright becausedocket.add()preserves the task'sParamSpec— pyright seesAnnotateddeps as required parameters with no default. See #334 for discussion. The default-value style remains the recommended approach for now.Note 2: does not handle nested dependencies, e.g.
Though this can be a fast-follow
Miscellaneous
Updated the
.pre-commit-config.yamlto no longer use the legacy alias