GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: generate snippet metadata by busunkim96 · Pull Request #1129 · googleapis/gapic-generator-python

This repository was archived by the owner on Aug 25, 2026. It is now read-only.

Conversation

@busunkim96

@busunkim96 busunkim96 commented Jan 11, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Follow up to #1121.

Main changes:

  • When snippets are generated, snippet metadata is also generated.
  • If a method has a snippet, that snippet is included in the method docstring.

Other changes:

  • Removed the method docstring in the code snippet file (line right below the method definition) since the same text is already in the comment block at the top of the file.
  • Removed the concept of a "standalone" sample. All generated samples are expected to be standalone. When someone wants a smaller portion of the sample (e.g., request initialization only) they should fetch it from the file by looking up the line numbers in the snippet metadata file.

Other Notes:

  • It doesn't look like it's possible to do type annotations with _pb2 types, so those are annotated as Any. It is possible to do mypy checking with https://github.com/dropbox/mypy-protobuf, but I think it will be easier make that change in a separate PR.
  • There are a lot of golden file updates, this range of commits has most of the generator and test changes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
busunkim96 and others added 17 commits January 4, 2022 22:57
@busunkim96
feat: add generated snippets to docstrings
a623d6e
Use snippet metadata proto to add generated snippets into method
docstrings. Metadata generation is intentionally incomplete, the
fields that are filled out are a minimal set to enable snippets in
docstrings.

The proto itself will move elsewhere, but it does not yet have a
permanent home.
@busunkim96
Merge branch 'master' into snippets-in-docstrings
d356b12
@busunkim96
chore: delete extra proto file
89df318
@busunkim96
chore: fix generator.py
b50dd72
@busunkim96
test: fix generator tests, format
7465546
@busunkim96
Merge branch 'master' into snippets-in-docstrings
237f076
@busunkim96
fix: adjust type annotation for _generate_samples_and_manifest
8e07b88
@busunkim96
Merge branch 'snippets-in-docstrings' of github.com:googleapis/gapic-…
872c156
…generator-python into snippets-in-docstrings

t push
@busunkim96
chore: update goldens
e176b02
@busunkim96
fix: stabilize metadata json
8315803
@busunkim96
chore: regen metadata files
c58c5ae
@busunkim96
fix: use OrderedDict
906be9c
@busunkim96
fix:sort snippets by region tag
767a0ae
@busunkim96
Merge branch 'master' into snippets-in-docstrings
2d1e738
@busunkim96
chore: format
823ad2f
@busunkim96
Merge branch 'snippets-in-docstrings' of github.com:googleapis/gapic-…
0ada00e
…generator-python into snippets-in-docstrings
@busunkim96
fix: remove unnecessary conversion to dict
1e3defc
@busunkim96
busunkim96 marked this pull request as ready for review January 13, 2022 18:08
@busunkim96
busunkim96 requested review from a team, amanda-tarafa and software-dov January 13, 2022 18:08
software-dov
software-dov approved these changes Jan 13, 2022
View reviewed changes

@software-dov software-dov left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just a few inline questions, no need to block submission.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread gapic/generator/generator.py Outdated
# output_files[manifest_fname] = CodeGeneratorResponse.File(
# content=manifest_doc.render(), name=manifest_fname
# )
if len(index.metadata_index.snippets) > 0:

@software-dov software-dov Jan 13, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

if index.metadata_index.snippets:

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

busunkim96 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread gapic/generator/generator.py Outdated
# NOTE(busunkim): Not all fields are yet populated in the snippet metadata.
# Expected filename: snippet_metadata_{metadata_schema_version}_{apishortname}_{apiversion}.json
snippet_metadata_path = str(pathlib.Path(
out_dir) / f"snippet_metadata_v1_{api_schema.naming.name}_{api_schema.naming.version}.json").lower()

@software-dov software-dov Jan 13, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify: the v1 here is about the snippet metadata itself, correct?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@busunkim96 busunkim96 Jan 14, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Amanda left a note below saying the metadata will not be versioned for now though, so I'll go ahead and remove it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread gapic/generator/generator.py Outdated
Comment on lines +212 to +213
# NOTE(busunkim): Not all fields are yet populated in the snippet metadata.
# Expected filename: snippet_metadata_{metadata_schema_version}_{apishortname}_{apiversion}.json

@software-dov software-dov Jan 13, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment still relevant, or is it cruft?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@busunkim96 busunkim96 Jan 14, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep still relevant, there's a lot of fields that are not yet filled out.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread gapic/generator/generator.py
opts: Options,
api_schema=api.API,
**context: Mapping,
api_schema: api.API,

@software-dov software-dov Jan 13, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh! Good catch.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread gapic/samplegen/samplegen.py Outdated
Comment on lines 925 to 926
raise types.InvalidConfig(
"Sample config is invalid", valid)

@software-dov software-dov Jan 13, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outside the scope of the review, but taking a second look, this isn't very helpful. Do you think it would be better to include the config or the filepath tothe config here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@busunkim96 busunkim96 Jan 14, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified the error message to look like this:

                if not valid:
                    raise types.InvalidConfig(
                        "Sample config in '{}' is invalid\n\n{}".format(config_fpath, cfg), valid)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread gapic/samplegen/samplegen.py


