Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit bc3836b

Browse files
fix: improve type checking (#2530)
1 parent 1578a2a commit bc3836b

File tree

91 files changed

+3690
-754
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+3690
-754
lines changed

gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/_mixins.py.j2

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% if "ListOperations" in api.mixin_api_methods %}
44
def list_operations(
55
self,
6-
request: Optional[operations_pb2.ListOperationsRequest] = None,
6+
request: Optional[Union[operations_pb2.ListOperationsRequest, dict]] = None,
77
*,
88
retry: OptionalRetry = gapic_v1.method.DEFAULT,
99
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -27,8 +27,12 @@
2727
# Create or coerce a protobuf request object.
2828
# The request isn't a proto-plus wrapped type,
2929
# so it must be constructed via keyword expansion.
30-
if isinstance(request, dict):
31-
request = operations_pb2.ListOperationsRequest(**request)
30+
if request is None:
31+
request_pb = operations_pb2.ListOperationsRequest()
32+
elif isinstance(request, dict):
33+
request_pb = operations_pb2.ListOperationsRequest(**request)
34+
else:
35+
request_pb = request
3236

3337
# Wrap the RPC method; this adds retry and timeout information,
3438
# and friendly error handling.
@@ -38,12 +42,12 @@
3842
# add these here.
3943
metadata = tuple(metadata) + (
4044
gapic_v1.routing_header.to_grpc_metadata(
41-
(("name", request.name),)),
45+
(("name", request_pb.name),)),
4246
)
4347

4448
# Send the request.
4549
response = rpc(
46-
request, retry=retry, timeout=timeout, metadata=metadata,)
50+
request_pb, retry=retry, timeout=timeout, metadata=metadata,)
4751

4852
# Done; return the response.
4953
return response
@@ -53,7 +57,7 @@
5357
{% if "GetOperation" in api.mixin_api_methods %}
5458
def get_operation(
5559
self,
56-
request: Optional[operations_pb2.GetOperationRequest] = None,
60+
request: Optional[Union[operations_pb2.GetOperationRequest, dict]] = None,
5761
*,
5862
retry: OptionalRetry = gapic_v1.method.DEFAULT,
5963
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -77,8 +81,12 @@
7781
# Create or coerce a protobuf request object.
7882
# The request isn't a proto-plus wrapped type,
7983
# so it must be constructed via keyword expansion.
80-
if isinstance(request, dict):
81-
request = operations_pb2.GetOperationRequest(**request)
84+
if request is None:
85+
request_pb = operations_pb2.GetOperationRequest()
86+
elif isinstance(request, dict):
87+
request_pb = operations_pb2.GetOperationRequest(**request)
88+
else:
89+
request_pb = request
8290

8391
# Wrap the RPC method; this adds retry and timeout information,
8492
# and friendly error handling.
@@ -88,12 +96,12 @@
8896
# add these here.
8997
metadata = tuple(metadata) + (
9098
gapic_v1.routing_header.to_grpc_metadata(
91-
(("name", request.name),)),
99+
(("name", request_pb.name),)),
92100
)
93101

94102
# Send the request.
95103
response = rpc(
96-
request, retry=retry, timeout=timeout, metadata=metadata,)
104+
request_pb, retry=retry, timeout=timeout, metadata=metadata,)
97105

