Repository navigation
fix(python/builder,cli): fix ID collisions and CLI schema parity - #4530
Conversation
There was a problem hiding this comment.
Code Review
This pull request enhances the A2UI code generator and Python runtime by adding support for schema constraints (such as minimum/maximum values and item counts), introducing strict Dynamic* type aliases, preserving required nullable fields as JSON null, and resolving component ID collisions during flattening and macro expansion. The code review feedback focuses on optimizing the Dart analyzer and Python emitter code by leveraging Dart's local variable type promotion to eliminate redundant map lookups and explicit type casts.
| final num? minimum = schema['minimum'] is num | ||
| ? schema['minimum'] as num | ||
| : null; | ||
| final num? maximum = schema['maximum'] is num | ||
| ? schema['maximum'] as num | ||
| : null; | ||
| final int? minItems = schema['minItems'] is int | ||
| ? schema['minItems'] as int | ||
| : null; | ||
| final int? maxItems = schema['maxItems'] is int | ||
| ? schema['maxItems'] as int | ||
| : null; |
There was a problem hiding this comment.
By assigning the schema values to local variables, we can avoid redundant map lookups and leverage Dart's type promotion to eliminate the explicit as num and as int casts.
| final num? minimum = schema['minimum'] is num | |
| ? schema['minimum'] as num | |
| : null; | |
| final num? maximum = schema['maximum'] is num | |
| ? schema['maximum'] as num | |
| : null; | |
| final int? minItems = schema['minItems'] is int | |
| ? schema['minItems'] as int | |
| : null; | |
| final int? maxItems = schema['maxItems'] is int | |
| ? schema['maxItems'] as int | |
| : null; | |
| final minimumVal = schema['minimum']; | |
| final num? minimum = minimumVal is num ? minimumVal : null; | |
| final maximumVal = schema['maximum']; | |
| final num? maximum = maximumVal is num ? maximumVal : null; | |
| final minItemsVal = schema['minItems']; | |
| final int? minItems = minItemsVal is int ? minItemsVal : null; | |
| final maxItemsVal = schema['maxItems']; | |
| final int? maxItems = maxItemsVal is int ? maxItemsVal : null; |
References
- Avoid unnecessary type casting when accessing properties that can be strictly typed via local variable type promotion.
| final bool isAny = | ||
| resolved.type is PrimitiveType && | ||
| (resolved.type as PrimitiveType).primitive == PrimitiveKind.any; |
There was a problem hiding this comment.
Assigning resolved.type to a local variable allows Dart's type promotion to automatically type-promote it to PrimitiveType after the is PrimitiveType check, eliminating the need for the explicit as PrimitiveType cast.
| final bool isAny = | |
| resolved.type is PrimitiveType && | |
| (resolved.type as PrimitiveType).primitive == PrimitiveKind.any; | |
| final resolvedType = resolved.type; | |
| final bool isAny = resolvedType is PrimitiveType && | |
| resolvedType.primitive == PrimitiveKind.any; |
References
- Access properties directly without type casting if they are already strictly typed via getters on the underlying class.
| if (schema['enum'] is List) { | ||
| final List<String> values = (schema['enum'] as List) | ||
| .map((e) => e.toString()) | ||
| .toList(); | ||
| var preferred = '$parentName${capitalize(propName)}'; | ||
| if ((parentName == 'Row' || parentName == 'Column') && | ||
| (propName == 'justify' || propName == 'align')) { | ||
| preferred = 'Flex${capitalize(propName)}'; | ||
| } | ||
| final rawValues = schema['enum'] as List; | ||
| final bool hasNull = rawValues.any((e) => e == null); | ||
| final List<Object> values = rawValues.whereType<Object>().toList(); |
There was a problem hiding this comment.
Assigning schema['enum'] to a local variable avoids redundant map lookups and allows Dart's type promotion to automatically promote it to List after the is List check, eliminating the need for the explicit as List cast.
| if (schema['enum'] is List) { | |
| final List<String> values = (schema['enum'] as List) | |
| .map((e) => e.toString()) | |
| .toList(); | |
| var preferred = '$parentName${capitalize(propName)}'; | |
| if ((parentName == 'Row' || parentName == 'Column') && | |
| (propName == 'justify' || propName == 'align')) { | |
| preferred = 'Flex${capitalize(propName)}'; | |
| } | |
| final rawValues = schema['enum'] as List; | |
| final bool hasNull = rawValues.any((e) => e == null); | |
| final List<Object> values = rawValues.whereType<Object>().toList(); | |
| final enumVal = schema['enum']; | |
| if (enumVal is List) { | |
| final bool hasNull = enumVal.any((e) => e == null); | |
| final List<Object> values = enumVal.whereType<Object>().toList(); |
References
- Avoid unnecessary type casting when accessing properties that can be strictly typed via local variable type promotion.
| final bool isNumericPrimitive = | ||
| prop.type is PrimitiveType && | ||
| ((prop.type as PrimitiveType).primitive == PrimitiveKind.integer || | ||
| (prop.type as PrimitiveType).primitive == PrimitiveKind.float); |
There was a problem hiding this comment.
Assigning prop.type to a local variable allows Dart's type promotion to automatically type-promote it to PrimitiveType after the is PrimitiveType check, eliminating the need for the explicit as PrimitiveType casts and redundant property accesses.
final propType = prop.type;
final bool isNumericPrimitive = propType is PrimitiveType &&
(propType.primitive == PrimitiveKind.integer ||
propType.primitive == PrimitiveKind.float);References
- Access properties directly without type casting if they are already strictly typed via getters on the underlying class.
1038deb to
6dd00f1
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
Python's typesafe component builders—generated by
dart/a2ui_cliand flattened into flat A2UI wire lists in two passes at runtime—suffered from silent component-ID collisions across multi-root trees, duplicate explicit sibling IDs, explicit ID references (ComponentRef), and macro expansions. At the same time,dart/a2ui_cli's Python emitter dropped several JSON Schema constraints and catalog metadata fields, accepted invalidFunctionCallvalues onIcon.name, and lacked structural hooks needed for future wire deserialization.Summary
root_0,root_1, … inside a shared flattening context, fail loud withA2uiValidationErroron duplicate explicit sibling IDs, reserveComponentReftargets during the first pass before allocating automatic IDs, and passreserved_idsfromMacroExpanderintoflatten_component_tree.preserve_ids,dangling_components(unattached components carried alongside a tree), andcatalog_idtoComponentTree, preserve required nullableNonevalues as explicit JSONnull, emitCATALOG_ID/CATALOG_COMPONENTS/ complete__all__in generated catalogs, and add__all__toa2ui.builder.v0_9.models.dart/a2ui_cliPython codegen schema gaps: Emit strict data-bindingDynamic*aliases, splitDataBindingvsFunctionCallin unions (fixingIcon.name), preserveintegerandminimum/minItemsconstraints (Slider.stepsint >= 1,Tabs.tabsmin_length=1), inspectallOfschemas (DateTimeInput.min/max->DynamicString), and preserve numeric/boolean enums and required nullable fields (fix(cli): preserve Python enum types and nullable fields #4189, fix(python): preserve required null fields in builder serialization #4190).Breaking changes: None for valid builder trees. Two previously broken patterns now fail fast instead of silently emitting corrupt wire payloads: duplicate explicit sibling IDs raise
A2uiValidationError, andIcon.namerejectsFunctionCall.New public API
Diff breakdown & where to review
+592 / -106flattener.py,id_allocator.py,tree.py,base_model.py,child.py),expander.py, anddart/a2ui_clianalyzer/emitter+288 / -146v0_9/v1_0basic.pycatalogs and 4dart/a2ui_clifixture goldens+281 / -12+13 / -0python/a2ui_agent/CHANGELOG.md+1174 / -264+592 / -106) across 12 filesStart here
python/a2ui_agent/src/a2ui/builder/core/flattener.py): Verify first-pass reservation of explicit IDs andComponentReftargets, and confirm fatal collision errors recorded onFlattenContext.errorre-raise cleanly (since Pydantic union serialization otherwise swallows raised errors).python/a2ui_agent/src/a2ui/builder/core/base_model.py): Check the serializer hook ensuring optionalNonefields stay omitted while required nullable fields serialize as JSONnull.dart/a2ui_cli/lib/src/analyzer/catalog_analyzer.dart,dart/a2ui_cli/lib/src/emitters/python/python_emitter.dart): VerifyallOfmerging, integer/constraintField(...)emission, andDataBinding/FunctionCallunion splitting.Implementation details
python/a2ui_agent/src/a2ui/builder/core/):flattener.py&id_allocator.py: Scopes multi-root sequences (root_id=None) asroot_0,root_1, … within one sharedFlattenContext. Detects duplicate explicit sibling IDs andComponentRefcollisions during Pass 1 and records fatal errors onFlattenContext.errorso Pydantic v2PlainSerializerinsideUnion[Sequence[Child], DynamicChildList]cannot swallowA2uiValidationError. Acceptsreserved_ids: AbstractSet[str] | None.tree.py: Addspreserve_ids: bool = False,dangling_components: Sequence[ComponentBuilderNode] = (), andcatalog_id: str | None = NonetoComponentTree.base_model.py: Adds_preserve_required_noneonBuilderBaseModelto keep required nullable fields set toNoneas explicit JSONnullwhile excluding unset/optionalNonefields.v0_9/models.py: Adds explicit__all__.python/a2ui_agent/src/a2ui/transformers/macros/):reserved_idsintoflatten_component_treeso auto-allocated macro subcomponents never overwrite sibling components on the same surface.dart/a2ui_cli/):catalog_analyzer.dart&types.dart: DistinguishesPrimitiveKind.integerfromfloat, readsminimum/maximum/minItems/maxItems, mergesallOfproperty schemas (catchingDateTimeInput.min/max->DynamicString), splitsDataBindingandFunctionCallrefs cleanly (fixingIcon.name), preserves numeric/boolean enums and required nullable fields (fix(cli): preserve Python enum types and nullable fields #4189, fix(python): preserve required null fields in builder serialization #4190).python_emitter.dart&type_mapper.dart: EmitsCATALOG_ID,CATALOG_COMPONENTS, complete runtime imports,Field(ge=..., min_length=...)constraints, strictDynamic*aliases, and__all__.python/a2ui_agent/src/a2ui/builder/v0_9/catalogs/basic.py,v1_0/catalogs/basic.py, and 4 Dart CLI fixture goldens; fixed brokeneventimport intest_pydantic_builders.py.Verification — dart format, dart analyze --fatal-infos, 114 Dart tests, pyink, mypy (0 errors), and 355 Python tests passed
cd dart/a2ui_cli && dart format --output=none --set-exit-if-changed lib/ test/ && dart analyze --fatal-infos && dart test: 0 analyzer issues, 114 passeduv run pyink --check&uv run mypy .: 0 errors across 245 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: 355 passed