gapic-generator-python icon indicating copy to clipboard operation
gapic-generator-python copied to clipboard

`await` missing in async sample code

Open ryuheechul opened this issue 2 years ago • 6 comments

For example, this one https://github.com/googleapis/google-cloud-python/blob/e6b5ea0a5b52ae56c944457c533b0ad225020357/packages/google-cloud-resource-manager/samples/generated_samples/cloudresourcemanager_v3_generated_folders_list_folders_async.py#L47

should be as below

-     page_result = client.list_folders(request=request)
+     page_result = await client.list_folders(request=request)

And it seems to be that many sample code are like that not just this one

ryuheechul avatar Dec 12 '23 18:12 ryuheechul

Hi @parthea , I would like to work on it. Please assign this to me.

kitokirti avatar Jan 19 '24 15:01 kitokirti

The sample in https://github.com/googleapis/gapic-generator-python/issues/1912#issue-2091455570 is automatically generated using the code generator in this repository. See a similar issue with the async samples and the corresponding fix to get an idea of the type of fix that would be needed for this issue.

parthea avatar Jan 19 '24 21:01 parthea

Hi @parthea , do we need to make changes in this particular file or all the files under generated_samples folder? Please suggest.

kitokirti avatar Jan 22 '24 09:01 kitokirti

@kitokirti I think the change will look something like this.

partheniou@partheniou-vm-3:~/git/gapic-generator-python$ git diff
diff --git a/gapic/templates/examples/feature_fragments.j2 b/gapic/templates/examples/feature_fragments.j2
index aa1ae320..a68e6554 100644
--- a/gapic/templates/examples/feature_fragments.j2
+++ b/gapic/templates/examples/feature_fragments.j2
@@ -264,7 +264,7 @@ operation
 {% endfor %}
 {% endif %}
 {% elif calling_form == calling_form_enum.RequestPagedAll %}
-page_result = {{ method_invocation_text|trim }}
+page_result = {% if transport == "grpc-async" %}await {% endif %}{{ method_invocation_text|trim }}
 
 # Handle the response
 {% if transport == "grpc-async" %}async {% endif %}for response in page_result:
@@ -272,7 +272,7 @@ page_result = {{ method_invocation_text|trim }}
     {{ dispatch_statement(statement)|trim }}
   {% endfor %}
 {% elif calling_form == calling_form_enum.RequestPaged %}
-page_result = {{ method_invocation_text|trim }}
+page_result = {% if transport == "grpc-async" %}await {% endif %}{{ method_invocation_text|trim }}
 
 # Handle the response
 {% if transport == "grpc-async" %}async {% endif %}for page in page_result.pages():

We would also need to ensure that the generated sample can be run. I think there is also an issue where asyncio.run(...) is missing in the sample code which is captured in https://github.com/googleapis/gapic-generator-python/issues/1795

import asyncio
...
asyncio.run(...)

parthea avatar Jan 22 '24 15:01 parthea

So would you like me to do the asyncio.run(...) changes first? Please suggest.

kitokirti avatar Jan 22 '24 16:01 kitokirti

Yes, you could try https://github.com/googleapis/gapic-generator-python/issues/1795 first

parthea avatar Jan 22 '24 17:01 parthea