98106
# Done; return the response.
99107
return response
@@ -102,7 +110,7 @@
102110
{% if "DeleteOperation" in api.mixin_api_methods %}
103111
def delete_operation(
104112
self,
105-
request: Optional[operations_pb2.DeleteOperationRequest] = None,
113+
request: Optional[Union[operations_pb2.DeleteOperationRequest, dict]] = None,
106114
*,
107115
retry: OptionalRetry = gapic_v1.method.DEFAULT,
108116
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -130,8 +138,12 @@
130138
# Create or coerce a protobuf request object.
131139
# The request isn't a proto-plus wrapped type,
132140
# so it must be constructed via keyword expansion.
133-
if isinstance(request, dict):
134-
request = operations_pb2.DeleteOperationRequest(**request)
141+
if request is None:
142+
request_pb = operations_pb2.DeleteOperationRequest()
143+
elif isinstance(request, dict):
144+
request_pb = operations_pb2.DeleteOperationRequest(**request)
145+
else:
146+
request_pb = request
135147

136148
# Wrap the RPC method; this adds retry and timeout information,
137149
# and friendly error handling.
@@ -141,17 +153,17 @@
141153
# add these here.
142154
metadata = tuple(metadata) + (
143155
gapic_v1.routing_header.to_grpc_metadata(
144-
(("name", request.name),)),
156+
(("name", request_pb.name),)),
145157
)
146158

147159
# Send the request.
148-
rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
160+
rpc(request_pb, retry=retry, timeout=timeout, metadata=metadata,)
149161
{% endif %}
150162

151163
{% if "CancelOperation" in api.mixin_api_methods %}
152164
def cancel_operation(
153165
self,
154-
request: Optional[operations_pb2.CancelOperationRequest] = None,
166+
request: Optional[Union[operations_pb2.CancelOperationRequest, dict]] = None,
155167
*,
156168
retry: OptionalRetry = gapic_v1.method.DEFAULT,
157169
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -178,8 +190,12 @@
178190
# Create or coerce a protobuf request object.
179191
# The request isn't a proto-plus wrapped type,
180192
# so it must be constructed via keyword expansion.
181-
if isinstance(request, dict):
182-
request = operations_pb2.CancelOperationRequest(**request)
193+
if request is None:
194+
request_pb = operations_pb2.CancelOperationRequest()
195+
elif isinstance(request, dict):
196+
request_pb = operations_pb2.CancelOperationRequest(**request)
197+
else:
198+
request_pb = request
183199

184200
# Wrap the RPC method; this adds retry and timeout information,
185201
# and friendly error handling.
@@ -189,17 +205,17 @@
189205
# add these here.
190206
metadata = tuple(metadata) + (
191207
gapic_v1.routing_header.to_grpc_metadata(
192-
(("name", request.name),)),
208+
(("name", request_pb.name),)),
193209
)
194210

195211
# Send the request.
196-
rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
212+
rpc(request_pb, retry=retry, timeout=timeout, metadata=metadata,)
197213
{% endif %}
198214

199215
{% if "WaitOperation" in api.mixin_api_methods %}
200216
def wait_operation(
201217
self,
202-
request: Optional[operations_pb2.WaitOperationRequest] = None,
218+
request: Optional[Union[operations_pb2.WaitOperationRequest, dict]] = None,
203219
*,
204220
retry: OptionalRetry = gapic_v1.method.DEFAULT,
205221
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -229,8 +245,12 @@
229245
# Create or coerce a protobuf request object.
230246
# The request isn't a proto-plus wrapped type,
231247
# so it must be constructed via keyword expansion.
232-
if isinstance(request, dict):
233-
request = operations_pb2.WaitOperationRequest(**request)
248+
if request is None:
249+
request_pb = operations_pb2.WaitOperationRequest()
250+
elif isinstance(request, dict):
251+
request_pb = operations_pb2.WaitOperationRequest(**request)
252+
else:
253+
request_pb = request
234254

235255
# Wrap the RPC method; this adds retry and timeout information,
236256
# and friendly error handling.
@@ -240,7 +260,7 @@
240260

241261
# Send the request.
242262
response = rpc(
243-
request, retry=retry, timeout=timeout, metadata=metadata,)
263+
request_pb, retry=retry, timeout=timeout, metadata=metadata,)
244264

