Python: Improve error message when TypeVar is used in handler registration#4553
Python: Improve error message when TypeVar is used in handler registration#4553ogkranthi wants to merge 6 commits intomicrosoft:mainfrom
Conversation
…ation Fixes microsoft#4547. Adds early detection of unresolved TypeVar instances in: - @handler decorator (both explicit and introspected type paths) - @executor decorator (both explicit and introspected type paths) - WorkflowContext type argument validation (direct and union members) When a TypeVar is detected, a clear ValueError is raised with actionable guidance to use concrete types via @handler(input=ConcreteType, output=ConcreteType).
Python Test Coverage Report •
Python Unit Test Overview
|
|||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
Adds earlier, more actionable validation errors when unresolved TypeVar objects are used in workflow handler/executor registration, aiming to fail fast (during registration) rather than later during workflow edge/type validation.
Changes:
- Add
TypeVardetection in@handlerregistration for both explicit decorator parameters and introspected annotations. - Add
TypeVardetection inFunctionExecutorregistration for both explicit decorator parameters and introspected annotations. - Add
TypeVardetection inWorkflowContext[...]generic argument validation, including union members.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_workflows/_workflow_context.py | Adds early TypeVar checks for WorkflowContext type arguments (direct + union members) to raise clearer ValueErrors. |
| python/packages/core/agent_framework/_workflows/_function_executor.py | Adds early TypeVar checks for @executor registration (explicit params + introspected message type). |
| python/packages/core/agent_framework/_workflows/_executor.py | Adds early TypeVar checks for @handler registration (explicit params + introspected message type). |
You can also share your feedback on Copilot code review. Take the survey.
|
@copilot open a new pull request to apply changes based on the comments in this thread |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
You can also share your feedback on Copilot code review. Take the survey.
- Add shared is_typevar() helper in _typing_utils.py that safely detects TypeVar from both typing and typing_extensions modules - Replace all isinstance(x, TypeVar) calls with is_typevar() in _executor.py, _function_executor.py, and _workflow_context.py - Add 18 unit tests covering TypeVar validation for @handler, @executor, and WorkflowContext[T] (explicit params, introspection, union members)
Pyright's reportUnknownVariableType flagged the inferred type as partially unknown. Adding an explicit `tuple[type, ...]` annotation resolves the strict-mode check.
|
@markwallace-microsoft This PR is ready for review — all review threads are resolved, the pyright CI issue has been fixed, and tests are passing. Could you take a look when you get a chance? |
Pyright cannot infer the runtime type of TypeVar constructors, so the tuple elements resolve to type[Unknown]. A type annotation alone does not satisfy strict mode — add an inline suppression for this specific diagnostic since the unknown types are intentional (runtime TypeVar class detection).
Summary
TypeVarinstances during handler/executor registration, surfacing a clearValueErrorwith actionable guidance instead of a confusingTypeCompatibilityErrorat edge validation time@handler,@executor, andWorkflowContext[T]type argument validation (both direct and union members)Fixes #4547
Changes
_executor.py— TypeVar check in@handlerdecorator for both explicit type params and introspected annotation paths_function_executor.py— TypeVar check inFunctionExecutor.__init__for both explicit and introspected paths_workflow_context.py— TypeVar check invalidate_workflow_context_annotationfor direct type args and union membersTest plan
@handler(input=SomeTypeVar)raisesValueErrorwith actionable messageTypeVarannotation (introspection path) raisesValueError@executor(input=SomeTypeVar)raisesValueErrorWorkflowContext[SomeTypeVar]raisesValueError