-
Notifications
You must be signed in to change notification settings - Fork 7
feat: support Annotated dependencies in dependency extraction
#346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| """Tests for Annotated[T, Depends(...)] style dependency injection.""" | ||
|
|
||
| from contextlib import asynccontextmanager | ||
| from typing import Annotated, AsyncGenerator | ||
| from uuid import uuid4 | ||
|
|
||
| from docket import ( | ||
| CurrentDocket, | ||
| CurrentExecution, | ||
| CurrentWorker, | ||
| Depends, | ||
| Docket, | ||
| Execution, | ||
| Worker, | ||
| ) | ||
|
|
||
|
|
||
| async def test_annotated_function_dependency(docket: Docket, worker: Worker): | ||
| """A task can declare dependencies using Annotated[T, Depends(fn)] syntax.""" | ||
|
|
||
| async def get_greeting() -> str: | ||
| return f"hello-{uuid4()}" | ||
|
|
||
| called = False | ||
|
|
||
| async def the_task(greeting: Annotated[str, Depends(get_greeting)]): | ||
| assert greeting.startswith("hello-") | ||
|
|
||
| nonlocal called | ||
| called = True | ||
|
|
||
| await docket.add(the_task)() # pyright: ignore[reportCallIssue] | ||
| await worker.run_until_finished() | ||
|
|
||
| assert called | ||
|
|
||
|
|
||
| async def test_annotated_context_manager_dependency(docket: Docket, worker: Worker): | ||
| """Annotated dependencies work with async context manager factories.""" | ||
|
|
||
| stages: list[str] = [] | ||
|
|
||
| @asynccontextmanager | ||
| async def get_resource() -> AsyncGenerator[str, None]: | ||
| stages.append("setup") | ||
| yield "resource-value" | ||
| stages.append("teardown") | ||
|
|
||
| called = False | ||
|
|
||
| async def the_task(resource: Annotated[str, Depends(get_resource)]): | ||
| assert resource == "resource-value" | ||
|
|
||
| nonlocal called | ||
| called = True | ||
|
|
||
| await docket.add(the_task)() # pyright: ignore[reportCallIssue] | ||
| await worker.run_until_finished() | ||
|
|
||
| assert called | ||
| assert stages == ["setup", "teardown"] | ||
|
|
||
|
|
||
| async def test_annotated_contextual_dependencies(docket: Docket, worker: Worker): | ||
| """Annotated syntax works with contextual dependencies like CurrentDocket.""" | ||
|
|
||
| called = False | ||
|
|
||
| async def the_task( | ||
| a: str, | ||
| this_docket: Annotated[Docket, CurrentDocket()], | ||
| this_worker: Annotated[Worker, CurrentWorker()], | ||
| this_execution: Annotated[Execution, CurrentExecution()], | ||
| ): | ||
| assert a == "hello" | ||
| assert this_docket is docket | ||
| assert this_worker is worker | ||
| assert isinstance(this_execution, Execution) | ||
|
|
||
| nonlocal called | ||
| called = True | ||
|
|
||
| await docket.add(the_task)("hello") # pyright: ignore[reportCallIssue] | ||
| await worker.run_until_finished() | ||
|
|
||
| assert called | ||
|
|
||
|
|
||
| async def test_annotated_mixed_with_positional_args(docket: Docket, worker: Worker): | ||
| """Annotated dependencies mix freely with regular positional arguments.""" | ||
|
|
||
| called = False | ||
|
|
||
| async def get_config() -> dict[str, int]: | ||
| return {"version": 2} | ||
|
|
||
| async def the_task( | ||
| name: str, | ||
| count: int, | ||
| config: Annotated[dict[str, int], Depends(get_config)], | ||
| ): | ||
| assert name == "test" | ||
| assert count == 42 | ||
| assert config == {"version": 2} | ||
|
|
||
| nonlocal called | ||
| called = True | ||
|
|
||
| await docket.add(the_task)("test", 42) # pyright: ignore[reportCallIssue] | ||
| await worker.run_until_finished() | ||
|
|
||
| assert called | ||
|
|
||
|
|
||
| async def test_annotated_with_default_style_deps(docket: Docket, worker: Worker): | ||
| """Annotated and default-style dependencies can coexist on the same task.""" | ||
|
|
||
| async def dep_a() -> str: | ||
| return "from-annotated" | ||
|
|
||
| async def dep_b() -> str: | ||
| return "from-default" | ||
|
|
||
| called = False | ||
|
|
||
| async def the_task( | ||
| a: Annotated[str, Depends(dep_a)], | ||
| b: str = Depends(dep_b), | ||
| ): | ||
| assert a == "from-annotated" | ||
| assert b == "from-default" | ||
|
|
||
| nonlocal called | ||
| called = True | ||
|
|
||
| await docket.add(the_task)() # pyright: ignore[reportCallIssue] | ||
| await worker.run_until_finished() | ||
|
|
||
| assert called | ||
|
|
||
|
|
||
| async def test_annotated_reusable_type_alias(docket: Docket, worker: Worker): | ||
| """Annotated types can be reused as aliases across multiple tasks.""" | ||
|
|
||
| async def get_db() -> str: | ||
| return "db-connection" | ||
|
|
||
| DBConn = Annotated[str, Depends(get_db)] | ||
|
|
||
| results: list[str] = [] | ||
|
|
||
| async def task_one(db: DBConn): # pyright: ignore[reportInvalidTypeForm,reportUnknownParameterType] | ||
| results.append(f"one:{db}") | ||
|
|
||
| async def task_two(db: DBConn): # pyright: ignore[reportInvalidTypeForm,reportUnknownParameterType] | ||
| results.append(f"two:{db}") | ||
|
|
||
| await docket.add(task_one)() # pyright: ignore[reportCallIssue,reportUnknownArgumentType] | ||
| await docket.add(task_two)() # pyright: ignore[reportCallIssue,reportUnknownArgumentType] | ||
| await worker.run_until_finished() | ||
|
|
||
| assert sorted(results) == ["one:db-connection", "two:db-connection"] |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When tasks are defined in modules using
from __future__ import annotations(common on Python 3.10),inspect.signature()exposesparam.annotationas a string, so__metadata__is missing and this helper returnsNone. That meansAnnotated[..., Depends(...)]parameters are silently skipped during dependency extraction, and workers later invoke the task without required injected args, raising runtimeTypeErrorfor missing parameters instead of resolving dependencies.Useful? React with 👍 / 👎.