Repository navigation
feat(models): remove postprocess_models.py and emit pure OOTB Pydantic DTOs #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
69f84d0
d7e757f
61b6301
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,8 +137,7 @@ echo "Compiling type bundle with ucp-schema generate-types..." | |
| "$UCP_SCHEMA_BIN" generate-types --schema-dir "$SCOPED_SCHEMA_DIR" --output "$TMP_TYPES_JSON" | ||
|
|
||
| echo "Generating Pydantic models from compiled type bundle..." | ||
| rm -rf "$OUTPUT_DIR" | ||
| mkdir -p "$OUTPUT_DIR" | ||
| mkdir -p "$OUTPUT_DIR" && rm -f "$OUTPUT_DIR/_generated.py" | ||
|
|
||
| uv run \ | ||
| --link-mode=copy \ | ||
|
|
@@ -161,8 +160,64 @@ uv run \ | |
| --use-title-as-name \ | ||
| --additional-imports pydantic.ConfigDict | ||
|
|
||
| echo "Post-processing generated models (constraints the generator ignores)..." | ||
| uv run python postprocess_models.py "$TMP_TYPES_JSON" src/ucp_sdk/models "$SCOPED_SCHEMA_DIR" | ||
| echo "Generating lazy package index in $OUTPUT_DIR/__init__.py..." | ||
| python3 - <<'PY' | ||
| import ast | ||
| from pathlib import Path | ||
|
|
||
| _SYNTHETIC_WRAPPERS = {"UCPSchemaTypes", "UcpSchemaTypes"} | ||
| schemas_dir = Path("src/ucp_sdk/models/schemas") | ||
| tree = ast.parse((schemas_dir / "_generated.py").read_text(encoding="utf-8")) | ||
| type_alias_cls = getattr(ast, "TypeAlias", ()) | ||
| names: set[str] = set() | ||
| for node in tree.body: | ||
| if isinstance(node, ast.ClassDef): | ||
| names.add(node.name) | ||
| elif isinstance(node, ast.Assign): | ||
| names.update(t.id for t in node.targets if isinstance(t, ast.Name)) | ||
| elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name): | ||
| names.add(node.target.id) | ||
| elif isinstance(node, type_alias_cls) and isinstance(getattr(node, "name", None), ast.Name): | ||
| names.add(node.name.id) | ||
| exports = sorted(n for n in names if not n.startswith("_") and n not in _SYNTHETIC_WRAPPERS) | ||
| imports = ", ".join(exports) | ||
| all_items = ", ".join(f'"{n}"' for n in exports) | ||
| (schemas_dir / "__init__.py").write_text( | ||
| '# Copyright 2026 UCP Authors\n' | ||
| '#\n' | ||
| '# Licensed under the Apache License, Version 2.0 (the "License");\n' | ||
| '# you may not use this file except in compliance with the License.\n' | ||
| '# You may obtain a copy of the License at\n' | ||
| '#\n' | ||
| '# http://www.apache.org/licenses/LICENSE-2.0\n' | ||
| '#\n' | ||
| '# Unless required by applicable law or agreed to in writing, software\n' | ||
| '# distributed under the License is distributed on an "AS IS" BASIS,\n' | ||
| '# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' | ||
| '# See the License for the specific language governing permissions and\n' | ||
| '# limitations under the License.\n\n' | ||
| '# generated by generate_models.sh\n\n' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Including the Apache 2.0 license header at the top of generated
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in |
||
| '"""Generated UCP schema models."""\n\n' | ||
| 'from __future__ import annotations\n\n' | ||
| 'import importlib\n' | ||
| 'from typing import TYPE_CHECKING, Any\n\n' | ||
| f'if TYPE_CHECKING:\n from ._generated import ({imports},)\n\n' | ||
| f'__all__ = [{all_items},]\n\n\n' | ||
| 'def __getattr__(name: str) -> Any:\n' | ||
| ' """Import public schema models and type aliases on first access."""\n' | ||
| ' if name not in __all__:\n' | ||
| ' msg = f"module {__name__!r} has no attribute {name!r}"\n' | ||
| ' raise AttributeError(msg)\n' | ||
| ' module = importlib.import_module("._generated", __name__)\n' | ||
| ' value = getattr(module, name)\n' | ||
| ' globals()[name] = value\n' | ||
| ' return value\n\n\n' | ||
| 'def __dir__() -> list[str]:\n' | ||
| ' """Return module attributes including lazily re-exported schema names."""\n' | ||
| ' return sorted({*globals(), *__all__})\n', | ||
| encoding="utf-8", | ||
| ) | ||
| PY | ||
|
|
||
| echo "Formatting generated models..." | ||
| uv run ruff check --fix src/ucp_sdk/models | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two notes here:
model-driftcurrently fails with HTTP 404 becauseUniversal-Commerce-Protocol/ucp-schemav2.0.0hasn't been published yet (waiting onUniversal-Commerce-Protocol/ucp-schema#96andUniversal-Commerce-Protocol/ucp-schema#97to merge andv2.0.0to be tagged)..github/workflows/*.ymldeclarative with single-linerun:steps, consider moving theucp-schemarelease binary download +SHA256SUMSverification into a dedicated script (orgenerate_models.sh), and removing the duplicateNormalize file endingsinline Python step below (lines 78–88), sincegenerate_models.shalready normalizes file endings onsrc/ucp_sdk/models.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in
61b6301: removed the duplicateNormalize file endingsstep fromtests.yml, anducp-schemav2.0.0is now published somodel-driftruns live. Kept the SHA256-verified binary install step in CI so localgenerate_models.shruns never mutate a developer's$HOME/.local/bin.