-
Notifications
You must be signed in to change notification settings - Fork 16
Add scripts for OpenAPI generation and post-generation and more convenience #100
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
Open
antalszava
wants to merge
7
commits into
main
Choose a base branch
from
makefile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c9886ef
Add cross-platform regeneration script and optional Makefile
antalszava b185379
Replace perl post-generation hooks with cross-platform Python script
antalszava 28de2e8
chore: contributing
antalszava c20e942
chore: formatting
antalszava 42f135a
Merge branch 'main' into makefile
antalszava b84ff80
Merge branch 'main' into makefile
antalszava b5921ea
chore: addressing PR comments
antalszava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Optional convenience wrappers; scripts/regenerate_models.py is the canonical | ||
| # regeneration workflow and works on any OS. | ||
|
|
||
| PYTEST_ARGS ?= | ||
|
|
||
| .PHONY: lint format typecheck test integration regen sync-spec check-generated | ||
|
|
||
| lint: | ||
| uv run ruff check | ||
| uv run ruff format --check | ||
|
|
||
| format: | ||
| uv run ruff format | ||
|
|
||
| typecheck: | ||
| uv run ty check ionq_core/ | ||
|
|
||
| test: | ||
| uv run pytest $(PYTEST_ARGS) | ||
|
|
||
| integration: | ||
| uv run pytest -m integration --no-cov $(PYTEST_ARGS) | ||
|
|
||
| regen: | ||
| uv run --group regen python scripts/regenerate_models.py | ||
|
|
||
| sync-spec: | ||
| uv run --group regen python scripts/regenerate_models.py --sync-spec | ||
|
|
||
| check-generated: regen | ||
| @if [ -n "$$(git status --porcelain ionq_core/)" ]; then \ | ||
| echo "Generated code is out of date: run 'make regen' and commit the results."; \ | ||
| git diff ionq_core/; \ | ||
| exit 1; \ | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,6 @@ package_name_override: ionq_core | |
| literal_enums: true | ||
|
|
||
| post_hooks: | ||
| - "perl -pi -e 's/token: str\\K$/ = field(repr=False)/' client.py" | ||
| - "perl -0777 -pi -e '$y=(gmtime)[5]+1900;s/\\A(?!# SPDX-FileCopyrightText)/# SPDX-FileCopyrightText: $y IonQ, Inc.\\n# SPDX-License-Identifier: Apache-2.0\\n# \\@generated\\n\\n/' $(find . -name '*.py')" | ||
| - "python ../scripts/post_generate.py" | ||
|
Collaborator
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. |
||
| - "ruff check . --fix-only" | ||
| - "ruff format ." | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """Post-generation hooks for openapi-python-client (cross-platform). | ||
|
|
||
| Invoked via post_hooks in openapi-python-client-config.yaml. Hides | ||
| AuthenticatedClient.token from repr and prepends SPDX/@generated headers. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from pathlib import Path | ||
|
|
||
| PACKAGE_DIR = Path(__file__).resolve().parent.parent / "ionq_core" | ||
|
|
||
| # Year of the package's first publication (v0.1.0, 2026-04-29). Fixed so that | ||
| # regeneration output is identical regardless of when it runs. | ||
| COPYRIGHT_YEAR = 2026 | ||
|
|
||
|
|
||
| def main() -> None: | ||
| client_file = PACKAGE_DIR / "client.py" | ||
| client_file.write_text( | ||
|
Collaborator
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. write_text emits CRLF on Windows, so regen output there isn't byte-identical (and the staleness gate is Linux-only). Add |
||
| re.sub( | ||
| r"(token: str)$", | ||
| r"\1 = field(repr=False)", | ||
| client_file.read_text(encoding="utf-8"), | ||
| flags=re.MULTILINE, | ||
| ), | ||
| encoding="utf-8", | ||
| newline="\n", | ||
| ) | ||
|
|
||
| header = ( | ||
| f"# SPDX-FileCopyrightText: {COPYRIGHT_YEAR} IonQ, Inc.\n" | ||
| "# SPDX-License-Identifier: Apache-2.0\n# @generated\n\n" | ||
| ) | ||
| for path in PACKAGE_DIR.rglob("*.py"): | ||
| text = path.read_text(encoding="utf-8") | ||
| if not text.startswith("# SPDX-FileCopyrightText"): | ||
| path.write_text(header + text, encoding="utf-8", newline="\n") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """Regenerate the generated client packages from the vendored OpenAPI spec. | ||
|
|
||
| Canonical invocation: | ||
|
|
||
| uv run --group regen python scripts/regenerate_models.py [--sync-spec] | ||
|
|
||
| Invoking this script as such ensures that the commands in here don't need to | ||
| be re-run via uv run. | ||
|
|
||
| Applies openapi-overlay.yaml (when present) to openapi.json, then runs | ||
| openapi-python-client with the repo's config and custom templates. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import urllib.request | ||
| from pathlib import Path | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parent.parent | ||
| SPEC_URL = "https://api.ionq.co/v0.4/api-docs" | ||
| SPEC_FILE = REPO_ROOT / "openapi.json" | ||
| OVERLAY_FILE = REPO_ROOT / "openapi-overlay.yaml" | ||
|
|
||
|
|
||
| def _get_tool_path(name: str) -> str: | ||
| path = shutil.which(name) | ||
| if path is None: | ||
| invocation_cmd = "uv run --group regen python scripts/regenerate_models.py" | ||
| sys.exit(f"error: {name!r} not found on PATH; run via '{invocation_cmd}'") | ||
| return path | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "--sync-spec", | ||
| action="store_true", | ||
| help=f"download the latest spec from {SPEC_URL} before regenerating", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| if args.sync_spec: | ||
| with urllib.request.urlopen(SPEC_URL, timeout=60) as response: | ||
| SPEC_FILE.write_bytes(response.read()) | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmp_dir: | ||
| patched_spec = Path(tmp_dir) / "patched-spec.json" | ||
| if OVERLAY_FILE.exists(): | ||
| cmd = [_get_tool_path("oas-patch"), "overlay", str(SPEC_FILE), str(OVERLAY_FILE), "-o", str(patched_spec)] | ||
| subprocess.run(cmd, check=True, cwd=REPO_ROOT) | ||
| else: | ||
| shutil.copyfile(SPEC_FILE, patched_spec) | ||
|
|
||
| # fmt: off | ||
| cmd = [ | ||
| _get_tool_path("openapi-python-client"), "generate", | ||
| "--path", str(patched_spec), | ||
| "--meta", "none", | ||
| "--config", "openapi-python-client-config.yaml", | ||
| "--custom-template-path", "custom-templates", | ||
| "--output-path", "ionq_core", | ||
| "--overwrite", | ||
| ] | ||
| # fmt: on | ||
| subprocess.run(cmd, check=True, cwd=REPO_ROOT) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ideally we'd want to run coverage on all targets, and then combine the output so that we can see coverage of target-specific branches. No for this PR, of course, just a big picture nice to have.