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 python 312 (ads templates) #1861

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends '_base.py.j2' %}
{% block content %}

__version__ = "0.0.0"
{% endblock %}
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ from collections import OrderedDict
import os
import re
from typing import Callable, Dict, Mapping, MutableMapping, MutableSequence, Optional, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union, cast
import pkg_resources
{% if service.any_deprecated %}
import warnings
{% endif %}
@@ -21,6 +20,9 @@ from google.auth.transport.grpc import SslCredentials # type: ignore
from google.auth.exceptions import MutualTLSChannelError # type: ignore
from google.oauth2 import service_account # type: ignore

{% set package_path = api.naming.module_namespace|join('.') + "." + api.naming.versioned_module_name %}
from {{package_path}} import gapic_version as package_version

try:
OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault]
except AttributeError: # pragma: NO COVER
@@ -806,14 +808,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
{% endif %}


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
"{{ api.naming.warehouse_package_name }}",
).version,
)
except pkg_resources.DistributionNotFound:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo()
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(gapic_version=package_version.__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 looks like this is grabbing the version that's defined in the above gapic_version file, is that correct?

The implementation is actually to access the version of the Ads client library itself, see the logic here

If you look at go/ads-client-libs-user-agent-design you can see that we opted to use the gccl user agent k/v for our own logging purposes, which is why the gapic_version keyword arg is used here instead of something else.

Is it possible to retrieve the client library version without pkg_resources? If not, then we might need to add that logic in our interceptors before this change can be made.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like this is grabbing the version that's defined in the above gapic_version file, is that correct?

Yes, that is correct. In google-cloud-python, we use release-please to update the version in gapic_version.py prior to releasing. As an example see googleapis/google-cloud-python#12010.

The implementation is actually to access the version of the Ads client library itself, see the logic here

We've moved away from this in GAPIC, and chose to populate gapic_version.py instead of a hardcoded version number in setup.py. Customers can obtain the version number programatically using __version__.

Is it possible to retrieve the client library version without pkg_resources? If not, then we might need to add that logic in our interceptors before this change can be made.

It is possible to use something else like https://docs.python.org/3/library/importlib.metadata.html but we chose to have a version module instead.



__all__ = (
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@

import abc
from typing import Awaitable, Callable, Dict, Optional, Sequence, Union
import pkg_resources

import google.auth # type: ignore
import google.api_core # type: ignore
@@ -42,14 +41,10 @@ from google.longrunning import operations_pb2 # type: ignore
{% endif %}
{% endfilter %}

try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
gapic_version=pkg_resources.get_distribution(
'{{ api.naming.warehouse_package_name }}',
).version,
)
except pkg_resources.DistributionNotFound:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo()
{% set package_path = api.naming.module_namespace|join('.') + "." + api.naming.versioned_module_name %}
from {{package_path}} import gapic_version as package_version

DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(gapic_version=package_version.__version__)


class {{ service.name }}Transport(abc.ABC):
Original file line number Diff line number Diff line change
@@ -5,6 +5,11 @@
import importlib
import sys

{% set package_path = api.naming.module_namespace|join('.') + "." + api.naming.versioned_module_name %}
from {{package_path}} import gapic_version as package_version

__version__ = package_version.__version__


if sys.version_info < (3, 7):
raise ImportError('This module requires Python 3.7 or later.')
5 changes: 0 additions & 5 deletions gapic/ads-templates/.coveragerc.j2
Original file line number Diff line number Diff line change
@@ -11,11 +11,6 @@ exclude_lines =
pragma: NO COVER
# Ignore debug-only repr
def __repr__
# Ignore pkg_resources exceptions.
# This is added at the module level as a safeguard for if someone
# generates the code and tries to run it without pip installing. This
# makes it virtually impossible to test properly.
except pkg_resources.DistributionNotFound
# This is used to indicate a python version mismatch,
# which is not easily tested in unit tests.
raise ImportError
101 changes: 65 additions & 36 deletions gapic/ads-templates/setup.py.j2
Original file line number Diff line number Diff line change
@@ -2,55 +2,84 @@

{% block content %}

import os
import re

import setuptools # type: ignore

package_root = os.path.abspath(os.path.dirname(__file__))

setuptools.setup(
name='{{ api.naming.warehouse_package_name }}',
version='0.0.1',
{% if api.naming.namespace %}
packages=setuptools.PEP420PackageFinder.find(),
namespace_packages={{ api.naming.namespace_packages }},
{% else %}
packages=setuptools.find_packages(),
{% endif %}
platforms='Posix; MacOS X; Windows',
include_package_data=True,
install_requires=(
{# TODO(dovs): remove when 1.x deprecation is complete #}
{% if 'rest' in opts.transport %}
"google-api-core[grpc] >= 2.10.0, < 3.0.0dev",
{% else %}
"google-api-core[grpc] >= 1.28.0, < 3.0.0dev",
{% endif %}
"googleapis-common-protos >= 1.53.0",
"grpcio >= 1.10.0",
"proto-plus >= 1.19.4, <2.0.0dev",
"proto-plus >= 1.22.2, <2.0.0dev; python_version>='3.11'",
name = '{{ api.naming.warehouse_package_name }}'

{% set warehouse_description = api.naming.warehouse_package_name.replace('-',' ')|title %}
{% set package_path = api.naming.module_namespace|join('/') + "/" + api.naming.module_name + "/" + api.naming.version %}

description = "{{ warehouse_description }} API client library"

version = None

with open(os.path.join(package_root, '{{ package_path }}/gapic_version.py')) as fp:
version_candidates = re.findall(r"(?<=\")\d+.\d+.\d+(?=\")", fp.read())
assert (len(version_candidates) == 1)
version = version_candidates[0]

if version[0] == "0":
release_status = "Development Status :: 4 - Beta"
else:
release_status = "Development Status :: 5 - Production/Stable"

dependencies = [
"google-api-core[grpc] >= 2.10.0, < 3.0.0dev",
"googleapis-common-protos >= 1.53.0",
"grpcio >= 1.10.0",
"proto-plus >= 1.19.4, <2.0.0dev",
"proto-plus >= 1.22.2, <2.0.0dev; python_version>='3.11'",
{% if api.requires_package(('google', 'iam', 'v1')) %}
'grpc-google-iam-v1',
"grpc-google-iam-v1",
{% endif %}
),
python_requires='>=3.7',{# Lazy import requires module-level getattr #}
]

package_root = os.path.abspath(os.path.dirname(__file__))

packages = [
package
for package in setuptools.find_namespace_packages()
if package.startswith("{{ api.naming.namespace_packages|first }}")
]

setuptools.setup(
name=name,
version=version,
description=description,
author="Google LLC",
author_email="googleapis-packages@google.com",
license="Apache 2.0",
setup_requires=[
'libcst >= 0.2.5',
],
scripts=[
'scripts/fixup_{{ api.naming.versioned_module_name }}_keywords.py',
],
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Operating System :: OS Independent',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Topic :: Internet',
'Topic :: Software Development :: Libraries :: Python Modules',
release_status,
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Operating System :: OS Independent",
"Topic :: Internet",
],
platforms="Posix; MacOS X; Windows",
packages=packages,
python_requires=">=3.7",
install_requires=dependencies,
include_package_data=True,
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't use the generated setup.py file, so I'm not sure it makes sense to make these changes, and perhaps you could remove it from the templates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file is still needed so that we can run tests for the ads templates. We'll need to maintain it as long as we support ads templates.

zip_safe=False,
)
{% endblock %}
9 changes: 6 additions & 3 deletions gapic/templates/setup.py.j2
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

import io
import os
import re

import setuptools # type: ignore

@@ -16,10 +17,12 @@ name = '{{ api.naming.warehouse_package_name }}'

description = "{{ warehouse_description }} API client library"

version = {}
version = None

with open(os.path.join(package_root, '{{ package_path }}/gapic_version.py')) as fp:
exec(fp.read(), version)
version = version["__version__"]
version_candidates = re.findall(r"(?<=\")\d+.\d+.\d+(?=\")", fp.read())
assert (len(version_candidates) == 1)
version = version_candidates[0]

if version[0] == "0":
release_status = "Development Status :: 4 - Beta"
9 changes: 6 additions & 3 deletions tests/integration/goldens/asset/setup.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
#
import io
import os
import re

import setuptools # type: ignore

@@ -25,10 +26,12 @@

description = "Google Cloud Asset API client library"

version = {}
version = None

with open(os.path.join(package_root, 'google/cloud/asset/gapic_version.py')) as fp:
exec(fp.read(), version)
version = version["__version__"]
version_candidates = re.findall(r"(?<=\")\d+.\d+.\d+(?=\")", fp.read())
assert (len(version_candidates) == 1)
version = version_candidates[0]

if version[0] == "0":
release_status = "Development Status :: 4 - Beta"
9 changes: 6 additions & 3 deletions tests/integration/goldens/credentials/setup.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
#
import io
import os
import re

import setuptools # type: ignore

@@ -25,10 +26,12 @@

description = "Google Iam Credentials API client library"

version = {}
version = None

with open(os.path.join(package_root, 'google/iam/credentials/gapic_version.py')) as fp:
exec(fp.read(), version)
version = version["__version__"]
version_candidates = re.findall(r"(?<=\")\d+.\d+.\d+(?=\")", fp.read())
assert (len(version_candidates) == 1)
version = version_candidates[0]

if version[0] == "0":
release_status = "Development Status :: 4 - Beta"
9 changes: 6 additions & 3 deletions tests/integration/goldens/eventarc/setup.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
#
import io
import os
import re

import setuptools # type: ignore

@@ -25,10 +26,12 @@

description = "Google Cloud Eventarc API client library"

version = {}
version = None

with open(os.path.join(package_root, 'google/cloud/eventarc/gapic_version.py')) as fp:
exec(fp.read(), version)
version = version["__version__"]
version_candidates = re.findall(r"(?<=\")\d+.\d+.\d+(?=\")", fp.read())
assert (len(version_candidates) == 1)
version = version_candidates[0]

if version[0] == "0":
release_status = "Development Status :: 4 - Beta"
9 changes: 6 additions & 3 deletions tests/integration/goldens/logging/setup.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
#
import io
import os
import re

import setuptools # type: ignore

@@ -25,10 +26,12 @@

description = "Google Cloud Logging API client library"

version = {}
version = None

with open(os.path.join(package_root, 'google/cloud/logging/gapic_version.py')) as fp:
exec(fp.read(), version)
version = version["__version__"]
version_candidates = re.findall(r"(?<=\")\d+.\d+.\d+(?=\")", fp.read())
assert (len(version_candidates) == 1)
version = version_candidates[0]

if version[0] == "0":
release_status = "Development Status :: 4 - Beta"
9 changes: 6 additions & 3 deletions tests/integration/goldens/redis/setup.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
#
import io
import os
import re

import setuptools # type: ignore

@@ -25,10 +26,12 @@

description = "Google Cloud Redis API client library"

version = {}
version = None

with open(os.path.join(package_root, 'google/cloud/redis/gapic_version.py')) as fp:
exec(fp.read(), version)
version = version["__version__"]
version_candidates = re.findall(r"(?<=\")\d+.\d+.\d+(?=\")", fp.read())
assert (len(version_candidates) == 1)
version = version_candidates[0]

if version[0] == "0":
release_status = "Development Status :: 4 - Beta"