Repository navigation
fix(codegen): find postal union arms by shape and pin their names - #1939
Conversation
datamodel-codegen names classes across the whole schema tree and walks bundled/ before core/. adcp#9049 made four bundled protocol schemas inline postal-country-system.json, so those modules now take PostalArea through PostalArea71 and core/postal_area.py's outer arms shift from PostalArea1 | PostalArea2 to PostalArea40 | PostalArea41. fix_postal_union_arm_order string-matched the old names, so every adcp "Generate Python SDK" run on a bundle containing #9049 failed with "generated postal_area.py has an unexpected outer union shape". Matching the new names alone is not enough: aliases.PostalArea imports PostalArea1/PostalArea2 and the deprecated GeoPostalArea resolves to PostalArea2, so import adcp.types fails next. The fix now picks the legacy arm by shape (a plain model whose system is the legacy_postal_system enum, with no country), pins the arms back to PostalArea1 (native) and PostalArea2 (legacy) by identifier-token rename, and then puts the legacy arm first. It still refuses any other outer shape, and refuses a pin that would clash with an existing name. On the current bundle the generated tree is unchanged. Refs #1935 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Ladon verdict: Approve
Approve — codegen fix with comprehensive tests, no blocking or medium findings.
This PR reworks fix_postal_union_arm_order in scripts/post_generate_fixes.py to locate postal union arms by shape (_postal_union_arms/_is_legacy_postal_arm) and pin them to PostalArea1/PostalArea2, surviving datamodel-codegen renumbering (adcp#9049). Verified:
- No wire-shape, public-API, or import-layering impact; generated tree is byte-identical on the pinned bundle and validated against the real #9049 bundle.
- Fail-closes with RuntimeError on pin clash or unexpected union shape rather than emitting wrong code; reverse-ordered token-level rename leaves strings/comments untouched; idempotent.
- Tests are comprehensive (renumbering, prefix decoys, swap, clash, 0/3-arm rejection).
Decision path: no critical/high/medium findings; high_risk false; gated_paths false; no author team gate; no prior decision. Rows 1–8 do not fire → row 9 approve.
Evidence: adcp "Generate Python SDK" job, run locallyThe job's
Key lines from the passing run (#9170 bundle + this PR): This matches adcp CI. The last pass was the Version Packages head on |
Summary
adcp's Generate Python SDK check (
validate-schema-bundle.yml, which checks out this repo'snext) fails on every adcp PR whose bundle contains adcontextprotocol/adcp#9049:The cause is generator renumbering, not a schema defect. datamodel-codegen names classes across the whole schema tree and walks
bundled/beforecore/. #9049 makes four bundled protocol schemas (get-adcp-capabilities-response,get-principal-response,sync-principal-request,sync-principal-response) inlinecore/postal-country-system.json. Those modules now takePostalAreathroughPostalArea71, socore/postal_area.py's outer arms move fromPostalArea1 | PostalArea2toPostalArea40 | PostalArea41.The fixer string-matched the old names. Renaming the match isn't enough either:
aliases.PostalAreaimportsPostalArea1andPostalArea2from_generated, and the deprecatedGeoPostalArearesolves toPostalArea2. With the shifted names,import adcp.typesfails next.Change
fix_postal_union_arm_order(scripts/post_generate_fixes.py) now:PostalArearoot, and the legacy arm is the plain model whosesystemis thelegacy_postal_systemenum and which declares nocountry(_postal_union_arms,_is_legacy_postal_arm). It still raises "unexpected outer union shape" for anything other than one legacy arm plus one other arm.PostalArea1and the legacy armPostalArea2, by identifier token via the existing_rename_code_names, so strings and comments are untouched and a swap works. It refuses if a target name is already taken.PostalArea1 | PostalArea2toPostalArea2 | PostalArea1, so legacy payloads withoutcountrydon't get a defaulted country injected.This stays one fix, so
post_generation_manifest.jsondoesn't change: the fix fires on both today's bundle and the new one. On the current pinned bundle (3.3.0-rc.0) the generated tree is byte-identical.Refs #1935 (this covers item 2; the
selector.dimension_systemsmatching in item 1 is separate).Verification
tests/test_code_generation.py:PostalArea40/41), with prefix decoys (PostalArea410,PostalArea4012 | PostalArea4013) andPostalArea40inside strings and comments that must stay unchanged;pytest tests/test_code_generation.py tests/test_lazy_types.py: 178 passed.make lintandmake typecheck-allpass.validate-schema-bundle.yml, against bundles built from adcp main and from adcp#9170's head. Output is in the PR comments.🤖 Generated with Claude Code