Skip to content

Commit 526ff10

Browse files
committed
update goldens
1 parent c6bae7b commit 526ff10

5 files changed

Lines changed: 33 additions & 93 deletions

File tree

  • packages
    • gapic-generator
    • google-api-core/google/api_core/gapic_v1

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,33 +215,21 @@ def setup_request_id(
215215
# Auto-populate if the key is missing, None, or falsy (e.g., empty string '').
216216
should_populate = not request.get(field_name)
217217
else:
218-
# Case 2: Object request (proto-plus wrapper, pure protobuf message, or mock/dict-like object).
219-
pb_msg = getattr(request, "_pb", None)
220-
is_proto_plus = pb_msg is not None and hasattr(pb_msg, "HasField")
218+
# Case 2: Object request (proto-plus wrapper or pure protobuf message).
221219
if is_proto3_optional:
222-
if is_proto_plus and pb_msg is not None:
223-
# Case 2a: Proto-plus message with explicit presence.
224-
# `proto.Message` instances wrap an underlying C++/Python protobuf message in `._pb`.
225-
# We check `pb_msg.HasField(field_name)` to determine if the field was set by the user.
226-
try:
227-
should_populate = not pb_msg.HasField(field_name)
228-
except ValueError:
229-
# `HasField` raises ValueError if the field does not support presence (e.g., non-optional field).
230-
# Fall back to checking if the attribute value is explicitly None.
231-
should_populate = getattr(request, field_name, None) is None
232-
else:
233-
# Case 2b: Pure protobuf message or custom object with explicit presence.
234-
try:
235-
should_populate = not request.HasField(field_name)
236-
except (AttributeError, ValueError):
237-
# Fall back for objects/mocks that do not implement `HasField` or where `HasField` fails.
238-
should_populate = getattr(request, field_name, None) is None
220+
# Extract the protobuf from proto-plus if wrapped.
221+
pure_pb: google.protobuf.message.Message = getattr(request, "_pb", request)
222+
try:
223+
should_populate = not pure_pb.HasField(field_name)
224+
except (AttributeError, ValueError):
225+
# Fall back if `HasField` fails or is unsupported.
226+
should_populate = getattr(pure_pb, field_name, None) is None
239227
else:
240-
# Case 2c: Object request without explicit presence (`is_proto3_optional=False`).
228+
# Case 2b: Object request without explicit presence (`is_proto3_optional=False`).
241229
# Auto-populate if the field value is falsy (None or empty string '').
242-
should_populate = not getattr(request, field_name, None)
230+
should_populate = not bool(getattr(request, field_name, False))
243231

244-
# Consolidate mutation to a single, clean DRY block.
232+
# If the field was found to be empty, set random id
245233
if should_populate:
246234
generated_id = str(uuid.uuid4())
247235
if isinstance(request, dict):

packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_compat.py.j2

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,6 @@ class MockRequest:
248248
for k, v in kwargs.items():
249249
setattr(self, k, v)
250250

251-
def __contains__(self, key):
252-
return hasattr(self, key)
253-
254251

255252
class MockProtoRequest:
256253
def __init__(self, **kwargs):
@@ -267,27 +264,18 @@ class MockProtoPlusRequest:
267264
for k, v in kwargs.items():
268265
setattr(self, k, v)
269266

270-
def __contains__(self, key):
271-
return hasattr(self, key)
272-
273267

274268
class MockValueErrorRequest:
275269
def HasField(self, key):
276270
raise ValueError("Mismatched field")
277271

278-
def __contains__(self, key):
279-
return hasattr(self, key)
280-
281272

282273
class MockProtoPlusValueErrorRequest:
283274
def __init__(self, **kwargs):
284275
self._pb = MockValueErrorRequest()
285276
for k, v in kwargs.items():
286277
setattr(self, k, v)
287278

288-
def __contains__(self, key):
289-
return hasattr(self, key)
290-
291279
UUID_REGEX = r"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}"
292280

293281
@pytest.mark.parametrize(

packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/_compat.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -212,33 +212,21 @@ def setup_request_id(
212212
# Auto-populate if the key is missing, None, or falsy (e.g., empty string '').
213213
should_populate = not request.get(field_name)
214214
else:
215-
# Case 2: Object request (proto-plus wrapper, pure protobuf message, or mock/dict-like object).
216-
pb_msg = getattr(request, "_pb", None)
217-
is_proto_plus = pb_msg is not None and hasattr(pb_msg, "HasField")
215+
# Case 2: Object request (proto-plus wrapper or pure protobuf message).
218216
if is_proto3_optional:
219-
if is_proto_plus and pb_msg is not None:
220-
# Case 2a: Proto-plus message with explicit presence.
221-
# `proto.Message` instances wrap an underlying C++/Python protobuf message in `._pb`.
222-
# We check `pb_msg.HasField(field_name)` to determine if the field was set by the user.
223-
try:
224-
should_populate = not pb_msg.HasField(field_name)
225-
except ValueError:
226-
# `HasField` raises ValueError if the field does not support presence (e.g., non-optional field).
227-
# Fall back to checking if the attribute value is explicitly None.
228-
should_populate = getattr(request, field_name, None) is None
229-
else:
230-
# Case 2b: Pure protobuf message or custom object with explicit presence.
231-
try:
232-
should_populate = not request.HasField(field_name)
233-
except (AttributeError, ValueError):
234-
# Fall back for objects/mocks that do not implement `HasField` or where `HasField` fails.
235-
should_populate = getattr(request, field_name, None) is None
217+
# Extract the protobuf from proto-plus if wrapped.
218+
pure_pb: google.protobuf.message.Message = getattr(request, "_pb", request)
219+
try:
220+
should_populate = not pure_pb.HasField(field_name)
221+
except (AttributeError, ValueError):
222+
# Fall back if `HasField` fails or is unsupported.
223+
should_populate = getattr(pure_pb, field_name, None) is None
236224
else:
237-
# Case 2c: Object request without explicit presence (`is_proto3_optional=False`).
225+
# Case 2b: Object request without explicit presence (`is_proto3_optional=False`).
238226
# Auto-populate if the field value is falsy (None or empty string '').
239-
should_populate = not getattr(request, field_name, None)
227+
should_populate = not bool(getattr(request, field_name, False))
240228

241-
# Consolidate mutation to a single, clean DRY block.
229+
# If the field was found to be empty, set random id
242230
if should_populate:
243231
generated_id = str(uuid.uuid4())
244232
if isinstance(request, dict):

packages/gapic-generator/tests/integration/goldens/storagebatchoperations/tests/unit/gapic/storagebatchoperations_v1/test_compat.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,6 @@ def __init__(self, **kwargs):
247247
for k, v in kwargs.items():
248248
setattr(self, k, v)
249249

250-
def __contains__(self, key):
251-
return hasattr(self, key)
252-
253250

254251
class MockProtoRequest:
255252
def __init__(self, **kwargs):
@@ -266,27 +263,18 @@ def __init__(self, **kwargs):
266263
for k, v in kwargs.items():
267264
setattr(self, k, v)
268265

269-
def __contains__(self, key):
270-
return hasattr(self, key)
271-
272266

273267
class MockValueErrorRequest:
274268
def HasField(self, key):
275269
raise ValueError("Mismatched field")
276270

277-
def __contains__(self, key):
278-
return hasattr(self, key)
279-
280271

281272
class MockProtoPlusValueErrorRequest:
282273
def __init__(self, **kwargs):
283274
self._pb = MockValueErrorRequest()
284275
for k, v in kwargs.items():
285276
setattr(self, k, v)
286277

287-
def __contains__(self, key):
288-
return hasattr(self, key)
289-
290278
UUID_REGEX = r"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}"
291279

292280
@pytest.mark.parametrize(

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,21 @@ def setup_request_id(
7676
# Auto-populate if the key is missing, None, or falsy (e.g., empty string '').
7777
should_populate = not request.get(field_name)
7878
else:
79-
# Case 2: Object request (proto-plus wrapper, pure protobuf message, or mock/dict-like object).
80-
pb_msg = getattr(request, "_pb", None)
81-
is_proto_plus = pb_msg is not None and hasattr(pb_msg, "HasField")
79+
# Case 2: Object request (proto-plus wrapper or pure protobuf message).
8280
if is_proto3_optional:
83-
if is_proto_plus and pb_msg is not None:
84-
# Case 2a: Proto-plus message with explicit presence.
85-
# `proto.Message` instances wrap an underlying C++/Python protobuf message in `._pb`.
86-
# We check `pb_msg.HasField(field_name)` to determine if the field was set by the user.
87-
try:
88-
should_populate = not pb_msg.HasField(field_name)
89-
except ValueError:
90-
# `HasField` raises ValueError if the field does not support presence (e.g., non-optional field).
91-
# Fall back to checking if the attribute value is explicitly None.
92-
should_populate = getattr(request, field_name, None) is None
93-
else:
94-
# Case 2b: Pure protobuf message or custom object with explicit presence.
95-
try:
96-
should_populate = not request.HasField(field_name)
97-
except (AttributeError, ValueError):
98-
# Fall back for objects/mocks that do not implement `HasField` or where `HasField` fails.
99-
should_populate = getattr(request, field_name, None) is None
81+
# Extract the protobuf from proto-plus if wrapped.
82+
pure_pb: google.protobuf.message.Message = getattr(request, "_pb", request)
83+
try:
84+
should_populate = not pure_pb.HasField(field_name)
85+
except (AttributeError, ValueError):
86+
# Fall back if `HasField` fails or is unsupported.
87+
should_populate = getattr(pure_pb, field_name, None) is None
10088
else:
101-
# Case 2c: Object request without explicit presence (`is_proto3_optional=False`).
89+
# Case 2b: Object request without explicit presence (`is_proto3_optional=False`).
10290
# Auto-populate if the field value is falsy (None or empty string '').
103-
should_populate = not getattr(request, field_name, None)
91+
should_populate = not bool(getattr(request, field_name, False))
10492

105-
# Consolidate mutation to a single, clean DRY block.
93+
# If the field was found to be empty, set random id
10694
if should_populate:
10795
generated_id = str(uuid.uuid4())
10896
if isinstance(request, dict):

0 commit comments

Comments
 (0)