Skip to content

chore(codegen): post_generate_fixes.py drops ProtocolEnvelope import + aliases.py has stale defensive lookups #822

Closed

Description

@bokelley

Follow-up split off from #807. Surfaced when regenerating types against the refreshed 3.1.0-beta.3 schema cache (#821).

Two coupled bugs in the codegen pipeline

1. scripts/post_generate_fixes.py strips ProtocolEnvelope import without re-adding

_remove_original_response_class (lines 1581-1591) removes the import when the generator's placeholder class Foo(AdcpVersionEnvelope, ProtocolEnvelope): pass is stripped. Then _write_if_needed appends manual class definitions (some of which reference ProtocolEnvelope — e.g., CreateMediaBuyResponse3 at line 1918) without re-adding the import.

Works today only because the previously-committed _generated.py was generated from an older schema snapshot where datamodel-codegen happened to emit the import for other reasons. New schema shape doesn't.

Symptom on regen: NameError: name 'ProtocolEnvelope' is not defined in:

  • media_buy/create_media_buy_response.py
  • Several brand/*_response.py files
  • Several content_standards/*_response.py files
  • A handful of media_buy/*_response.py files

Fix sketch (verified in a throwaway branch):

# In _write_if_needed, after appending snippet:
combined = source.rstrip() + snippet.rstrip() + "\n"
protocol_import = "from ..core.protocol_envelope import ProtocolEnvelope\n"
if "ProtocolEnvelope" in combined and protocol_import not in combined:
    combined = combined.replace(
        "from ..core.version_envelope import AdcpVersionEnvelope\n",
        protocol_import + "from ..core.version_envelope import AdcpVersionEnvelope\n",
        1,
    )
target.write_text(combined)

2. src/adcp/types/aliases.py carries stale defensive lookups

Multiple aliases use the pattern getattr(_g, "FooResponse1", _g.FooResponse) where neither name exists in the regenerated _generated.py. Example at aliases.py:101:

AcquireRightsResponse1 = getattr(_g, "AcquireRightsResponse1", _g.AcquireRightsResponse)
# AttributeError: module 'adcp.types._generated' has no attribute 'AcquireRightsResponse'

Schema-driven names have moved upstream; the fallback name never materialized. Each affected alias needs to be checked against the regenerated _generated.py and either re-pointed or deleted.

Why bundled together

You can't regen types until (1) is fixed (codegen produces non-importing files). Once (1) is fixed and the regen runs, (2) surfaces as the next import error. Both need to land before the regenerated types are committed.

Acceptance

  • scripts/post_generate_fixes.py re-adds the ProtocolEnvelope import when appending classes that reference it
  • aliases.py defensive lookups updated to current generated names (or removed where the fallback is the wrong shape)
  • python scripts/generate_types.py succeeds from a clean state
  • python -c "from adcp.types import _generated; from adcp.types import aliases" imports cleanly
  • pytest tests/test_code_generation.py tests/test_asset_aliases_stable.py tests/test_schemas_version_pin.py -v passes
  • The original handrolled-alias audit from chore(types): schema sync against 3.1.0-beta.3 + drop obsolete handrolled aliases #807 can finally proceed against a working regen

Refs

Activity

  1. added
    no-triageSkip the Claude triage bot — human or designated agent will handle this issue
    on May 22, 2026
  2. bokelley commented on May 23, 2026

    @bokelley
    ContributorAuthor

    Active fix candidate: #824. I updated the PR branch against current main so CI can rerun on the current base. The PR body currently says Refs #822 rather than Closes #822, so if it merges as-is we should close this issue manually or update the PR body before merge.

  3. bokelley commented on Jun 6, 2026

    @bokelley
    ContributorAuthor

    Both bugs are resolved on main. (1) _sync_protocol_envelope_import in scripts/post_generate_fixes.py re-adds the ProtocolEnvelope import when a restored arm references it (handles the existing-import, future-import-only, and no-import cases); create_media_buy_response.py carries the import. (2) The stale two-arg getattr(_g, 'X', _g.Y) fallbacks are gone — aliases.py now uses a single _generated_alias(name, fallback) try/except helper. Regen-from-clean against rc.9 succeeds. Closing as fixed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    claude-triagedno-triageSkip the Claude triage bot — human or designated agent will handle this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions