Skip to content

Commit f61bfa7

Browse files
committed
docs(generator): clarify lower-bound google-api-core version comment in _compat.py.j2
1 parent 43e13bd commit f61bfa7

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/_compat.py.j2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ try:
2020
get_universe_domain,
2121
)
2222
except ImportError:
23-
# TODO(https://github.com/googleapis/google-cloud-python/issues/17813): Remove these fallbacks when google-api-core >= 2.18.0 is the minimum required version.
23+
# TODO(https://github.com/googleapis/google-cloud-python/issues/17813): Universe domain support was introduced in google-api-core 2.18.0.
24+
# Remove these fallback definitions when google-api-core >= 2.18.0 becomes the minimum required version in generated client setup dependencies.
2425
def get_default_mtls_endpoint(api_endpoint: Optional[str]) -> Optional[str]:
2526
"""Converts api endpoint to mTLS endpoint."""
2627
if not api_endpoint:

packages/google-api-core/google/api_core/gapic_v1/requests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def setup_request_id(
6565
setattr(request, field_name, str(uuid.uuid4()))
6666
except (AttributeError, ValueError):
6767
# Proto-plus messages or other objects
68+
if hasattr(request, "_pb"):
69+
try:
70+
if not request._pb.HasField(field_name):
71+
setattr(request, field_name, str(uuid.uuid4()))
72+
return
73+
except (AttributeError, ValueError):
74+
pass
6875
if getattr(request, field_name, None) is None:
6976
setattr(request, field_name, str(uuid.uuid4()))
7077
else:

packages/google-api-core/tests/unit/gapic/test_requests.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,18 @@ def test_setup_request_id(request_obj, is_proto3_optional, expected):
113113
assert value == expected
114114

115115

116-
def test_setup_request_id_assertion_strictness(mocker):
117-
# Mock uuid.uuid4 to return a UUID with trailing characters
118-
mock_uuid = mocker.patch("uuid.uuid4")
119-
mock_uuid.return_value.__str__.return_value = (
120-
"12345678-1234-4123-8123-123456789012-extra"
121-
)
116+
from unittest import mock
122117

123-
# We expect test_setup_request_id to fail (raise AssertionError) because the UUID is invalid.
124-
# If test_setup_request_id uses re.fullmatch, it will fail (raise AssertionError), so pytest.raises passes.
125-
# If test_setup_request_id uses re.match, it will NOT fail, so pytest.raises fails!
126-
with pytest.raises(AssertionError):
127-
test_setup_request_id(MockRequest(), is_proto3_optional=True, expected="uuid")
118+
119+
def test_setup_request_id_assertion_strictness():
120+
# Mock uuid.uuid4 to return a UUID with trailing characters
121+
with mock.patch("uuid.uuid4") as mock_uuid:
122+
mock_uuid.return_value.__str__.return_value = (
123+
"12345678-1234-4123-8123-123456789012-extra"
124+
)
125+
126+
# We expect test_setup_request_id to fail (raise AssertionError) because the UUID is invalid.
127+
# If test_setup_request_id uses re.fullmatch, it will fail (raise AssertionError), so pytest.raises passes.
128+
# If test_setup_request_id uses re.match, it will NOT fail, so pytest.raises fails!
129+
with pytest.raises(AssertionError):
130+
test_setup_request_id(MockRequest(), is_proto3_optional=True, expected="uuid")

0 commit comments

Comments
 (0)