245265
# Done; return the response.
246266
return response
@@ -254,7 +274,7 @@
254274
{% if "SetIamPolicy" in api.mixin_api_methods %}
255275
def set_iam_policy(
256276
self,
257-
request: Optional[iam_policy_pb2.SetIamPolicyRequest] = None,
277+
request: Optional[Union[iam_policy_pb2.SetIamPolicyRequest, dict]] = None,
258278
*,
259279
retry: OptionalRetry = gapic_v1.method.DEFAULT,
260280
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -344,8 +364,12 @@
344364

345365
# The request isn't a proto-plus wrapped type,
346366
# so it must be constructed via keyword expansion.
347-
if isinstance(request, dict):
348-
request = iam_policy_pb2.SetIamPolicyRequest(**request)
367+
if request is None:
368+
request_pb = iam_policy_pb2.SetIamPolicyRequest()
369+
elif isinstance(request, dict):
370+
request_pb = iam_policy_pb2.SetIamPolicyRequest(**request)
371+
else:
372+
request_pb = request
349373

350374
# Wrap the RPC method; this adds retry and timeout information,
351375
# and friendly error handling.
@@ -355,12 +379,12 @@
355379
# add these here.
356380
metadata = tuple(metadata) + (
357381
gapic_v1.routing_header.to_grpc_metadata(
358-
(("resource", request.resource),)),
382+
(("resource", request_pb.resource),)),
359383
)
360384

361385
# Send the request.
362386
response = rpc(
363-
request, retry=retry, timeout=timeout, metadata=metadata,)
387+
request_pb, retry=retry, timeout=timeout, metadata=metadata,)
364388

365389
# Done; return the response.
366390
return response
@@ -369,7 +393,7 @@
369393
{% if "GetIamPolicy" in api.mixin_api_methods %}
370394
def get_iam_policy(
371395
self,
372-
request: Optional[iam_policy_pb2.GetIamPolicyRequest] = None,
396+
request: Optional[Union[iam_policy_pb2.GetIamPolicyRequest, dict]] = None,
373397
*,
374398
retry: OptionalRetry = gapic_v1.method.DEFAULT,
375399
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -460,8 +484,12 @@
460484

461485
# The request isn't a proto-plus wrapped type,
462486
# so it must be constructed via keyword expansion.
463-
if isinstance(request, dict):
464-
request = iam_policy_pb2.GetIamPolicyRequest(**request)
487+
if request is None:
488+
request_pb = iam_policy_pb2.GetIamPolicyRequest()
489+
elif isinstance(request, dict):
490+
request_pb = iam_policy_pb2.GetIamPolicyRequest(**request)
491+
else:
492+
request_pb = request
465493

466494
# Wrap the RPC method; this adds retry and timeout information,
467495
# and friendly error handling.
@@ -471,12 +499,12 @@
471499
# add these here.
472500
metadata = tuple(metadata) + (
473501
gapic_v1.routing_header.to_grpc_metadata(
474-
(("resource", request.resource),)),
502+
(("resource", request_pb.resource),)),
475503
)
476504

477505
# Send the request.
478506
response = rpc(
479-
request, retry=retry, timeout=timeout, metadata=metadata,)
507+
request_pb, retry=retry, timeout=timeout, metadata=metadata,)
480508

481509
# Done; return the response.
482510
return response
@@ -485,7 +513,7 @@
485513
{% if "TestIamPermissions" in api.mixin_api_methods %}
486514
def test_iam_permissions(
487515
self,
488-
request: Optional[iam_policy_pb2.TestIamPermissionsRequest] = None,
516+
request: Optional[Union[iam_policy_pb2.TestIamPermissionsRequest, dict]] = None,
489517
*,
490518
retry: OptionalRetry = gapic_v1.method.DEFAULT,
491519
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -514,8 +542,12 @@
514542

515543
# The request isn't a proto-plus wrapped type,
516544
# so it must be constructed via keyword expansion.
517-
if isinstance(request, dict):
518-
request = iam_policy_pb2.TestIamPermissionsRequest(**request)
545+
if request is None:
546+
request_pb = iam_policy_pb2.TestIamPermissionsRequest()
547+
elif isinstance(request, dict):
548+
request_pb = iam_policy_pb2.TestIamPermissionsRequest(**request)
549+
else:
550+
request_pb = request
519551

520552
# Wrap the RPC method; this adds retry and timeout information,
521553
# and friendly error handling.
@@ -525,12 +557,12 @@
525557
# add these here.
526558
metadata = tuple(metadata) + (
527559
gapic_v1.routing_header.to_grpc_metadata(
528-
(("resource", request.resource),)),
560+
(("resource", request_pb.resource),)),
529561
)
530562

