-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(gapic-generator): add pure-python package initialization and versioning codegen #17639
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
Draft
ohmayr
wants to merge
1
commit into
feature/codegen-writer
Choose a base branch
from
feature/codegen-package-init
base: feature/codegen-writer
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.
+134
−5
Draft
Changes from all commits
Commits
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
34 changes: 34 additions & 0 deletions
34
packages/gapic-generator/gapic/codegen/components/package_init.py
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,34 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from gapic.codegen.writer import CodeWriter | ||
| from gapic.schema.api import API | ||
|
|
||
|
|
||
| class PackageInitGenerator: | ||
| """Pure Python generator for package initialization files (__init__.py, gapic_version.py, py.typed).""" | ||
|
|
||
| @staticmethod | ||
| def generate_gapic_version(api_schema: API) -> str: | ||
| """Generates gapic_version.py content.""" | ||
| writer = CodeWriter() | ||
| writer.write_license_header() | ||
| writer.write_line(f'__version__ = "{api_schema.gapic_version}" # {{x-release-please-version}}') | ||
| return writer.dump() | ||
|
|
||
| @staticmethod | ||
| def generate_py_typed(api_schema: API) -> str: | ||
| """Generates py.typed file (PEP 561 marker).""" | ||
| pkg_name = api_schema.naming.warehouse_package_name | ||
| return f"# Marker file for PEP 561.\n# The {pkg_name} package uses inline types.\n" | ||
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,39 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import Any, Optional | ||
| from gapic.codegen.components.package_init import PackageInitGenerator | ||
|
|
||
|
|
||
| class PurePythonEngine: | ||
| """Pure-Python template-free code generation engine.""" | ||
|
|
||
| @staticmethod | ||
| def render(template_name: str, context: dict[str, Any]) -> Optional[str]: | ||
| """Dispatches template rendering to pure-Python component generators. | ||
|
|
||
| Returns None to fall back to Jinja rendering if the template is not yet migrated. | ||
| """ | ||
| api_schema = context.get("api") | ||
| if not api_schema: | ||
| return None | ||
|
|
||
| # Package Init & Versioning Component | ||
| if template_name.endswith("gapic_version.py.j2"): | ||
| return PackageInitGenerator.generate_gapic_version(api_schema) | ||
| if template_name.endswith("py.typed.j2"): | ||
| return PackageInitGenerator.generate_py_typed(api_schema) | ||
|
|
||
| # Fallback to Jinja for un-migrated templates | ||
| return None |
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
51 changes: 51 additions & 0 deletions
51
packages/gapic-generator/tests/unit/codegen/test_package_init.py
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,51 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from unittest.mock import MagicMock | ||
| from gapic.codegen.components.package_init import PackageInitGenerator | ||
|
|
||
|
|
||
| def test_generate_gapic_version(): | ||
| mock_api = MagicMock() | ||
| mock_api.gapic_version = "0.1.0" | ||
| content = PackageInitGenerator.generate_gapic_version(mock_api) | ||
|
|
||
| assert "# Copyright 2026 Google LLC" in content | ||
| assert '__version__ = "0.1.0" # {x-release-please-version}' in content | ||
|
|
||
|
|
||
| def test_generate_py_typed(): | ||
| mock_api = MagicMock() | ||
| mock_api.naming.warehouse_package_name = "google-iam-credentials" | ||
| content = PackageInitGenerator.generate_py_typed(mock_api) | ||
| assert "# Marker file for PEP 561." in content | ||
| assert "google-iam-credentials" in content | ||
|
|
||
|
|
||
| def test_pure_python_engine_render(): | ||
| from gapic.codegen.engine import PurePythonEngine | ||
|
|
||
| assert PurePythonEngine.render("gapic_version.py.j2", {}) is None | ||
|
|
||
| mock_api = MagicMock() | ||
| mock_api.gapic_version = "0.1.0" | ||
| mock_api.naming.warehouse_package_name = "google-iam-credentials" | ||
|
|
||
| v_out = PurePythonEngine.render("gapic_version.py.j2", {"api": mock_api}) | ||
| assert '__version__ = "0.1.0"' in v_out | ||
|
|
||
| t_out = PurePythonEngine.render("py.typed.j2", {"api": mock_api}) | ||
| assert "google-iam-credentials" in t_out | ||
|
|
||
| assert PurePythonEngine.render("other_file.py.j2", {"api": mock_api}) is None |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.