def generate_sample(sample, api_schema, sample_template: jinja2.Template) -> str:
def generate_sample(sample, api_schema, sample_template: jinja2.Template) -> Tuple[str, Any]:

@software-dov software-dov Jan 13, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use Any as the type of the second second element instead of the real type? Is it related to the # type: ignore a few lines down?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@busunkim96 busunkim96 Jan 14, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, since the type is ignored I cannot use it in an annotation. I learned about mypy-protobuf from a PR with Tres though, so I'll open a separate PR to start type checking all the protobuf types.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 Outdated
{% endif %}
r"""{{ method.meta.doc|rst(width=72, indent=8) }}

.. code-block::

@software-dov software-dov Jan 13, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the .. code-block:: be inside the if statement a few lines below?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@busunkim96 busunkim96 Jan 14, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh good catch, this would have created a lot of noise in the regen PRs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/async_client.py Outdated


.. code-block::
from google.cloud import asset_v1

@software-dov software-dov Jan 13, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indentation is a little unfortunate, but we can probably ignore it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@busunkim96 busunkim96 Jan 14, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrrm I didn't notice this earlier, I'll try poking at the template to see where this is coming from.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
amanda-tarafa
amanda-tarafa approved these changes Jan 14, 2022
View reviewed changes

@amanda-tarafa amanda-tarafa left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a couple of comments on the generated metadata file, but it looks good!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/integration/goldens/asset/samples/generated_samples/snippet_metadata_v1_asset_v1.json
@@ -0,0 +1,1048 @@
{

@amanda-tarafa amanda-tarafa Jan 14, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the time beeing, we dropped versioning of the metadata scheam so you can drop the first v1 here.
Also, we are doing, where possible, snippet_metadata_{protopackage}.json, although the real important part is snippet_metadata*.json as the rest of the name is not going to be used to identify anything automatically.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/integration/goldens/asset/samples/generated_samples/snippet_metadata_v1_asset_v1.json Outdated
"clientMethod": {
"async": true,
"method": {
"fullName": "AnalyzeIamPolicyLongrunning",

@amanda-tarafa amanda-tarafa Jan 14, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fullName shuld be the RPC method name qualified by the proto package and the service name
This looks more like shortName.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@busunkim96 busunkim96 Jan 15, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, I'll switch this out for shortName.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/integration/goldens/asset/samples/generated_samples/snippet_metadata_v1_asset_v1.json
"method": {
"fullName": "AnalyzeIamPolicyLongrunning",
"service": {
"shortName": "AssetService"

@amanda-tarafa amanda-tarafa Jan 14, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And fullName would be the service name qualified by the proto package.
Fine if this is one the ones you are adding later.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/integration/goldens/asset/samples/generated_samples/snippet_metadata_v1_asset_v1.json Outdated
}
}
},
"file": "samples/generated_samples/cloudasset_generated_asset_v1_asset_service_analyze_iam_policy_longrunning_async.py",

@amanda-tarafa amanda-tarafa Jan 14, 2022

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path should be relative to the location of this metadata file. Just double checking that's the case.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@busunkim96 busunkim96 Jan 15, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it's currently relative to the library root. I'll remove the samples/generated_samples since the metadata sits in the same directory as the snippets.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
busunkim96 added 2 commits January 14, 2022 22:04
@busunkim96
chore: remove unused imports
11d84b4
@busunkim96
fix: fix left alignment issue in docstrings
446591a
software-dov
software-dov reviewed Jan 18, 2022
View reviewed changes
Comment thread gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 Resolved

Uh oh!

There was an error while loading. Please reload this page.

busunkim96 added 3 commits January 18, 2022 16:50
@busunkim96
fix: fix file path in snippet metadata
f710bb7
@busunkim96
fix: use method shortName field
d6f2490
@busunkim96
Merge branch 'master' into snippets-in-docstrings
e358e25
busunkim96 added 2 commits January 18, 2022 17:31
@busunkim96
chore: update goldens
02f1258
@busunkim96
chore: fix code-block syntax
c7a41c5
@busunkim96
busunkim96 merged commit 9e46031 into master Jan 18, 2022
@busunkim96
busunkim96 deleted the snippets-in-docstrings branch January 18, 2022 18:39
This was referenced Jan 18, 2022
chore(master): release reverted #1133
Merged
chore(master): release 0.40.0 #1138
Closed
chore(master): release 0.60.0 #1139
Merged
chore(master): release 0.39.1 #1146
Closed
This was referenced Jan 26, 2022
chore(main): release 0.39.1 #1160
Closed
chore(main): release 0.39.1 #1162
Closed
chore(main): release 0.61.0 #1163
Merged
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

2 more reviewers
@software-dov software-dov software-dov approved these changes
@amanda-tarafa amanda-tarafa amanda-tarafa approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@busunkim96 @software-dov @amanda-tarafa
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.