Repository navigation
feat(python/builder,cli): type-check FunctionCall returnType (#3607) - #4531
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces return-typed FunctionCall subclasses (such as StringFunctionCall, BooleanFunctionCall, etc.) to enforce return type validation at both runtime (via Pydantic) and statically (via mypy) for catalog functions in versions 0.9 and 1.0. The Dart analyzer and Python code generator have been updated to output these typed subclasses, and corresponding tests and goldens have been updated. Feedback on the changes points out an inconsistency in _json_schema.py where the explicit return_type check does not allow "validationResult" when "boolean" is expected, which should be updated to match the compatibility logic of the newly added _declared_return_type check.
| if "return_type" in call.model_fields_set: | ||
| if call.return_type != self.expected: | ||
| raise ValueError( |
There was a problem hiding this comment.
In _validate, when call.return_type is explicitly set to "validationResult" and self.expected is "boolean", the check call.return_type != self.expected will raise a ValueError. However, "validationResult" is a compatible return type for a "boolean" expected type (as handled in the _declared_return_type check above). We should update this check to also allow "validationResult" when self.expected is "boolean".
if "return_type" in call.model_fields_set:
compatible = call.return_type == self.expected or (
self.expected == "boolean" and call.return_type == "validationResult"
)
if not compatible:
raise ValueError(…and at runtime (#3607) Signed-off-by: Jacob Simionato <jsimionato@google.com>
d44dcd6 to
8b946e3
Compare
4c524d7 to
69fae65
Compare
|
GitHub marked this PR as merged when I reordered the stack: its commit became reachable from the old base branch. Nothing landed on |
Problem
In A2UI Python builders, every generated catalog function helper (such as
FormatString,Required, orOpenUrl) returned an untypedFunctionCall. As a result, passing a mismatched function return type—such as avoidaction function into aDynamicStringproperty (a property accepting a literal string, data binding, or string-returning function call) or astringfunction into aCheckRule(a validation check)—passed both static type checking (mypy) and Pydantic runtime validation, failing only on the client renderer.Closes #3607
Summary
FunctionCallreturn types statically (mypy) and at runtime (Pydantic): Introduce 8 return-typedFunctionCallaliases backed by nominal return-type sentinel classes so Python'sbool <: intandstr <: Sequence[str]subtype rules cannot silently leak mismatched calls.FunctionCallergonomic at runtime: KeepFunctionCallaliased directly to the core Pydantic model at runtime (avoiding Pydantic v2 generic-subclass coercion failures when passing rawFunctionCall(...)), while declaring genericFunctionCall[_ReturnT]underif TYPE_CHECKING:for static checks and attaching PydanticAfterValidatorchecks toDynamic*property aliases andv1_0.CheckRule.dart/a2ui_cli: Propagate JSON SchemareturnTypethroughCatalogAnalyzerandPythonEmitter, regenerating v0.9 and v1.0 basic catalog function helpers and aligninga2ui-core's schema validator with declared return types.Breaking changes: None for valid calls. Return-type mismatches (such as passing a
VoidFunctionCallwhere a string or boolean is expected) now fail fast under bothmypyand Pydantic validation.New public API
a2ui.builder.v0_9&v1_0)StringFunctionCall,NumberFunctionCall,BooleanFunctionCallDynamic*component propertiesValidationResultFunctionCall,ArrayFunctionCall,ObjectFunctionCall,VoidFunctionCallCheckRule,DynamicStringList, object slots, andActionsAnyFunctionCallDiff breakdown & where to review
+535 / -22builder/core/function_call.py), v0.9/v1.0 models & validators,a2ui_corereturn-type check, anddart/a2ui_cliemitter+124 / -28basic.pyfunction helpers and 4 Dart CLI goldens+227 / -1mypy.apistatic rejection tests for v0.9 & v1.0 plus Dart CLI tests+14 / -0python/a2ui_agent/CHANGELOG.mdandpython/a2ui_core/CHANGELOG.md+900 / -51+535 / -22) across 10 filesStart here
python/a2ui_agent/src/a2ui/builder/core/function_call.py): Verify the nominal sentinel hierarchy and confirm the Pydantic runtime validator accepts unannotated rawFunctionCallwhile rejecting declared return-type mismatches.python/a2ui_agent/src/a2ui/builder/v1_0/models.py,v0_9/models.py): Verify theif TYPE_CHECKING:genericFunctionCall[_ReturnT]declaration andAnnotated[FunctionCall, ...]runtimeAfterValidatorwiring onDynamic*andv1_0.CheckRule.dart/a2ui_cli/lib/src/emitters/python/python_emitter.dart): Check how catalog functionreturnTypemaps to each typed alias and stamps the declared return-type metadata attribute.Implementation details
python/a2ui_agent/src/a2ui/builder/core/function_call.py: Defines nominal sentinel types (ValueReturn,StringReturn,NumberReturn,BooleanReturn,ValidationResultReturn,ArrayReturn,ObjectReturn,VoidReturn),DECLARED_RETURN_TYPE_ATTR,with_declared_return_type, and PydanticAfterValidatorhelpervalidate_function_return_type.python/a2ui_agent/src/a2ui/builder/v0_9/models.py,v1_0/models.py, and__init__.pybarrels: Exports all 8 return-typedFunctionCallaliases; keepsFunctionCallbound to the concrete Pydantic model at runtime and declares genericFunctionCall[_ReturnT]with keyword-init field annotations (call,args,catalog_id) underif TYPE_CHECKING:so mypy@dataclass_transformvalidates bothFunctionCall(call=...)and return-type compatibility.python/a2ui_core/src/a2ui/core/schema/_json_schema.py: Honors_declared_return_typeinReturnType._validatewhen validating builder function calls.python/a2ui_agent/src/a2ui/transformers/macros/macro.py: UnwrapsAnnotated[FunctionCall, ...]cleanly during macro schema synthesis.dart/a2ui_cli/lib/src/analyzer/types.dart,catalog_analyzer.dart,python_emitter.dart: AddsFunctionReturnKindonAnalysedFunctionApiand emits typed return annotations +_declared_return_typemetadata on every generated catalog function helper.python/a2ui_agent/tests/test_pydantic_builders.py&test_pydantic_builders_v1_0.py: Tests both Pydantic runtime rejection andmypy.api.runstatic rejection across v0.9 and v1.0.Verification — 114 Dart CLI tests, mypy (0 errors across 246 files), and 332 Python tests passed
cd dart/a2ui_cli && dart format --output=none --set-exit-if-changed lib/ test/ && dart analyze --fatal-infos && dart test: 114 passeduv run mypy .: 0 errors across 246 source filesuv run pytest python/a2ui_agent/tests/test_pydantic_builders.py python/a2ui_agent/tests/test_pydantic_builders_v1_0.py python/a2ui_agent/tests/test_macros.py: 332 passed