Skip to content

Commit 56d65fd

Browse files
bokelleysangilish
andauthored
fix(codegen): preserve regenerated response aliases (#854)
* fix(codegen): preserve regenerated response aliases * test: pin semantic response aliases * fix: satisfy mypy for codegen aliases * fix: wrap response alias fallbacks --------- Co-authored-by: sangilish <56685007+sangilish@users.noreply.github.com>
1 parent e4b716d commit 56d65fd

31 files changed

Lines changed: 363 additions & 181 deletions

scripts/consolidate_exports.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def extract_exports_from_module(module_path: Path) -> set[str]:
2828

2929
exports = set()
3030

31+
def _add_public_type_name(name: str) -> None:
32+
if name and not name.startswith("_") and name[0].isupper():
33+
exports.add(name)
34+
3135
# Only look at module-level nodes (not inside classes)
3236
for node in tree.body:
3337
# Class definitions
@@ -39,8 +43,11 @@ def extract_exports_from_module(module_path: Path) -> set[str]:
3943
for target in node.targets:
4044
if isinstance(target, ast.Name) and not target.id.startswith("_"):
4145
# Only export if it looks like a type name (starts with capital)
42-
if target.id and target.id[0].isupper():
43-
exports.add(target.id)
46+
_add_public_type_name(target.id)
47+
elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
48+
# ``Foo: TypeAlias = ...`` is common in post-generated response
49+
# unions; keep those public aliases in the consolidated namespace.
50+
_add_public_type_name(node.target.id)
4451

4552
return exports
4653

@@ -484,11 +491,11 @@ def _stem_matches_export(module_stem: str, export_name: str) -> bool:
484491
content = "\n".join(lines)
485492
# Product is generated as a RootModel union, but the SDK's public Product
486493
# export intentionally points at the concrete first arm for subclassing.
487-
# Avoid importing the RootModel under the public name so mypy sees the
488-
# later compatibility assignment as a plain class alias, not a class
489-
# redefinition.
494+
# Avoid importing the RootModel so mypy sees the later compatibility
495+
# assignment as the first public Product binding, not a class redefinition.
490496
content = content.replace(
491-
" Product,\n Product1,\n", " Product as ProductUnion,\n Product1,\n"
497+
" Product,\n Product1,\n",
498+
" Product1,\n",
492499
)
493500
return content
494501

scripts/post_generate_fixes.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ def _load_resolve_bundle_key():
4646
OUTPUT_DIR = REPO_ROOT / "src" / "adcp" / "types" / "generated_poc"
4747
SCHEMA_DIR = REPO_ROOT / "schemas" / "cache" / _BUNDLE_KEY
4848

49+
_PROTOCOL_ENVELOPE_IMPORT = "from ..core.protocol_envelope import ProtocolEnvelope\n"
50+
_VERSION_ENVELOPE_IMPORT = "from ..core.version_envelope import AdcpVersionEnvelope\n"
51+
52+
53+
def _sync_protocol_envelope_import(source: str) -> str:
54+
"""Keep the ProtocolEnvelope import aligned with restored response arms."""
55+
uses_protocol_envelope = "ProtocolEnvelope" in source.replace(_PROTOCOL_ENVELOPE_IMPORT, "")
56+
if not uses_protocol_envelope:
57+
return source.replace(_PROTOCOL_ENVELOPE_IMPORT, "")
58+
if _PROTOCOL_ENVELOPE_IMPORT in source:
59+
return source
60+
if _VERSION_ENVELOPE_IMPORT in source:
61+
return source.replace(
62+
_VERSION_ENVELOPE_IMPORT,
63+
_PROTOCOL_ENVELOPE_IMPORT + _VERSION_ENVELOPE_IMPORT,
64+
1,
65+
)
66+
future_import = "from __future__ import annotations\n\n"
67+
if future_import in source:
68+
return source.replace(future_import, future_import + _PROTOCOL_ENVELOPE_IMPORT, 1)
69+
return _PROTOCOL_ENVELOPE_IMPORT + source
70+
4971

5072
def add_model_validator_to_product():
5173
"""Add model_validators to Product class.
@@ -1593,15 +1615,12 @@ def restore_response_variant_aliases() -> None:
15931615

15941616
def _remove_original_response_class(source: str, base: str) -> str:
15951617
"""Remove the generator's envelope-only class before restoring a union alias."""
1596-
protocol_import = "from ..core.protocol_envelope import ProtocolEnvelope\n"
15971618
source = re.sub(
15981619
rf"\n\nclass {re.escape(base)}\(AdcpVersionEnvelope, ProtocolEnvelope\):\n pass\n",
15991620
"\n",
16001621
source,
16011622
)
1602-
if "ProtocolEnvelope" not in source.replace(protocol_import, ""):
1603-
source = source.replace(protocol_import, "")
1604-
return source
1623+
return _sync_protocol_envelope_import(source)
16051624

16061625
def _normalize_existing_arms(target: Path, base: str) -> None:
16071626
"""Keep compatibility arms payload-shaped and expose final names as aliases."""
@@ -1621,6 +1640,7 @@ def _normalize_existing_arms(target: Path, base: str) -> None:
16211640
f"\n{base}: TypeAlias = ",
16221641
new_source,
16231642
)
1643+
new_source = _sync_protocol_envelope_import(new_source)
16241644
if new_source != original:
16251645
target.write_text(new_source)
16261646