531563
# Send the request.
532564
response = rpc(
533-
request, retry=retry, timeout=timeout, metadata=metadata,)
565+
request_pb, retry=retry, timeout=timeout, metadata=metadata,)
534566

535567
# Done; return the response.
536568
return response
@@ -543,7 +575,7 @@
543575
{% if "GetLocation" in api.mixin_api_methods %}
544576
def get_location(
545577
self,
546-
request: Optional[locations_pb2.GetLocationRequest] = None,
578+
request: Optional[Union[locations_pb2.GetLocationRequest, dict]] = None,
547579
*,
548580
retry: OptionalRetry = gapic_v1.method.DEFAULT,
549581
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -567,8 +599,12 @@
567599
# Create or coerce a protobuf request object.
568600
# The request isn't a proto-plus wrapped type,
569601
# so it must be constructed via keyword expansion.
570-
if isinstance(request, dict):
571-
request = locations_pb2.GetLocationRequest(**request)
602+
if request is None:
603+
request_pb = locations_pb2.GetLocationRequest()
604+
elif isinstance(request, dict):
605+
request_pb = locations_pb2.GetLocationRequest(**request)
606+
else:
607+
request_pb = request
572608

573609
# Wrap the RPC method; this adds retry and timeout information,
574610
# and friendly error handling.
@@ -578,12 +614,12 @@
578614
# add these here.
579615
metadata = tuple(metadata) + (
580616
gapic_v1.routing_header.to_grpc_metadata(
581-
(("name", request.name),)),
617+
(("name", request_pb.name),)),
582618
)
583619

584620
# Send the request.
585621
response = rpc(
586-
request, retry=retry, timeout=timeout, metadata=metadata,)
622+
request_pb, retry=retry, timeout=timeout, metadata=metadata,)
587623

588624
# Done; return the response.
589625
return response
@@ -592,7 +628,7 @@
592628
{% if "ListLocations" in api.mixin_api_methods %}
593629
def list_locations(
594630
self,
595-
request: Optional[locations_pb2.ListLocationsRequest] = None,
631+
request: Optional[Union[locations_pb2.ListLocationsRequest, dict]] = None,
596632
*,
597633
retry: OptionalRetry = gapic_v1.method.DEFAULT,
598634
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
@@ -616,8 +652,12 @@
616652
# Create or coerce a protobuf request object.
617653
# The request isn't a proto-plus wrapped type,
618654
# so it must be constructed via keyword expansion.
619-
if isinstance(request, dict):
620-
request = locations_pb2.ListLocationsRequest(**request)
655+
if request is None:
656+
request_pb = locations_pb2.ListLocationsRequest()
657+
elif isinstance(request, dict):
658+
request_pb = locations_pb2.ListLocationsRequest(**request)
659+
else:
660+
request_pb = request
621661

622662
# Wrap the RPC method; this adds retry and timeout information,
623663
# and friendly error handling.
@@ -627,12 +667,12 @@
627667
# add these here.
628668
metadata = tuple(metadata) + (
629669
gapic_v1.routing_header.to_grpc_metadata(
630-
(("name", request.name),)),
670+
(("name", request_pb.name),)),
631671
)
632672

633673
# Send the request.
634674
response = rpc(
635-
request, retry=retry, timeout=timeout, metadata=metadata,)
675+
request_pb, retry=retry, timeout=timeout, metadata=metadata,)
636676

637677
# Done; return the response.
638678
return response

0 commit comments

Comments
 (0)