Skip to content

Commit 868f201

Browse files
gkevinzhenggcf-owl-bot[bot]parthea
authored
fix: Fixed internal method generation naming issues (#2365)
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent b7e0f7f commit 868f201

File tree

94 files changed

+3015
-2484
lines changed

Some content is hidden

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

94 files changed

+3015
-2484
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
345345
{% if method.operation_service %}{# Extended Operations LRO #}
346346
def {{ method.name|snake_case }}_unary(self,
347347
{% else %}
348-
def {{ method.safe_name|snake_case }}(self,
348+
def {{ method.client_method_name|snake_case }}(self,
349349
{% endif %}{# Extended Operations LRO #}
350350
{% if not method.client_streaming %}
351351
request: Optional[Union[{{ method.input.ident }}, dict]] = None,

gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def test_{{ service.client_name|snake_case }}_create_channel_credentials_file(cl
516516
{% endif %}
517517

518518

519-
{% for method in service.methods.values() if 'grpc' in opts.transport %}{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.safe_name|snake_case %}
519+
{% for method in service.methods.values() if 'grpc' in opts.transport %}{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.client_method_name|snake_case %}
520520
@pytest.mark.parametrize("request_type", [
521521
{{ method.input.ident }},
522522
dict,
@@ -579,7 +579,7 @@ def test_{{ method_name }}(request_type, transport: str = 'grpc'):
579579
)
580580
{% endif %}
581581
{% if method.client_streaming %}
582-
response = client.{{ method.safe_name|snake_case }}(iter(requests))
582+
response = client.{{ method.client_method_name|snake_case }}(iter(requests))
583583
{% else %}
584584
response = client.{{ method_name }}(request)
585585
{% endif %}
@@ -1053,7 +1053,7 @@ def test_{{ method_name }}_raw_page_lro():
10531053

10541054
{% endfor %} {# method in methods for grpc #}
10551055

1056-
{% for method in service.methods.values() if 'rest' in opts.transport %}{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.safe_name|snake_case %}{% if method.http_options %}
1056+
{% for method in service.methods.values() if 'rest' in opts.transport %}{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.client_method_name|snake_case %}{% if method.http_options %}
10571057
{# TODO(kbandes): remove this if condition when client streaming are supported. #}
10581058
{% if not method.client_streaming %}
10591059
@pytest.mark.parametrize("request_type", [
@@ -1252,7 +1252,7 @@ def test_{{ method.name|snake_case }}_rest(request_type):
12521252
req.return_value = response_value
12531253
req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"}
12541254
{% if method.client_streaming %}
1255-
response = client.{{ method.safe_name|snake_case }}(iter(requests))
1255+
response = client.{{ method.client_method_name|snake_case }}(iter(requests))
12561256
{% elif method.server_streaming %}
12571257
with mock.patch.object(response_value, 'iter_content') as iter_content:
12581258
iter_content.return_value = iter(json_return_value)
@@ -1550,7 +1550,7 @@ def test_{{ method_name }}_rest_bad_request(transport: str = 'rest', request_typ
15501550
req.return_value = response_value
15511551
req.return_value.headers = {"header-1": "value-1", "header-2": "value-2"}
15521552
{% if method.client_streaming %}
1553-
client.{{ method.safe_name|snake_case }}(iter(requests))
1553+
client.{{ method.client_method_name|snake_case }}(iter(requests))
15541554
{% else %}
15551555
client.{{ method_name }}(request)
15561556
{% endif %}
@@ -1818,7 +1818,7 @@ def test_{{ method_name }}_rest_no_http_options():
18181818
{% endfor -%} {#- method in methods for rest #}
18191819

18201820
{% for method in service.methods.values() if 'rest' in opts.transport and
1821-
not method.http_options %}{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.safe_name|snake_case %}
1821+
not method.http_options %}{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.client_method_name|snake_case %}
18221822
def test_{{ method_name }}_rest_error():
18231823
client = {{ service.client_name }}(
18241824
credentials=ga_credentials.AnonymousCredentials(),

gapic/samplegen/samplegen.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,9 @@ def generate_sample_specs(
10631063
# [{START|END} ${apishortname}_${apiVersion}_generated_${serviceName}_${rpcName}_{sync|async|rest}]
10641064
region_tag = f"{api_short_name}_{api_version}_generated_{service_name}_{rpc_name}_{sync_or_async}"
10651065

1066-
# We assume that the only methods that start with an underscore are internal methods.
1067-
is_internal = rpc_name.startswith("_")
1066+
is_internal = api_schema.all_methods[
1067+
f"{api_schema.naming.proto_package}.{service_name}.{rpc_name}"
1068+
].is_internal
10681069
if is_internal:
10691070
region_tag += "_internal"
10701071

@@ -1119,7 +1120,9 @@ def _fill_sample_metadata(sample: dict, api_schema: api.API):
11191120

11201121
# Client Method
11211122
setattr(snippet_metadata.client_method, "async", async_)
1122-
snippet_metadata.client_method.short_name = utils.to_snake_case(method.name)
1123+
snippet_metadata.client_method.short_name = utils.to_snake_case(
1124+
method.client_method_name
1125+
)
11231126
snippet_metadata.client_method.full_name = f"{snippet_metadata.client_method.client.full_name}.{snippet_metadata.client_method.short_name}"
11241127

11251128
if not method.void:
@@ -1217,6 +1220,7 @@ def generate_sample(
12171220
)
12181221

12191222
calling_form = types.CallingForm.method_default(rpc)
1223+
sample["is_internal"] = rpc.is_internal
12201224

12211225
v = Validator(rpc, api_schema)
12221226
# Tweak some small aspects of the sample to set defaults for optional

gapic/schema/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ def gapic_metadata(self, options: Options) -> gapic_metadata_pb2.GapicMetadata:
687687
transport.library_client = client_name
688688
for method in methods:
689689
method_desc = transport.rpcs.get_or_create(method.name)
690-
method_desc.methods.append(to_snake_case(method.name))
690+
method_desc.methods.append(to_snake_case(method.client_method_name))
691691

692692
return gm
693693

gapic/schema/wrappers.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,11 +1499,14 @@ def __getattr__(self, name):
14991499
return getattr(self.method_pb, name)
15001500

15011501
@property
1502-
def safe_name(self) -> str:
1503-
# Used to prevent collisions with python keywords at the client level
1502+
def client_method_name(self) -> str:
1503+
"""Returns the name of the generated method name.
15041504
1505-
name = self.name
1506-
return name + "_" if name.lower() in keyword.kwlist else name
1505+
This is used to prevent collisions with Python keywords at the
1506+
client level, as well as prefix internal method names with underscore.
1507+
"""
1508+
name = self.name + "_" if self.name.lower() in keyword.kwlist else self.name
1509+
return make_private(name) if self.is_internal else name
15071510

15081511
@property
15091512
def transport_safe_name(self) -> str:
@@ -1534,14 +1537,6 @@ def is_operation_polling_method(self):
15341537
and self.options.Extensions[ex_ops_pb2.operation_polling_method]
15351538
)
15361539

1537-
@utils.cached_property
1538-
def name(self):
1539-
return (
1540-
make_private(self.method_pb.name)
1541-
if self.is_internal
1542-
else self.method_pb.name
1543-
)
1544-
15451540
@utils.cached_property
15461541
def client_output(self):
15471542
return self._client_output(enable_asyncio=False)
@@ -2435,17 +2430,10 @@ def with_internal_methods(self, *, public_methods: Set[str]) -> "Service":
24352430
Service: A version of this `Service` with `Method` objects corresponding to methods
24362431
not in `public_methods` marked as internal.
24372432
"""
2438-
2439-
# Internal methods need to be keyed with underscore prefixed method names
2440-
# (e.g. google.Service.Method -> google.Service._Method) in order for
2441-
# samplegen to work properly.
24422433
return dataclasses.replace(
24432434
self,
24442435
methods={
2445-
meth.name: meth
2446-
for meth in (
2447-
meth.with_internal_methods(public_methods=public_methods)
2448-
for meth in self.methods.values()
2449-
)
2436+
k: v.with_internal_methods(public_methods=public_methods)
2437+
for k, v in self.methods.items()
24502438
},
24512439
)

gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class {{ service.async_client_name }}:
266266
)
267267

268268
{% for method in service.methods.values() %}
269-
{% with method_name = method.safe_name|snake_case + "_unary" if method.operation_service else method.safe_name|snake_case %}
269+
{% with method_name = method.client_method_name|snake_case + "_unary" if method.operation_service else method.client_method_name|snake_case %}
270270
{%+ if not method.server_streaming %}async {% endif %}def {{ method_name }}(self,
271271
{% endwith %}
272272
{% if not method.client_streaming %}

gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,11 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
654654

655655
{% for method in service.methods.values() %}
656656
{% if method.operation_service %}{# Uses extended operations #}
657-
{{ macros.client_method(method, method.safe_name|snake_case + "_unary", snippet_index, api, service) }}
657+
{{ macros.client_method(method, method.client_method_name|snake_case + "_unary", snippet_index, api, service) }}
658658

659-
{{ macros.client_method(method, method.safe_name|snake_case, snippet_index, api, service, full_extended_lro=True) }}
659+
{{ macros.client_method(method, method.client_method_name|snake_case, snippet_index, api, service, full_extended_lro=True) }}
660660
{% else %}
661-
{{ macros.client_method(method, method.safe_name|snake_case, snippet_index, api, service) }}
661+
{{ macros.client_method(method, method.client_method_name|snake_case, snippet_index, api, service) }}
662662
{% endif %}
663663
{% endfor %}
664664

gapic/templates/examples/feature_fragments.j2

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ await{{ " "}}
235235
{%- endif -%}
236236
{% if calling_form in [calling_form_enum.RequestStreamingBidi,
237237
calling_form_enum.RequestStreamingClient] %}
238-
client.{{ sample.rpc|snake_case }}(requests=request_generator())
238+
client.{{ render_method_name(sample)|trim }}(requests=request_generator())
239239
{% else %}{# TODO: deal with flattening #}
240240
{# TODO: set up client streaming once some questions are answered #}
241-
client.{{ sample.rpc|snake_case }}({{ render_request_params_unary(sample.request)|trim }})
241+
client.{{ render_method_name(sample)|trim }}({{ render_request_params_unary(sample.request)|trim }})
242242
{% endif %}
243243
{% endmacro %}
244244

@@ -305,11 +305,15 @@ response = {{ operation_text|trim }}.result()
305305
{% endif %}
306306
{% endmacro %}
307307

308-
{% macro render_method_name(method_name) %}
309-
{{ method_name|snake_case }}
308+
{% macro render_method_name(sample) %}
309+
{% if sample.is_internal %}
310+
_{{ sample.rpc|snake_case }}
311+
{% else %}
312+
{{ sample.rpc|snake_case }}
313+
{% endif %}
310314
{% endmacro %}
311315

312-
{% macro render_main_block(method_name, full_request) %}
316+
{% macro render_main_block(sample, full_request) %}
313317
def main():
314318
import argparse
315319

@@ -330,7 +334,7 @@ def main():
330334
{% endfor %}
331335
args = parser.parse_args()
332336

333-
sample_{{ render_method_name(method_name)|trim }}({{ arg_list|join(", ") }})
337+
sample_{{ render_method_name(sample)|trim }}({{ arg_list|join(", ") }})
334338

335339

336340
if __name__ == "__main__":

gapic/templates/examples/sample.py.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
{# also need calling form #}
35-
{% if sample.transport == "grpc-async" %}async {% endif %}def sample_{{ frags.render_method_name(sample.rpc)|trim }}({{ frags.print_input_params(sample.request)|trim }}):
35+
{% if sample.transport == "grpc-async" %}async {% endif %}def sample_{{ sample.rpc|snake_case|trim }}({{ frags.print_input_params(sample.request)|trim }}):
3636
{{ frags.render_client_setup(sample.module_name, sample.client_name)|indent }}
3737
{{ frags.render_request_setup(sample.request, sample.request_module_name, sample.request_type, calling_form, calling_form_enum)|indent }}
3838
{% with method_call = frags.render_method_call(sample, calling_form, calling_form_enum, sample.transport) %}
@@ -41,5 +41,5 @@
4141

4242
# [END {{ sample.id }}]
4343
{# TODO: Enable main block (or decide to remove main block from python sample) #}
44-
{# {{ frags.render_main_block(sample.rpc, sample.request) }} #}
44+
{# {{ frags.render_main_block(sample, sample.request) }} #}
4545
{% endblock %}

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ def test_{{ service.client_name|snake_case }}_create_channel_credentials_file(cl
927927
{% endfor -%} {#- method in methods for rest #}
928928

929929
{% for method in service.methods.values() if 'rest' in opts.transport and
930-
not method.http_options %}{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.safe_name|snake_case %}
930+
not method.http_options %}{% with method_name = method.name|snake_case + "_unary" if method.operation_service else method.client_method_name|snake_case %}
931931
def test_{{ method_name }}_rest_error():
932932
client = {{ service.client_name }}(
933933
credentials=ga_credentials.AnonymousCredentials(),

0 commit comments

Comments
 (0)