src/adcp/types/_ergonomic.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@
5151
from adcp.types.generated_poc.core.error import Error
5252
from adcp.types.generated_poc.core.ext import ExtensionObject
5353
from adcp.types.generated_poc.core.format import Format
54+
from adcp.types.generated_poc.core.package import Package
5455
from adcp.types.generated_poc.core.product import Product
5556
from adcp.types.generated_poc.enums.advertiser_industry import AdvertiserIndustry
5657
from adcp.types.generated_poc.enums.asset_content_type import AssetContentType
5758
from adcp.types.generated_poc.enums.creative_sort_field import CreativeSortField
5859
from adcp.types.generated_poc.enums.delivery_type import DeliveryType
5960
from adcp.types.generated_poc.enums.disclosure_persistence import DisclosurePersistence
6061
from adcp.types.generated_poc.enums.disclosure_position import DisclosurePosition
62+
from adcp.types.generated_poc.enums.media_buy_status import MediaBuyStatus
6163
from adcp.types.generated_poc.enums.pacing import Pacing
6264
from adcp.types.generated_poc.enums.sort_direction import SortDirection
6365
from adcp.types.generated_poc.enums.task_status import TaskStatus
@@ -79,6 +81,9 @@
7981
)
8082
from adcp.types.generated_poc.media_buy.package_request import PackageRequest
8183
from adcp.types.generated_poc.media_buy.package_update import PackageUpdate
84+
from adcp.types.generated_poc.media_buy.create_media_buy_response import (
85+
CreateMediaBuyResponse1,
86+
)
8287
from adcp.types.generated_poc.media_buy.get_media_buy_delivery_response import (
8388
GetMediaBuyDeliveryResponse,
8489
MediaBuyDelivery,
@@ -476,6 +481,24 @@ def _apply_coercion() -> None:
476481
)
477482
ListCreativeFormatsResponse.model_rebuild(force=True)
478483

484+
# Apply coercion to CreateMediaBuyResponse1
485+
# - packages: list[Package] (accepts subclass instances)
486+
# - media_buy_status: MediaBuyStatus | str | None
487+
_patch_field_annotation(
488+
CreateMediaBuyResponse1,
489+
"packages",
490+
Annotated[
491+
list[Package],
492+
BeforeValidator(coerce_subclass_list(Package)),
493+
],
494+
)
495+
_patch_field_annotation(
496+
CreateMediaBuyResponse1,
497+
"media_buy_status",
498+
Annotated[MediaBuyStatus | None, BeforeValidator(coerce_to_enum(MediaBuyStatus))],
499+
)
500+
CreateMediaBuyResponse1.model_rebuild(force=True)
501+
479502
# Apply coercion to GetMediaBuyDeliveryResponse
480503
# - context: ContextObject | dict | None
481504
# - status: TaskStatus | str | None

