Skip to content

feat(python/builder,cli): type-check FunctionCall returnType (#3607) - #4531

Merged
jacobsimionato merged 1 commit into
macros/2-checks-a11y-paramsfrom
macros/4-typed-function-calls
Dec 3, 2026
Merged

jacobsimionato merged 1 commit into
macros/2-checks-a11y-paramsfrom
macros/4-typed-function-calls

Conversation

@jacobsimionato

@jacobsimionato jacobsimionato commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

In A2UI Python builders, every generated catalog function helper (such as FormatString, Required, or OpenUrl) returned an untyped FunctionCall. As a result, passing a mismatched function return type—such as a void action function into a DynamicString property (a property accepting a literal string, data binding, or string-returning function call) or a string function into a CheckRule (a validation check)—passed both static type checking (mypy) and Pydantic runtime validation, failing only on the client renderer.

Closes #3607

Summary

  1. Enforce FunctionCall return types statically (mypy) and at runtime (Pydantic): Introduce 8 return-typed FunctionCall aliases backed by nominal return-type sentinel classes so Python's bool <: int and str <: Sequence[str] subtype rules cannot silently leak mismatched calls.
  2. Keep unparameterized FunctionCall ergonomic at runtime: Keep FunctionCall aliased directly to the core Pydantic model at runtime (avoiding Pydantic v2 generic-subclass coercion failures when passing raw FunctionCall(...)), while declaring generic FunctionCall[_ReturnT] under if TYPE_CHECKING: for static checks and attaching Pydantic AfterValidator checks to Dynamic* property aliases and v1_0.CheckRule.
  3. Emit return-typed function helpers from dart/a2ui_cli: Propagate JSON Schema returnType through CatalogAnalyzer and PythonEmitter, regenerating v0.9 and v1.0 basic catalog function helpers and aligning a2ui-core's schema validator with declared return types.

Breaking changes: None for valid calls. Return-type mismatches (such as passing a VoidFunctionCall where a string or boolean is expected) now fail fast under both mypy and Pydantic validation.

New public API

from a2ui.builder import v1_0 as b

# Accepted statically and at runtime (FormatString -> StringFunctionCall):
ok = b.Text(text=b.FormatString(value="Hello"))

# Rejected by BOTH mypy and Pydantic runtime validation (OpenUrl -> VoidFunctionCall):
# bad = b.Text(text=b.OpenUrl(url="https://example.com"))
Symbol (a2ui.builder.v0_9 & v1_0) Role
StringFunctionCall, NumberFunctionCall, BooleanFunctionCall Return-typed function calls accepted by matching Dynamic* component properties
ValidationResultFunctionCall, ArrayFunctionCall, ObjectFunctionCall, VoidFunctionCall Return-typed calls for CheckRule, DynamicStringList, object slots, and Actions
AnyFunctionCall Wildcard return-typed call for dynamic or untyped custom functions

Diff breakdown & where to review

Category Files Diff Summary
Production logic 10 +535 / -22 Nominal return-type sentinels (builder/core/function_call.py), v0.9/v1.0 models & validators, a2ui_core return-type check, and dart/a2ui_cli emitter
Generated builders & goldens 6 +124 / -28 Regenerated v0.9 & v1.0 basic.py function helpers and 4 Dart CLI goldens
Tests & conformance 3 +227 / -1 Runtime rejection + mypy.api static rejection tests for v0.9 & v1.0 plus Dart CLI tests
Docs & changelogs 2 +14 / -0 python/a2ui_agent/CHANGELOG.md and python/a2ui_core/CHANGELOG.md
Total 21 +900 / -51 513 net prod lines (+535 / -22) across 10 files

Start here

  1. Nominal sentinels & runtime validator (python/a2ui_agent/src/a2ui/builder/core/function_call.py): Verify the nominal sentinel hierarchy and confirm the Pydantic runtime validator accepts unannotated raw FunctionCall while rejecting declared return-type mismatches.
  2. Versioned builder models (python/a2ui_agent/src/a2ui/builder/v1_0/models.py, v0_9/models.py): Verify the if TYPE_CHECKING: generic FunctionCall[_ReturnT] declaration and Annotated[FunctionCall, ...] runtime AfterValidator wiring on Dynamic* and v1_0.CheckRule.
  3. Codegen (dart/a2ui_cli/lib/src/emitters/python/python_emitter.dart): Check how catalog function returnType maps 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 Pydantic AfterValidator helper validate_function_return_type.
  • python/a2ui_agent/src/a2ui/builder/v0_9/models.py, v1_0/models.py, and __init__.py barrels: Exports all 8 return-typed FunctionCall aliases; keeps FunctionCall bound to the concrete Pydantic model at runtime and declares generic FunctionCall[_ReturnT] with keyword-init field annotations (call, args, catalog_id) under if TYPE_CHECKING: so mypy @dataclass_transform validates both FunctionCall(call=...) and return-type compatibility.
  • python/a2ui_core/src/a2ui/core/schema/_json_schema.py: Honors _declared_return_type in ReturnType._validate when validating builder function calls.
  • python/a2ui_agent/src/a2ui/transformers/macros/macro.py: Unwraps Annotated[FunctionCall, ...] cleanly during macro schema synthesis.
  • dart/a2ui_cli/lib/src/analyzer/types.dart, catalog_analyzer.dart, python_emitter.dart: Adds FunctionReturnKind on AnalysedFunctionApi and emits typed return annotations + _declared_return_type metadata 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 and mypy.api.run static 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 passed
  • uv run mypy .: 0 errors across 246 source files
  • uv 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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 473 to 475
if "return_type" in call.model_fields_set:
if call.return_type != self.expected:
raise ValueError(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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(

@jacobsimionato jacobsimionato changed the title feat(python/builder,cli): enforce FunctionCall returnType statically and at runtime (#3607) feat(python/builder,cli): type-check FunctionCall returnType (#3607) Dec 2, 2026
@jacobsimionato
jacobsimionato removed this pull request from stack #4536 December 2, 2026 23:18
…and at runtime (#3607)

Signed-off-by: Jacob Simionato <jsimionato@google.com>
@jacobsimionato
jacobsimionato removed this pull request from stack #4552 December 3, 2026 12:03
@jacobsimionato
jacobsimionato force-pushed the macros/4-typed-function-calls branch from d44dcd6 to 8b946e3 Compare December 3, 2026 12:04
@jacobsimionato
jacobsimionato force-pushed the macros/3-builder-ids-and-cli branch from 4c524d7 to 69fae65 Compare December 3, 2026 12:04
Base automatically changed from macros/3-builder-ids-and-cli to macros/2-checks-a11y-params December 3, 2026 12:04
@jacobsimionato
jacobsimionato merged commit 8b946e3 into macros/2-checks-a11y-params Dec 3, 2026
@jacobsimionato
jacobsimionato deleted the macros/4-typed-function-calls branch December 3, 2026 12:04
@jacobsimionato

Copy link
Copy Markdown
Collaborator Author

GitHub marked this PR as merged when I reordered the stack: its commit became reachable from the old base branch. Nothing landed on main. The change continues in #4565, part of the reordered macros stack (https://github.com/a2ui-project/a2ui/stack/4568).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant