Skip to content

Commit a528682

Browse files
bokelleyclaude
andcommitted
fix: Regenerate consolidated exports to handle import conflicts
The previous generated.py was importing the same type names from multiple modules, causing "last import wins" conflicts that broke the backward compatibility aliases. ## Changes - Created scripts/consolidate_exports.py to properly generate consolidated exports - Regenerated src/adcp/types/generated.py with conflict-free imports - All backward compatibility aliases now properly exported (ActivateSignalError, etc.) - Reduced from 1009 exports to 313 unique exports (duplicates removed) ## Root Cause When importing from multiple generated_poc modules, many types are re-exported by multiple modules (Error, Deployment, ActivationKey, etc.). Python's import system handles this by using the last import, which was overwriting the aliases added to individual files. ## Solution The consolidation script extracts all public types from each module and creates a single import per module. Since the duplicate types are identical across modules, Python's "last import wins" behavior is acceptable - we just need to ensure the aliases are included in at least one import. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 269640a commit a528682

File tree

2 files changed

+135
-13
lines changed

2 files changed

+135
-13
lines changed

scripts/consolidate_exports.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Create a consolidated export file that re-exports all types from generated_poc modules.
4+
5+
This script analyzes all modules in generated_poc/ and creates a single generated.py
6+
that imports and re-exports all public types, handling naming conflicts appropriately.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import ast
12+
import time
13+
from pathlib import Path
14+
15+
GENERATED_POC_DIR = Path(__file__).parent.parent / "src" / "adcp" / "types" / "generated_poc"
16+
OUTPUT_FILE = Path(__file__).parent.parent / "src" / "adcp" / "types" / "generated.py"
17+
18+
19+
def extract_exports_from_module(module_path: Path) -> set[str]:
20+
"""Extract all public class and type alias names from a Python module."""
21+
with open(module_path) as f:
22+
try:
23+
tree = ast.parse(f.read())
24+
except SyntaxError:
25+
return set()
26+
27+
exports = set()
28+
29+
for node in ast.walk(tree):
30+
# Class definitions
31+
if isinstance(node, ast.ClassDef):
32+
if not node.name.startswith("_"):
33+
exports.add(node.name)
34+
# Module-level assignments (type aliases)
35+
elif isinstance(node, ast.Assign):
36+
for target in node.targets:
37+
if isinstance(target, ast.Name) and not target.id.startswith("_"):
38+
# Only export if it looks like a type name (starts with capital)
39+
if target.id[0].isupper():
40+
exports.add(target.id)
41+
42+
return exports
43+
44+
45+
def generate_consolidated_exports() -> str:
46+
"""Generate the consolidated exports file content."""
47+
# Discover all modules
48+
modules = sorted(GENERATED_POC_DIR.glob("*.py"))
49+
modules = [m for m in modules if m.stem != "__init__" and not m.stem.startswith(".")]
50+
51+
print(f"Found {len(modules)} modules to consolidate")
52+
53+
# Build import statements and collect all exports
54+
import_lines = []
55+
all_exports = set()
56+
57+
for module_path in modules:
58+
module_name = module_path.stem
59+
exports = extract_exports_from_module(module_path)
60+
61+
if not exports:
62+
continue
63+
64+
print(f" {module_name}: {len(exports)} exports")
65+
66+
# Create import statement
67+
exports_str = ", ".join(sorted(exports))
68+
import_line = f"from adcp.types.generated_poc.{module_name} import {exports_str}"
69+
import_lines.append(import_line)
70+
71+
all_exports.update(exports)
72+
73+
# Generate file content
74+
lines = [
75+
'"""Generated AdCP types.',
76+
"",
77+
"Auto-generated by datamodel-code-generator from JSON schemas.",
78+
"DO NOT EDIT MANUALLY.",
79+
"",
80+
"Generated from: https://github.com/adcontextprotocol/adcp/tree/main/schemas",
81+
f"Generation date: {time.time()}",
82+
'"""',
83+
"",
84+
"from __future__ import annotations",
85+
"",
86+
"# Import all types from generated_poc modules",
87+
]
88+
89+
lines.extend(import_lines)
90+
91+
lines.extend([
92+
"",
93+
"# Explicit exports",
94+
f"__all__ = {sorted(list(all_exports))}",
95+
""
96+
])
97+
98+
return "\n".join(lines)
99+
100+
101+
def main():
102+
"""Generate consolidated exports file."""
103+
print("Generating consolidated exports from generated_poc modules...")
104+
105+
if not GENERATED_POC_DIR.exists():
106+
print(f"Error: {GENERATED_POC_DIR} does not exist")
107+
return 1
108+
109+
content = generate_consolidated_exports()
110+
111+
print(f"\nWriting {OUTPUT_FILE}...")
112+
OUTPUT_FILE.write_text(content)
113+
114+
print(f"✓ Successfully generated consolidated exports")
115+
export_count = len([name for name in content.split("__all__ = [")[1].split("]")[0].strip("[]").split(",") if name.strip()])
116+
print(f" Total exports: {export_count}")
117+
118+
return 0
119+
120+
121+
if __name__ == "__main__":
122+
exit(main())

0 commit comments

Comments
 (0)