src/adcp/types/_generated.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
DO NOT EDIT MANUALLY.
1111
1212
Generated from: https://github.com/adcontextprotocol/adcp/tree/main/schemas
13-
Generation date: 2026-05-22 14:10:57 UTC
13+
Generation date: 2026-05-24 21:33:52 UTC
1414
"""
1515

1616
# ruff: noqa: E501, I001
@@ -260,7 +260,13 @@
260260
Tags,
261261
)
262262
from adcp.types.generated_poc.brand.acquire_rights_request import AcquireRightsRequest, Campaign
263-
from adcp.types.generated_poc.brand.acquire_rights_response import AcquireRightsResponse
263+
from adcp.types.generated_poc.brand.acquire_rights_response import (
264+
AcquireRightsResponse,
265+
AcquireRightsResponse1,
266+
AcquireRightsResponse2,
267+
AcquireRightsResponse3,
268+
AcquireRightsResponse4,
269+
)
264270
from adcp.types.generated_poc.brand.creative_approval_request import CreativeApprovalRequest
265271
from adcp.types.generated_poc.brand.creative_approval_response import CreativeApprovalResponse
266272
from adcp.types.generated_poc.brand.get_brand_identity_request import (
@@ -273,6 +279,8 @@
273279
FontRole,
274280
FontRole2,
275281
GetBrandIdentityResponse,
282+
GetBrandIdentityResponse1,
283+
GetBrandIdentityResponse2,
276284
OpentypeFeature,
277285
Style,
278286
WeightRangeItem,
@@ -305,8 +313,15 @@
305313
UpdateRightsResponse2,
306314
)
307315
from adcp.types.generated_poc.brand.verification_status import VerificationStatus
308-
from adcp.types.generated_poc.brand.verify_brand_claim_request import VerifyBrandClaimRequest
309-
from adcp.types.generated_poc.brand.verify_brand_claim_response import VerifyBrandClaimResponse
316+
from adcp.types.generated_poc.brand.verify_brand_claim_request import (
317+
ClaimType,
318+
VerifyBrandClaimRequest,
319+
)
320+
from adcp.types.generated_poc.brand.verify_brand_claim_response import (
321+
VerifyBrandClaimErrorResponse,
322+
VerifyBrandClaimResponse,
323+
VerifyBrandClaimSuccessResponse,
324+
)
310325
from adcp.types.generated_poc.brand.verify_brand_claims_request import (
311326
Claim,
312327
Claim1,
@@ -322,7 +337,6 @@
322337
VerifyBrandClaimsRequestBulk,
323338
)
324339
from adcp.types.generated_poc.brand.verify_brand_claims_response import (
325-
ClaimType,
326340
ResultEntry,
327341
ResultEntry1,
328342
ResultEntry2,
@@ -435,13 +449,18 @@
435449
)
436450
from adcp.types.generated_poc.content_standards.get_content_standards_response import (
437451
GetContentStandardsResponse,
452+
GetContentStandardsResponse1,
453+
GetContentStandardsResponse2,
438454
)
439455
from adcp.types.generated_poc.content_standards.get_media_buy_artifacts_request import (
440456
GetMediaBuyArtifactsRequest,
441457
TimeRange,
442458
)
443459
from adcp.types.generated_poc.content_standards.get_media_buy_artifacts_response import (
460+
ArtifactRecord,
444461
GetMediaBuyArtifactsResponse,
462+
GetMediaBuyArtifactsResponse1,
463+
GetMediaBuyArtifactsResponse2,
445464
)
446465
from adcp.types.generated_poc.content_standards.list_content_standards_request import (
447466
ListContentStandardsRequest,
@@ -753,7 +772,6 @@
753772
MaterialSubmission,
754773
MetricOptimization,
755774
MetricOptimization1,
756-
Product as ProductUnion,
757775
Product1,
758776
Product2,
759777
ProductCardDetailed,
@@ -1043,6 +1061,8 @@
10431061
)
10441062
from adcp.types.generated_poc.creative.get_creative_features_response import (
10451063
GetCreativeFeaturesResponse,
1064+
GetCreativeFeaturesResponse1,
1065+
GetCreativeFeaturesResponse2,
10461066
)
10471067
from adcp.types.generated_poc.creative.list_creative_formats_request import (
10481068
AssetType,
@@ -1719,24 +1739,12 @@
17191739
# Backward compatibility aliases for renamed types
17201740
MediaBuyPackage = _PackageFromGetMediaBuysResponse
17211741
DeliveryStatus = _DeliveryStatusFromGetMediaBuysResponse
1722-
AcquireRightsResponse1 = AcquireRightsResponse
1723-
AcquireRightsResponse2 = AcquireRightsResponse
1724-
AcquireRightsResponse3 = AcquireRightsResponse
1725-
AcquireRightsResponse4 = AcquireRightsResponse
17261742
ComplyTestControllerResponse1 = ComplyTestControllerResponse
17271743
ComplyTestControllerResponse2 = ComplyTestControllerResponse
17281744
ComplyTestControllerResponse3 = ComplyTestControllerResponse
17291745
ComplyTestControllerResponse4 = ComplyTestControllerResponse
17301746
CreateContentStandardsResponse1 = CreateContentStandardsResponse
17311747
CreateContentStandardsResponse2 = CreateContentStandardsResponse
1732-
GetBrandIdentityResponse1 = GetBrandIdentityResponse
1733-
GetBrandIdentityResponse2 = GetBrandIdentityResponse
1734-
GetContentStandardsResponse1 = GetContentStandardsResponse
1735-
GetContentStandardsResponse2 = GetContentStandardsResponse
1736-
GetCreativeFeaturesResponse1 = GetCreativeFeaturesResponse
1737-
GetCreativeFeaturesResponse2 = GetCreativeFeaturesResponse
1738-
GetMediaBuyArtifactsResponse1 = GetMediaBuyArtifactsResponse
1739-
GetMediaBuyArtifactsResponse2 = GetMediaBuyArtifactsResponse
17401748
ListContentStandardsResponse1 = ListContentStandardsResponse
17411749
ListContentStandardsResponse2 = ListContentStandardsResponse
17421750
UpdateContentStandardsResponse1 = UpdateContentStandardsResponse
@@ -1831,6 +1839,7 @@
18311839
"Area",
18321840
"Arm",
18331841
"Artifact",
1842+
"ArtifactRecord",
18341843
"ArtifactRef",
18351844
"ArtifactWebhook",
18361845
"ArtifactWebhookPayload",
@@ -3056,8 +3065,10 @@
30563065
"VerificationStatus",
30573066
"VerifyAgent",
30583067
"VerifyAgent759",
3068+
"VerifyBrandClaimErrorResponse",
30593069
"VerifyBrandClaimRequest",
30603070
"VerifyBrandClaimResponse",
3071+
"VerifyBrandClaimSuccessResponse",
30613072
"VerifyBrandClaimsRequest",
30623073
"VerifyBrandClaimsRequestBulk",
30633074
"VerifyBrandClaimsResponseBulk",

0 commit comments

Comments
 (0)