You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add semantic type aliases for discriminated unions (#49)
* feat: Add semantic type aliases for discriminated unions
Add clear, descriptive type aliases for discriminated union types that
were previously only available with auto-generated numbered names
(e.g., PreviewRender1, SubAsset1).
New semantic aliases:
- Preview Renders (output_format discriminator):
* UrlPreviewRender = PreviewRender1 (output_format='url')
* HtmlPreviewRender = PreviewRender2 (output_format='html')
* BothPreviewRender = PreviewRender3 (output_format='both')
- VAST Assets (delivery_type discriminator):
* UrlVastAsset = VastAsset1 (delivery_type='url')
* InlineVastAsset = VastAsset2 (delivery_type='inline')
- DAAST Assets (delivery_type discriminator):
* UrlDaastAsset = DaastAsset1 (delivery_type='url')
* InlineDaastAsset = DaastAsset2 (delivery_type='inline')
- SubAssets (asset_kind discriminator):
* MediaSubAsset = SubAsset1 (asset_kind='media')
* TextSubAsset = SubAsset2 (asset_kind='text')
All aliases are:
- Exported from adcp.types.aliases
- Exported from adcp.types.generated
- Re-exported from main adcp package for convenience
- Fully tested with comprehensive test coverage
This improves code readability by conveying semantic meaning rather
than relying on generated numbering.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* docs: Document semantic type alias patterns and generated file policy
Add comprehensive documentation for:
- How to add semantic type aliases for discriminated unions
- Process for creating, exporting, and testing aliases
- Current list of all semantic aliases in the codebase
- Guidelines for when to create aliases vs when not to
- Clarify that generated.py must never be manually modified
This ensures future developers understand the alias system and don't
accidentally modify auto-generated code.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix: Remove semantic aliases from generated.py (will be overwritten)
The semantic type aliases were incorrectly added to generated.py,
which is auto-generated and will be overwritten on next type generation.
Moved all semantic alias imports to use aliases.py directly:
- src/adcp/types/__init__.py now imports from aliases instead of generated
- Removed all alias definitions and exports from generated.py
- All functionality preserved through aliases.py
This ensures aliases survive type regeneration.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* refactor: Remove misleading PreviewRender aliases
Removed the old, misleading aliases for PreviewRender types:
- PreviewRenderImage (misleading - actually output_format='url')
- PreviewRenderHtml (ambiguous - doesn't clarify it's output_format)
- PreviewRenderIframe (misleading - actually output_format='both')
Kept only the accurate, discriminator-based aliases:
- UrlPreviewRender (output_format='url')
- HtmlPreviewRender (output_format='html')
- BothPreviewRender (output_format='both')
These names accurately reflect the discriminator field values and
prevent confusion about what the types represent.
Since this is brand new code with no existing users, removing the
misleading names now prevents technical debt.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* test: Add comprehensive discriminator tests for semantic type aliases
Verifies that semantic aliases match their discriminator field values:
- PreviewRender: output_format ('url', 'html', 'both')
- VastAsset: delivery_type ('url', 'inline')
- DaastAsset: delivery_type ('url', 'inline')
- SubAsset: asset_kind ('media', 'text')
Tests validate:
- Correct discriminator values are present
- Correct fields exist for each variant
- Wrong discriminator values are rejected
- Serialization/deserialization roundtrips work correctly
21 new tests, all passing.
---------
Co-authored-by: Claude <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: CLAUDE.md
+97-1Lines changed: 97 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,9 @@ PackageRequest = dict[str, Any]
19
19
20
20
**NEVER Modify Generated Files Directly**
21
21
22
-
Files in `src/adcp/types/generated_poc/` are auto-generated by `scripts/generate_types.py`. Any manual edits will be lost on regeneration.
22
+
Files in `src/adcp/types/generated_poc/` and `src/adcp/types/generated.py` are auto-generated by `scripts/generate_types.py`. Any manual edits will be lost on regeneration.
23
+
24
+
**CRITICAL**: Do not add code to `generated.py` or any files in `generated_poc/` directory. These are regenerated from schemas.
23
25
24
26
**Post-Generation Fix System:**
25
27
@@ -64,6 +66,100 @@ Edit `scripts/post_generate_fixes.py` and add a new function. The script:
64
66
- Validates fixes were successfully applied
65
67
- Fails loudly if schema changes break the fix patterns
66
68
69
+
## Semantic Type Aliases for Discriminated Unions
70
+
71
+
**Problem**: The code generator (`datamodel-code-generator`) creates numbered type names for discriminated union variants (e.g., `PreviewRender1`, `PreviewRender2`, `SubAsset1`, `SubAsset2`). While functionally correct, these don't convey semantic meaning.
72
+
73
+
**Solution**: Add semantic type aliases in `src/adcp/types/aliases.py` that provide clear, descriptive names based on the discriminator field value.
74
+
75
+
**Process for Adding New Semantic Aliases:**
76
+
77
+
1.**Identify the discriminated union types** - Look for numbered types (e.g., `TypeName1`, `TypeName2`) in generated files
78
+
2.**Determine the discriminator field** - Check the schema/generated code for the `Literal` field that distinguishes variants
79
+
3.**Create semantic aliases in `aliases.py`**:
80
+
```python
81
+
# Import the generated types
82
+
from adcp.types.generated import PreviewRender1, PreviewRender2, PreviewRender3
83
+
84
+
# Create semantic aliases based on discriminator values
0 commit comments