-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Python: Improve error message when TypeVar is used in handler registration #4553
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
Open
ogkranthi
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
ogkranthi:fix/typevar-handler-registration-error-message
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a7dde3
Python: Improve error message when TypeVar is used in handler registr…
8caa9fb
Merge branch 'main' into fix/typevar-handler-registration-error-message
ogkranthi d6790ff
Merge branch 'main' into fix/typevar-handler-registration-error-message
ogkranthi 865a1b1
Address PR review: runtime-safe TypeVar detection and unit tests
b82b61c
Fix pyright error: add type annotation to _TYPEVAR_TYPES
1080b6d
Suppress pyright reportUnknownVariableType for _TYPEVAR_TYPES
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
Some comments aren't visible on the classic Files Changed page.
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
20 changes: 20 additions & 0 deletions
20
python/packages/core/agent_framework/_workflows/_typing_utils.py
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
190 changes: 190 additions & 0 deletions
190
python/packages/core/tests/workflow/test_typevar_validation.py
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,190 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| """Tests for unresolved TypeVar detection during handler/executor registration.""" | ||
|
|
||
| from typing import TypeVar | ||
|
|
||
| import pytest | ||
| from typing_extensions import Never | ||
|
|
||
| from agent_framework import ( | ||
| Executor, | ||
| FunctionExecutor, | ||
| WorkflowContext, | ||
| executor, | ||
| handler, | ||
| ) | ||
| from agent_framework._workflows._typing_utils import is_typevar | ||
|
|
||
| T = TypeVar("T") | ||
| U = TypeVar("U") | ||
|
|
||
|
|
||
| class TestIsTypevarHelper: | ||
| """Tests for the runtime-safe is_typevar helper.""" | ||
|
|
||
| def test_detects_typing_typevar(self): | ||
| """is_typevar should detect TypeVar from typing module.""" | ||
| import typing | ||
|
|
||
| tv = typing.TypeVar("tv") | ||
| assert is_typevar(tv) | ||
|
|
||
| def test_detects_typing_extensions_typevar(self): | ||
| """is_typevar should detect TypeVar from typing_extensions module.""" | ||
| import typing_extensions | ||
|
|
||
| tv = typing_extensions.TypeVar("tv") | ||
| assert is_typevar(tv) | ||
|
|
||
| def test_rejects_concrete_types(self): | ||
| """is_typevar should return False for concrete types.""" | ||
| assert not is_typevar(str) | ||
| assert not is_typevar(int) | ||
| assert not is_typevar(None) | ||
| assert not is_typevar(Never) | ||
|
|
||
| def test_rejects_non_types(self): | ||
| """is_typevar should return False for non-type values.""" | ||
| assert not is_typevar("hello") | ||
| assert not is_typevar(42) | ||
| assert not is_typevar([]) | ||
|
|
||
|
|
||
| class TestHandlerTypeVarValidation: | ||
| """Tests for @handler decorator rejecting unresolved TypeVars.""" | ||
|
|
||
| def test_handler_explicit_input_typevar_raises(self): | ||
| """@handler(input=T) with a TypeVar should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| class _Bad(Executor): # pyright: ignore[reportUnusedClass] | ||
| @handler(input=T) # type: ignore[arg-type] | ||
| async def handle(self, message, ctx: WorkflowContext[str]) -> None: # type: ignore[no-untyped-def] | ||
| pass | ||
|
|
||
| def test_handler_explicit_output_typevar_raises(self): | ||
| """@handler(input=str, output=T) with a TypeVar should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| class _Bad(Executor): # pyright: ignore[reportUnusedClass] | ||
| @handler(input=str, output=T) # type: ignore[arg-type] | ||
| async def handle(self, message: str, ctx: WorkflowContext[str]) -> None: | ||
| pass | ||
|
|
||
| def test_handler_explicit_workflow_output_typevar_raises(self): | ||
| """@handler(input=str, workflow_output=T) should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| class _Bad(Executor): # pyright: ignore[reportUnusedClass] | ||
| @handler(input=str, workflow_output=T) # type: ignore[arg-type] | ||
| async def handle(self, message: str, ctx: WorkflowContext[str]) -> None: | ||
| pass | ||
|
|
||
| def test_handler_introspected_typevar_raises(self): | ||
| """@handler with TypeVar in message annotation should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| class _Bad(Executor): # pyright: ignore[reportUnusedClass] | ||
| @handler # type: ignore[arg-type] | ||
| async def handle(self, message: T, ctx: WorkflowContext[str]) -> None: # type: ignore[valid-type] | ||
| pass | ||
|
|
||
| def test_handler_concrete_types_work(self): | ||
| """@handler with concrete types should succeed.""" | ||
|
|
||
| class Good(Executor): | ||
| @handler(input=str, output=str) | ||
| async def handle(self, message: str, ctx: WorkflowContext[str]) -> None: | ||
| pass | ||
|
|
||
| assert Good is not None | ||
|
|
||
|
|
||
| class TestExecutorTypeVarValidation: | ||
| """Tests for @executor decorator rejecting unresolved TypeVars.""" | ||
|
|
||
| def test_executor_explicit_input_typevar_raises(self): | ||
| """@executor(input=T) with a TypeVar should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| @executor(input=T) # type: ignore[arg-type] | ||
| async def bad_func(message, ctx: WorkflowContext[str]) -> None: # type: ignore[no-untyped-def] | ||
| pass | ||
|
|
||
| def test_executor_explicit_output_typevar_raises(self): | ||
| """@executor(input=str, output=T) with a TypeVar should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| @executor(input=str, output=T) # type: ignore[arg-type] | ||
| async def bad_func(message: str, ctx: WorkflowContext[str]) -> None: | ||
| pass | ||
|
|
||
| def test_executor_introspected_typevar_raises(self): | ||
| """@executor with TypeVar in message annotation should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
| FunctionExecutor(self._make_typevar_func()) # type: ignore[arg-type] | ||
|
|
||
| def test_executor_concrete_types_work(self): | ||
| """@executor with concrete types should succeed.""" | ||
|
|
||
| @executor(input=str, output=str) | ||
| async def good_func(message: str, ctx: WorkflowContext[str]) -> None: | ||
| pass | ||
|
|
||
| assert good_func is not None | ||
|
|
||
| @staticmethod | ||
| def _make_typevar_func(): | ||
| """Create a function with TypeVar annotation for testing.""" | ||
|
|
||
| async def func(message: T, ctx: WorkflowContext[str]) -> None: # type: ignore[valid-type] | ||
| pass | ||
|
|
||
| return func | ||
|
|
||
|
|
||
| class TestWorkflowContextTypeVarValidation: | ||
| """Tests for WorkflowContext[T] rejecting unresolved TypeVars.""" | ||
|
|
||
| def test_context_direct_typevar_raises(self): | ||
| """WorkflowContext[T] with a TypeVar should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| @executor(id="bad") | ||
| async def bad_func(message: str, ctx: WorkflowContext[T]) -> None: # type: ignore[valid-type] | ||
| pass | ||
|
|
||
| def test_context_union_typevar_raises(self): | ||
| """WorkflowContext[T | str] with a TypeVar in union should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| @executor(id="bad") | ||
| async def bad_func(message: str, ctx: WorkflowContext[T | str]) -> None: # type: ignore[valid-type] | ||
| pass | ||
|
|
||
| def test_context_workflow_output_typevar_raises(self): | ||
| """WorkflowContext[str, T] with a TypeVar should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| @executor(id="bad") | ||
| async def bad_func(message: str, ctx: WorkflowContext[str, T]) -> None: # type: ignore[valid-type] | ||
| pass | ||
|
|
||
| def test_context_concrete_types_work(self): | ||
| """WorkflowContext[str] with concrete types should succeed.""" | ||
|
|
||
| @executor(id="good") | ||
| async def good_func(message: str, ctx: WorkflowContext[str]) -> None: | ||
| pass | ||
|
|
||
| assert good_func is not None | ||
|
|
||
| def test_context_class_handler_typevar_raises(self): | ||
| """Class-based handler with WorkflowContext[T] should raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unresolved TypeVar"): | ||
|
|
||
| class _Bad(Executor): # pyright: ignore[reportUnusedClass] | ||
| @handler # pyright: ignore[reportUnknownArgumentType] | ||
| async def handle(self, message: str, ctx: WorkflowContext[T]) -> None: # type: ignore[valid-type] | ||
| pass |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.