Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for reading google.api.api_version #1999

Merged
merged 37 commits into from
May 8, 2024

Conversation

parthea
Copy link
Contributor

@parthea parthea commented Mar 23, 2024

Towards b/330968465

@product-auto-label product-auto-label bot added the size: s Pull request size is small. label Mar 23, 2024
@parthea parthea force-pushed the add-support-for-reading-api-version branch from d002b39 to 950e89b Compare April 8, 2024 21:03
Copy link

conventional-commit-lint-gcf bot commented Apr 8, 2024

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Apr 8, 2024
@parthea parthea force-pushed the add-support-for-reading-api-version branch 2 times, most recently from bf409e6 to 816a2cf Compare April 10, 2024 14:10
gapic/templates/setup.py.j2 Outdated Show resolved Hide resolved
gapic/ads-templates/setup.py.j2 Outdated Show resolved Hide resolved
setup.py Outdated Show resolved Hide resolved
gapic/templates/testing/_default_constraints.j2 Outdated Show resolved Hide resolved
gapic/templates/testing/constraints-3.7.txt.j2 Outdated Show resolved Hide resolved
@parthea parthea force-pushed the add-support-for-reading-api-version branch 3 times, most recently from 5a57275 to c61464c Compare April 10, 2024 15:29
@parthea parthea force-pushed the add-support-for-reading-api-version branch from c61464c to 4e3138c Compare April 10, 2024 17:14
@parthea parthea added the owlbot:run Add this label to trigger the Owlbot post processor. label Apr 10, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Apr 10, 2024
Copy link
Contributor

@vchudnov-g vchudnov-g left a comment

Choose a reason for hiding this comment

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

Not a review; I know this is a draft at the moment. Just a fly-by comment.

@@ -388,6 +391,12 @@ class {{ service.async_client_name }}:
)
{% endif %}

{% if service.version %}
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good if we could call this code in the _client_macros.j2 file, so we reduce duplication. Maybe a separate macro call set_version_header(service.version), and the macro could take care of the conditional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 009fc71

@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: m Pull request size is medium. labels Apr 29, 2024
Comment on lines +202 to +203
api_core_major, api_core_minor = [int(part) for part in api_core_version.__version__.split(".")[0:2]]
if api_core_major > 2 or (api_core_major == 2 and api_core_minor >= 19):
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to guard our tests like this every time a new feature is dependent on a version of api-core which is not the minimum version. This will also add a lot of toil for future clean up.

Instead, we should decide a generalized way of doing this and also keep track of the things that we need to clean up.

I would suggest using a helper function as a decorator for this. Something like:

def is_supported_version(major_version, minor_version):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            api_core_major, api_core_minor = [int(part) for part in api_core_version.__version__.split(".")[0:2]]
            if api_core_major > major_version or (api_core_major == major_version and api_core_minor >= minor_version):
                return func(*args, **kwargs)
            else:
               print("Skipping test due to unsupported version ...")
        return wrapper
    return decorator

Let me know what you think!

Copy link
Contributor

Choose a reason for hiding this comment

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

A good idea. See my related comment about being able to just have a helper function to get the version parts. The decorator could be done as part of that work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I filed #2023 to clean up the function as a follow up task so we don't block this PR indefinitely with improvements

@@ -24,6 +25,7 @@ from google.api_core import retry_async as retries
from google.auth import credentials as ga_credentials # type: ignore
from google.oauth2 import service_account # type: ignore

{{ shared_macros.add_google_api_core_version_header_import(service) }}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just pass down service.version here? Same is true for add_api_version_header_to_metadata if that's the only attribute we need from service for these two macros.

Copy link
Contributor Author

@parthea parthea May 7, 2024

Choose a reason for hiding this comment

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

Fixed in f150e67 and 220f873

Copy link
Contributor

@vchudnov-g vchudnov-g left a comment

Choose a reason for hiding this comment

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

Looks fine. My comments are minor, except for the one about the intended symlink not yet being a symlink; it looks like a regular file (which means we have the same file duplicated).

except ImportError: # pragma: NO COVER
HAS_GOOGLE_API_CORE_VERSION_HEADER = False
#}
{% if service.version %}
Copy link
Contributor

Choose a reason for hiding this comment

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

This does not show up as a symlink; see comment below.

@@ -0,0 +1,69 @@
{#
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a regular file, not a symlink:

git ls-files -s  gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/_shared_macros.j2
100644 7094d3039af53ead4b844ba9952d7084f0526bd2 0       gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/_shared_macros.j2

See the meaning of Git modes. (Credit: SO answer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch and thanks for the link! Fixed in 7957ded

(py39) partheniou@partheniou-vm-3:~/git/gapic-generator-python$ git ls-files -s  gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/_shared_macros.j2
120000 6884f2def889f3f4f2a5b19b54e76a52c2826174 0       gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/_shared_macros.j2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I reverted this change in 542e4e1 and filed #2028 to investigate if symlinks can be used.

Copy link
Contributor

Choose a reason for hiding this comment

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

If the symlinks don't work (weird, they should), can you specify a template outside the current directory (something like {% include ../templates/foo/bar %}

If that also doesn't work, I would suggest keeping the duplicate files but adding a prominent header to the ads one saying that this is a copy of the one in the standard templates, intended to be a symlink. My rationale: we have duplicate code either way, and the macro file9s) makes them more organized....and it paves the way for the symlink once we can figure it out.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see. You reverted back to the duplicate file, like I said in that last paragraph. SG. Could you add a note that this is intended to be a symlink to the other file, and for now is a duplicate?

Comment on lines 34 to 35
{{ shared_macros.add_google_api_core_version_header_import(service) }}
try:
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need blank lines before and/or after?

Suggested change
{{ shared_macros.add_google_api_core_version_header_import(service) }}
try:
{{ shared_macros.add_google_api_core_version_header_import(service) }}
try:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I applied this change but reverted it in 16402e1 as it caused unnecessary blank lines to be added to the golden files as the import statements in shared_macros.add_google_api_core_version_header_import(service) are guarded by conditional logic

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. Hopefully the generated files are nicely formatted through all code paths.

tests/system/test_streams.py Outdated Show resolved Hide resolved
Comment on lines +202 to +203
api_core_major, api_core_minor = [int(part) for part in api_core_version.__version__.split(".")[0:2]]
if api_core_major > 2 or (api_core_major == 2 and api_core_minor >= 19):
Copy link
Contributor

Choose a reason for hiding this comment

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

A good idea. See my related comment about being able to just have a helper function to get the version parts. The decorator could be done as part of that work.

…ce/_shared_macros.j2

Co-authored-by: Victor Chudnovsky <vchudnov@google.com>
Copy link
Contributor

@vchudnov-g vchudnov-g left a comment

Choose a reason for hiding this comment

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

Just one suggestion to flag the intended symlink.

Thanks for working on this and responding to the review comments!

@@ -38,32 +38,32 @@
{% endwith %}{# method_settings #}
{% endmacro %}

{% macro add_google_api_core_version_header_import(service) %}
{% macro add_google_api_core_version_header_import(service_version) %}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a note at the top that this is intended to be a symlink to the other file, and for now is a duplicate (link to the issue you filed)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in a62b313

@parthea parthea merged commit b2486e5 into main May 8, 2024
67 checks passed
@parthea parthea deleted the add-support-for-reading-api-version branch May 8, 2024 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants