René's URL Explorer Experiment


Title: refactor(gapic-generator): deduplicate sync/async prep_wrapped_messages and transport method wrapping · Issue #18536 · googleapis/google-cloud-python · GitHub

Open Graph Title: refactor(gapic-generator): deduplicate sync/async prep_wrapped_messages and transport method wrapping · Issue #18536 · googleapis/google-cloud-python

X Title: refactor(gapic-generator): deduplicate sync/async prep_wrapped_messages and transport method wrapping · Issue #18536 · googleapis/google-cloud-python

Description: Problem Summary In packages/gapic-generator/gapic/templates/, there is code duplication and structural asymmetry between synchronous and asynchronous RPC preparation and method wrapping across the generated transport classes: _prep_wrapp...

Open Graph Description: Problem Summary In packages/gapic-generator/gapic/templates/, there is code duplication and structural asymmetry between synchronous and asynchronous RPC preparation and method wrapping across the ...

X Description: Problem Summary In packages/gapic-generator/gapic/templates/, there is code duplication and structural asymmetry between synchronous and asynchronous RPC preparation and method wrapping across the ...

Opengraph URL: https://github.com/googleapis/google-cloud-python/issues/18536

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"refactor(gapic-generator): deduplicate sync/async prep_wrapped_messages and transport method wrapping","articleBody":"### Problem Summary\n\nIn `packages/gapic-generator/gapic/templates/`, there is code duplication and structural asymmetry between synchronous and asynchronous RPC preparation and method wrapping across the generated transport classes:\n\n1. **`_prep_wrapped_messages` Duplication**:\n   - `packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2` (lines 239–280) defines `_prep_wrapped_messages(self, client_info)` for synchronous transports.\n   - `packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2` (lines 322–371) defines `prep_wrapped_messages_async_method(api, service)` for asynchronous transports.\n   - These two blocks are ~50 lines of Jinja template code that are nearly identical. Both iterate over `service.methods.values()` and `api.mixin_api_methods.keys()`, mapping every RPC to its retry predicate, default timeout, client info, fully-qualified method name, and streaming flags. The only differences are:\n     - `self._wrap_method` vs `self._wrap_async_method`\n     - `retries.Retry(...)` vs `retries.AsyncRetry(...)`\n\n2. **`_wrap_method` vs `_wrap_async_method` Duplication**:\n   - Both methods are defined side-by-side in `base.py.j2` (lines 174–237).\n   - Both methods implement identical compatibility checks and argument filtering across three generations of `google-api-core` (`client_options`, `kind`, `method_name`, `is_streaming`), delegating respectively to `gapic_v1.method.wrap_method` and `gapic_v1.method_async.wrap_method`.\n   - Consider moving these to `_compat.py` to reduce the footprint in generated code.\n\n3. **Architectural Asymmetry (Base Class vs Shared Macros)**:\n   - `_wrap_async_method` is defined directly on the synchronous base class `{{ service.name }}Transport` in `base.py.j2`, even though synchronous transports never call it.\n   - Conversely, asynchronous `_prep_wrapped_messages` is *not* in `base.py.j2`—it lives as an external Jinja macro in `_shared_macros.j2` that is injected into `grpc_asyncio.py.j2` and `rest_asyncio.py.j2` to override the base class implementation.\n   - This mixing makes the transport hierarchy harder to reason about and causes maintenance drift when retry parameters, metadata, or tracing attributes are updated.\n\n---\n\n### Deduplication Options to Consider\n\n#### Option A: Polymorphic Dispatch in Base Transport (Recommended)\nConsolidate `_prep_wrapped_messages` into a single, unified implementation in `base.py.j2` by making transport classes declare their retry class and RPC wrapper callable:\n\n```python\n# In base.py.j2 ({{ service.name }}Transport):\n_is_async: bool = False\n_retry_type = retries.Retry\n\ndef _wrap_rpc(self, func, *args, **kwargs):\n    # Unified wrap helper parameterized by sync vs async\n    wrap_func = gapic_v1.method_async.wrap_method if self._is_async else gapic_v1.method.wrap_method\n    supports_tracing = _ASYNC_WRAP_METHOD_SUPPORTS_TRACING if self._is_async else _WRAP_METHOD_SUPPORTS_TRACING\n    if supports_tracing:\n        kwargs[\"client_options\"] = self._client_options\n        if self.kind:\n            kwargs[\"kind\"] = self.kind\n        return wrap_func(func, *args, **kwargs)\n    ...\n```\n\n- **Pros**:\n  - `_prep_wrapped_messages` only exists once in `base.py.j2`. All transports (gRPC, AsyncIO gRPC, REST, AsyncIO REST) inherit it naturally.\n  - Deletes `prep_wrapped_messages_async_method` from `_shared_macros.j2` entirely (~50 lines removed).\n  - Eliminates duplicate template loops for mixin and service methods.\n- **Cons**: Requires setting transport class-level attributes (`_is_async`, `_retry_type`) on async transports.\n\n#### Option B: Parameterized Jinja Macro\nUnify synchronous and asynchronous message preparation into a single reusable Jinja macro in `_shared_macros.j2`:\n\n```jinja2\n{% macro prep_wrapped_messages(api, service, is_async=False) %}\ndef _prep_wrapped_messages(self, client_info):\n    self._wrapped_methods = {\n        {% for method in service.methods.values() %}\n        self.{{ method.transport_safe_name|snake_case }}: self.{{ \"_wrap_async_method\" if is_async else \"_wrap_method\" }}(\n            ...\n            {% if method.retry %}\n            default_retry=retries.{{ \"AsyncRetry\" if is_async else \"Retry\" }}(...),\n            {% endif %}\n            ...\n        ),\n        {% endfor %}\n    }\n{% endmacro %}\n```\n\n- **Pros**:\n  - Keeps generation purely in templates without modifying Python transport class contracts or inheritance semantics.\n  - Single source of truth for method iteration and configuration.\n- **Cons**: Still renders two copies of the method across generated files; Jinja syntax becomes slightly more branchy.\n\n#### Option C: Dedicated `AsyncTransport` Base Class\nIntroduce an explicit `_Base{{ service.name }}AsyncTransport` class (similar to `_Base{{ service.name }}RestTransport`):\n- `{{ service.name }}Transport`: Sync base class containing sync `_prep_wrapped_messages` and `_wrap_method`.\n- `_Base{{ service.name }}AsyncTransport`: Async base class containing async `_prep_wrapped_messages` and `_wrap_async_method`.\n\n- **Pros**:\n  - Clean separation of concerns; async wrappers are no longer baked into sync base classes.\n  - Idiomatic object-oriented hierarchy.\n- **Cons**:\n  - Larger structural change across generated codebases and public type contracts.\n  - Requires updating multiple transport inheritance graphs.\n\n---\n\n### Related Discussion\n- PR #18433: `feat(observability): [WIP] implement universal 4-path OpenTelemetry tracing`\n- Follow-up from reviewer feedback regarding code duplication between sync and async transports.\n","author":{"url":"https://github.com/chalmerlowe","@type":"Person","name":"chalmerlowe"},"datePublished":"2026-10-01T11:59:34Z","url":"https://github.com/googleapis/google-cloud-python/issues/18536"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:708c4c79-b8c1-8469-2a44-f49b4cee08e1
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idED5E:84C13:6058A6:7E7085:6AFE6C3E
html-safe-nonce4661b9217c90d92ec8fd154f1281817c856d42ef90aaa08158d94a8130e1a54a
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDVFOjg0QzEzOjYwNThBNjo3RTcwODU6NkFGRTZDM0UiLCJ2aXNpdG9yX2lkIjoiMzg0NjQ5MTMwNTI4MjUzMDM2NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac62ae49cc45faca806c24bce58a75543c544913a0c8c6d94bcbad4c5af8e5c04f
hovercard-subject-tagissue:5661765921
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/google-cloud-python/18536/issue_layout
twitter:imagehttps://opengraph.githubassets.com/4cf784a608d47bb577abd93166deadf6e653eff0c59afd01037d76231cbd8d0c/googleapis/google-cloud-python/issues/18536
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/4cf784a608d47bb577abd93166deadf6e653eff0c59afd01037d76231cbd8d0c/googleapis/google-cloud-python/issues/18536
og:image:altProblem Summary In packages/gapic-generator/gapic/templates/, there is code duplication and structural asymmetry between synchronous and asynchronous RPC preparation and method wrapping across the ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamechalmerlowe
hostnamegithub.com
expected-hostnamegithub.com
Noneb3c0f155eafc27e60db6e254d1ffff0e072e9ab3b4018a5ee518797c19d89f86
turbo-cache-controlno-preview
go-importgithub.com/googleapis/google-cloud-python git https://github.com/googleapis/google-cloud-python.git
octolytics-dimension-user_id16785467
octolytics-dimension-user_logingoogleapis
octolytics-dimension-repository_id16316451
octolytics-dimension-repository_nwogoogleapis/google-cloud-python
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id16316451
octolytics-dimension-repository_network_root_nwogoogleapis/google-cloud-python
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release4a10310af51a1bfffbedc4fd81cb582acf0cb0ff
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/googleapis/google-cloud-python/issues/18536#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fgoogle-cloud-python%2Fissues%2F18536
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fgoogle-cloud-python%2Fissues%2F18536
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=googleapis%2Fgoogle-cloud-python
Reloadhttps://github.com/googleapis/google-cloud-python/issues/18536
Reloadhttps://github.com/googleapis/google-cloud-python/issues/18536
Reloadhttps://github.com/googleapis/google-cloud-python/issues/18536
googleapishttps://github.com/googleapis
google-cloud-pythonhttps://github.com/googleapis/google-cloud-python
Codehttps://github.com/googleapis/google-cloud-python
Issues450 (450)https://github.com/googleapis/google-cloud-python/issues
Pull requests200 (200)https://github.com/googleapis/google-cloud-python/pulls
Discussionshttps://github.com/googleapis/google-cloud-python/discussions
Actionshttps://github.com/googleapis/google-cloud-python/actions
Projectshttps://github.com/googleapis/google-cloud-python/projects
Security and qualityhttps://github.com/googleapis/google-cloud-python/security
Insightshttps://github.com/googleapis/google-cloud-python/insights
https://github.com/googleapis/google-cloud-python
New issuehttps://github.com/login?return_to=https://github.com/googleapis/google-cloud-python/issues/18536
New issuehttps://github.com/login?return_to=https://github.com/googleapis/google-cloud-python/issues/18536
refactor(gapic-generator): deduplicate sync/async prep_wrapped_messages and transport method wrappinghttps://github.com/googleapis/google-cloud-python/issues/18536#top
https://github.com/chalmerlowe
chalmerlowehttps://github.com/chalmerlowe
on Oct 1, 2026https://github.com/googleapis/google-cloud-python/issues/18536#issue-5661765921
feat(observability): implement universal 4-path OpenTelemetry tracing #18433https://github.com/googleapis/google-cloud-python/pull/18433
Sign up for freehttps://github.com/signup?return_to=https://github.com/googleapis/google-cloud-python/issues/18536
Sign in to commenthttps://github.com/login?return_to=https://github.com/googleapis/google-cloud-python/issues/18536
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.