Skip to content

chore(generator): move read_environment_variables to _compat.py.j2 - #17997

Open
hebaalazzeh wants to merge 2 commits into
mainfrom
chore/move-read-environment-variables
Open

chore(generator): move read_environment_variables to _compat.py.j2#17997
hebaalazzeh wants to merge 2 commits into
mainfrom
chore/move-read-environment-variables

Conversation

@hebaalazzeh

Copy link
Copy Markdown
Contributor

We move _read_environment_variables and its associated unit tests into _compat.py.j2 as read_environment_variables.

This reduces code duplication in packages that have multiple services. In the future, once should_use_mtls_endpoint is added to google-auth, we can defer parsing GOOGLE_API_USE_MTLS_ENDPOINT upstream.

Towards: #17883

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the environment variable reading logic by moving _read_environment_variables from the service client template into a shared helper function read_environment_variables in _compat.py.j2, and adds corresponding unit tests. The review feedback highlights critical issues: a missing closing parenthesis in _compat.py.j2 that will cause a syntax error, and missing imports for MutualTLSChannelError in both _compat.py.j2 and test_compat.py.j2 which will lead to runtime NameErrors.

google.auth.exceptions.MutualTLSChannelError: If GOOGLE_API_USE_MTLS_ENDPOINT
is not any of ["auto", "never", "always"].
"""
use_client_cert = should_use_client_cert()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

MutualTLSChannelError is raised in read_environment_variables but is not imported in this file. Please import it from google.auth.exceptions to avoid a NameError at runtime.

    from google.auth.exceptions import MutualTLSChannelError

    use_client_cert = should_use_client_cert()

{% set package_path = api.naming.module_namespace|join('.') + "." + api.naming.versioned_module_name %}
from {{package_path}}._compat import transcode_request
from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert
from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert, read_environment_variables

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

MutualTLSChannelError is used in test_read_environment_variables but is not imported in this test file. Please import it from google.auth.exceptions to avoid a NameError during test execution.

from {{package_path}}._compat import get_universe_domain, get_api_endpoint, get_default_mtls_endpoint, should_use_client_cert, read_environment_variables
from google.auth.exceptions import MutualTLSChannelError

@hebaalazzeh hebaalazzeh self-assigned this Aug 5, 2026
@hebaalazzeh
hebaalazzeh marked this pull request as ready for review August 5, 2026 05:04
@hebaalazzeh
hebaalazzeh requested a review from a team as a code owner August 5, 2026 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant