From 4cb80439db744a0e77fc8632780e67de7d93ad75 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 20:37:43 +0000 Subject: [PATCH 01/10] Revert "fix: add certain raw imports to RESERVED_NAMES (#824)" This reverts commit 04bd8aaf0fc2c2c0615105cab39dc33266b66775. --- gapic/utils/reserved_names.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/gapic/utils/reserved_names.py b/gapic/utils/reserved_names.py index 9104015d5c..9bf1c9a914 100644 --- a/gapic/utils/reserved_names.py +++ b/gapic/utils/reserved_names.py @@ -21,15 +21,7 @@ # They are explicitly allowed message, module, and field names. RESERVED_NAMES = frozenset( itertools.chain( - # We CANNOT make exceptions for keywords. keyword.kwlist, - # We make SOME exceptions for certain names that collide with builtins. set(dir(builtins)) - {"filter", "map", "id", "input", "property"}, - # This is a hand-maintained list of modules that are directly imported - # in templates, i.e. they are not added as dependencies to any type, - # the raw text is just there in the template. - # More can be added as collisions are discovered. - # See issue #819 for additional info. - {"auth", "credentials", "exceptions", "future", "options", "policy", "math"} ) ) From a675912e935c88b1b715e1d153004e7172e8f51f Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 20:46:08 +0000 Subject: [PATCH 02/10] fix: always import google-auth as 'google.auth' --- .../services/%service/transports/base.py.j2 | 4 +-- .../services/%service/transports/grpc.py.j2 | 6 ++--- .../%name_%version/%sub/test_%service.py.j2 | 12 ++++----- .../services/%service/transports/base.py.j2 | 8 +++--- .../services/%service/transports/grpc.py.j2 | 2 +- .../%service/transports/grpc_asyncio.py.j2 | 1 - .../services/%service/transports/rest.py.j2 | 1 - .../%name_%version/%sub/test_%service.py.j2 | 26 +++++++++---------- 8 files changed, 29 insertions(+), 31 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 index a6a0458a33..b3e51b4138 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 @@ -6,7 +6,7 @@ import abc import typing import pkg_resources -from google import auth +import google.auth from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore {% if service.has_lro %} @@ -70,7 +70,7 @@ class {{ service.name }}Transport(metaclass=abc.ABCMeta): # If no credentials are provided, then determine the appropriate # defaults. if credentials is None: - credentials, _ = auth.default(scopes=self.AUTH_SCOPES) + credentials, _ = google.auth.default(scopes=self.AUTH_SCOPES) # Save the credentials. self._credentials = credentials diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 index 381d003569..b8c809a13c 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 @@ -10,7 +10,7 @@ from google.api_core import grpc_helpers # type: ignore from google.api_core import operations_v1 # type: ignore {% endif %} from google.api_core import gapic_v1 # type: ignore -from google import auth # type: ignore +import google.auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore @@ -105,7 +105,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): host = api_mtls_endpoint if ":" in api_mtls_endpoint else api_mtls_endpoint + ":443" if credentials is None: - credentials, _ = auth.default(scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id) + credentials, _ = google.auth.default(scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id) # Create SSL credentials with client_cert_source or application # default SSL credentials. @@ -135,7 +135,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): host = host if ":" in host else host + ":443" if credentials is None: - credentials, _ = auth.default(scopes=self.AUTH_SCOPES) + credentials, _ = google.auth.default(scopes=self.AUTH_SCOPES) # create a new channel. The provided one is ignored. self._grpc_channel = type(self).create_channel( diff --git a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index e507bb573a..80f0c8b301 100644 --- a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -12,7 +12,7 @@ from proto.marshal.rules.dates import DurationRule, TimestampRule {# Import the service itself as well as every proto module that it imports. -#} {% filter sort_lines %} -from google import auth +import google.auth from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.oauth2 import service_account @@ -629,7 +629,7 @@ def test_transport_grpc_default(): ]) def test_transport_adc(transport_class): # Test default credentials are used if not provided. - with mock.patch.object(auth, 'default') as adc: + with mock.patch.object(google.auth, 'default') as adc: adc.return_value = (credentials.AnonymousCredentials(), None) transport_class() adc.assert_called_once() @@ -664,7 +664,7 @@ def test_{{ service.name|snake_case }}_base_transport(): def test_{{ service.name|snake_case }}_base_transport_with_adc(): # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(auth, 'default') as adc, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: + with mock.patch.object(google.auth, 'default') as adc, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: Transport.return_value = None adc.return_value = (credentials.AnonymousCredentials(), None) transport = transports.{{ service.name }}Transport() @@ -673,7 +673,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_adc(): def test_{{ service.name|snake_case }}_auth_adc(): # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(auth, 'default') as adc: + with mock.patch.object(google.auth, 'default') as adc: adc.return_value = (credentials.AnonymousCredentials(), None) {{ service.client_name }}() adc.assert_called_once_with(scopes=( @@ -686,7 +686,7 @@ def test_{{ service.name|snake_case }}_auth_adc(): def test_{{ service.name|snake_case }}_transport_auth_adc(): # If credentials and host are not provided, the transport class should use # ADC credentials. - with mock.patch.object(auth, 'default') as adc: + with mock.patch.object(google.auth, 'default') as adc: adc.return_value = (credentials.AnonymousCredentials(), None) transports.{{ service.name }}GrpcTransport(host="squid.clam.whelk") adc.assert_called_once_with(scopes=( @@ -743,7 +743,7 @@ def test_{{ service.name|snake_case }}_transport_channel_mtls_with_client_cert_s cred = credentials.AnonymousCredentials() with pytest.warns(DeprecationWarning): - with mock.patch.object(auth, 'default') as adc: + with mock.patch.object(google.auth, 'default') as adc: adc.return_value = (cred, None) transport = transport_class( host="squid.clam.whelk", diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 index fe1549742c..92abaa8b1d 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 @@ -7,7 +7,7 @@ from typing import Awaitable, Callable, Dict, Optional, Sequence, Union import packaging.version import pkg_resources -from google import auth # type: ignore +import google.auth # type: ignore import google.api_core # type: ignore from google.api_core import exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore @@ -39,7 +39,7 @@ except pkg_resources.DistributionNotFound: try: # google.auth.__version__ was added in 1.26.0 - _GOOGLE_AUTH_VERSION = auth.__version__ + _GOOGLE_AUTH_VERSION = google.auth.__version__ except AttributeError: try: # try pkg_resources if it is available _GOOGLE_AUTH_VERSION = pkg_resources.get_distribution("google-auth").version @@ -108,14 +108,14 @@ class {{ service.name }}Transport(abc.ABC): raise exceptions.DuplicateCredentialArgs("'credentials_file' and 'credentials' are mutually exclusive") if credentials_file is not None: - credentials, _ = auth.load_credentials_from_file( + credentials, _ = google.auth.load_credentials_from_file( credentials_file, **scopes_kwargs, quota_project_id=quota_project_id ) elif credentials is None: - credentials, _ = auth.default(**scopes_kwargs, quota_project_id=quota_project_id) + credentials, _ = google.auth.default(**scopes_kwargs, quota_project_id=quota_project_id) # Save the credentials. self._credentials = credentials diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 index 6f0866a988..0aa0eef087 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 @@ -10,7 +10,7 @@ from google.api_core import grpc_helpers # type: ignore from google.api_core import operations_v1 # type: ignore {% endif %} from google.api_core import gapic_v1 # type: ignore -from google import auth # type: ignore +import google.auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 index a518ec94b2..c491b98a46 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 @@ -10,7 +10,6 @@ from google.api_core import grpc_helpers_async # type: ignore {% if service.has_lro %} from google.api_core import operations_v1 # type: ignore {% endif %} -from google import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore import packaging.version diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index afe9985600..18e0197870 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -9,7 +9,6 @@ from typing import Callable, Dict, Optional, Sequence, Tuple from google.api_core import operations_v1 {% endif %} from google.api_core import gapic_v1 # type: ignore -from google import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 8c2042f9bc..a04ecc9812 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -19,7 +19,7 @@ from requests.sessions import Session {# Import the service itself as well as every proto module that it imports. #} {% filter sort_lines %} -from google import auth +import google.auth from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.oauth2 import service_account @@ -1414,7 +1414,7 @@ def test_transport_get_channel(): ]) def test_transport_adc(transport_class): # Test default credentials are used if not provided. - with mock.patch.object(auth, 'default') as adc: + with mock.patch.object(google.auth, 'default') as adc: adc.return_value = (credentials.AnonymousCredentials(), None) transport_class() adc.assert_called_once() @@ -1475,7 +1475,7 @@ def test_{{ service.name|snake_case }}_base_transport(): @requires_google_auth_gte_1_25_0 def test_{{ service.name|snake_case }}_base_transport_with_credentials_file(): # Instantiate the base transport with a credentials file - with mock.patch.object(auth, 'load_credentials_from_file', autospec=True) as load_creds, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: + with mock.patch.object(google.auth, 'load_credentials_from_file', autospec=True) as load_creds, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: Transport.return_value = None load_creds.return_value = (credentials.AnonymousCredentials(), None) transport = transports.{{ service.name }}Transport( @@ -1495,7 +1495,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_credentials_file(): @requires_google_auth_lt_1_25_0 def test_{{ service.name|snake_case }}_base_transport_with_credentials_file_old_google_auth(): # Instantiate the base transport with a credentials file - with mock.patch.object(auth, 'load_credentials_from_file', autospec=True) as load_creds, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: + with mock.patch.object(google.auth, 'load_credentials_from_file', autospec=True) as load_creds, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: Transport.return_value = None load_creds.return_value = (credentials.AnonymousCredentials(), None) transport = transports.{{ service.name }}Transport( @@ -1513,7 +1513,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_credentials_file_old_ def test_{{ service.name|snake_case }}_base_transport_with_adc(): # Test the default credentials are used if credentials and credentials_file are None. - with mock.patch.object(auth, 'default', autospec=True) as adc, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: + with mock.patch.object(google.auth, 'default', autospec=True) as adc, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: Transport.return_value = None adc.return_value = (credentials.AnonymousCredentials(), None) transport = transports.{{ service.name }}Transport() @@ -1523,7 +1523,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_adc(): @requires_google_auth_gte_1_25_0 def test_{{ service.name|snake_case }}_auth_adc(): # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(auth, 'default', autospec=True) as adc: + with mock.patch.object(google.auth, 'default', autospec=True) as adc: adc.return_value = (credentials.AnonymousCredentials(), None) {{ service.client_name }}() adc.assert_called_once_with( @@ -1539,7 +1539,7 @@ def test_{{ service.name|snake_case }}_auth_adc(): @requires_google_auth_lt_1_25_0 def test_{{ service.name|snake_case }}_auth_adc_old_google_auth(): # If no credentials are provided, we should use ADC credentials. - with mock.patch.object(auth, 'default', autospec=True) as adc: + with mock.patch.object(google.auth, 'default', autospec=True) as adc: adc.return_value = (credentials.AnonymousCredentials(), None) {{ service.client_name }}() adc.assert_called_once_with( @@ -1563,7 +1563,7 @@ def test_{{ service.name|snake_case }}_auth_adc_old_google_auth(): def test_{{ service.name|snake_case }}_transport_auth_adc(transport_class): # If credentials and host are not provided, the transport class should use # ADC credentials. - with mock.patch.object(auth, 'default', autospec=True) as adc: + with mock.patch.object(google.auth, 'default', autospec=True) as adc: adc.return_value = (credentials.AnonymousCredentials(), None) transport_class(quota_project_id="octopus", scopes=["1", "2"]) adc.assert_called_once_with( @@ -1587,7 +1587,7 @@ def test_{{ service.name|snake_case }}_transport_auth_adc(transport_class): def test_{{ service.name|snake_case }}_transport_auth_adc_old_google_auth(transport_class): # If credentials and host are not provided, the transport class should use # ADC credentials. - with mock.patch.object(auth, "default", autospec=True) as adc: + with mock.patch.object(google.auth, "default", autospec=True) as adc: adc.return_value = (credentials.AnonymousCredentials(), None) transport_class(quota_project_id="octopus") adc.assert_called_once_with(scopes=( @@ -1609,7 +1609,7 @@ def test_{{ service.name|snake_case }}_transport_auth_adc_old_google_auth(transp def test_{{ service.name|snake_case }}_transport_create_channel(transport_class, grpc_helpers): # If credentials and host are not provided, the transport class should use # ADC credentials. - with mock.patch.object(auth, "default", autospec=True) as adc, mock.patch.object( + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch.object( grpc_helpers, "create_channel", autospec=True ) as create_channel: creds = credentials.AnonymousCredentials() @@ -1651,7 +1651,7 @@ def test_{{ service.name|snake_case }}_transport_create_channel(transport_class, def test_{{ service.name|snake_case }}_transport_create_channel_old_api_core(transport_class, grpc_helpers): # If credentials and host are not provided, the transport class should use # ADC credentials. - with mock.patch.object(auth, "default", autospec=True) as adc, mock.patch.object( + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch.object( grpc_helpers, "create_channel", autospec=True ) as create_channel: creds = credentials.AnonymousCredentials() @@ -1688,7 +1688,7 @@ def test_{{ service.name|snake_case }}_transport_create_channel_old_api_core(tra def test_{{ service.name|snake_case }}_transport_create_channel_user_scopes(transport_class, grpc_helpers): # If credentials and host are not provided, the transport class should use # ADC credentials. - with mock.patch.object(auth, "default", autospec=True) as adc, mock.patch.object( + with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch.object( grpc_helpers, "create_channel", autospec=True ) as create_channel: creds = credentials.AnonymousCredentials() @@ -1833,7 +1833,7 @@ def test_{{ service.name|snake_case }}_transport_channel_mtls_with_client_cert_s cred = credentials.AnonymousCredentials() with pytest.warns(DeprecationWarning): - with mock.patch.object(auth, 'default') as adc: + with mock.patch.object(google.auth, 'default') as adc: adc.return_value = (cred, None) transport = transport_class( host="squid.clam.whelk", From 483d1c14ab065ad71c52ac98e293a2ccb2e1ad03 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 21:08:13 +0000 Subject: [PATCH 03/10] fix: resolve conflicts for policy and options --- gapic/schema/metadata.py | 10 ++- .../%sub/services/%service/async_client.py.j2 | 34 ++++---- .../%sub/services/%service/client.py.j2 | 34 ++++---- .../services/%service/transports/base.py.j2 | 18 ++--- .../services/%service/transports/grpc.py.j2 | 22 ++--- .../%service/transports/grpc_asyncio.py.j2 | 24 +++--- .../services/%service/transports/rest.py.j2 | 4 +- .../%name_%version/%sub/test_%service.py.j2 | 80 +++++++++---------- 8 files changed, 116 insertions(+), 110 deletions(-) diff --git a/gapic/schema/metadata.py b/gapic/schema/metadata.py index b1ae02228c..b63921762f 100644 --- a/gapic/schema/metadata.py +++ b/gapic/schema/metadata.py @@ -74,9 +74,16 @@ def __str__(self) -> str: """ # Most (but not all) types are in a module. if self.module: + module_name = self.module + + # This module is from a different proto package + # Most commonly happens for a common proto + # https://pypi.org/project/googleapis-common-protos/ + if not self.proto_package.startswith(self.api_naming.proto_package): + module_name = f'{self.module}_pb2' + # If collisions are registered and conflict with our module, # use the module alias instead. - module_name = self.module if self.module_alias: module_name = self.module_alias @@ -170,7 +177,6 @@ def python_import(self) -> imp.Import: return imp.Import( package=self.package, module=f'{self.module}_pb2', - alias=self.module_alias if self.module_alias else self.module, ) @property diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 index ec0eee6831..10070ce406 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 @@ -22,8 +22,8 @@ from google.oauth2 import service_account # type: ignore {% endfor %} {% endfor %} {% if opts.add_iam_methods %} -from google.iam.v1 import iam_policy_pb2 as iam_policy # type: ignore -from google.iam.v1 import policy_pb2 as policy # type: ignore +from google.iam.v1 import iam_policy_pb2 # type: ignore +from google.iam.v1 import policy_pb2 # type: ignore {% endif %} {% endfilter %} from .transports.base import {{ service.name }}Transport, DEFAULT_CLIENT_INFO @@ -320,16 +320,16 @@ class {{ service.async_client_name }}: {% if opts.add_iam_methods %} async def set_iam_policy( self, - request: iam_policy.SetIamPolicyRequest = None, + request: iam_policy_pb2.PolicySetIamPolicyRequest = None, *, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> policy.Policy: + ) -> policy_pb2.Policy: r"""Sets the IAM access control policy on the specified function. Replaces any existing policy. Args: - request (:class:`~.iam_policy.SetIamPolicyRequest`): + request (:class:`~.iam_policy_pb2.PolicySetIamPolicyRequest`): The request object. Request message for `SetIamPolicy` method. retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -338,7 +338,7 @@ class {{ service.async_client_name }}: metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: - ~.policy.Policy: + ~.policy_pb2.Policy: Defines an Identity and Access Management (IAM) policy. It is used to specify access control policies for Cloud Platform resources. @@ -402,7 +402,7 @@ class {{ service.async_client_name }}: # The request isn't a proto-plus wrapped type, # so it must be constructed via keyword expansion. if isinstance(request, dict): - request = iam_policy.SetIamPolicyRequest(**request) + request = iam_policy_pb2.PolicySetIamPolicyRequest(**request) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. @@ -426,17 +426,17 @@ class {{ service.async_client_name }}: async def get_iam_policy( self, - request: iam_policy.GetIamPolicyRequest = None, + request: iam_policy_pb2.PolicyGetIamPolicyRequest = None, *, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> policy.Policy: + ) -> policy_pb2.Policy: r"""Gets the IAM access control policy for a function. Returns an empty policy if the function exists and does not have a policy set. Args: - request (:class:`~.iam_policy.GetIamPolicyRequest`): + request (:class:`~.iam_policy_pb2.PolicyGetIamPolicyRequest`): The request object. Request message for `GetIamPolicy` method. retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -445,7 +445,7 @@ class {{ service.async_client_name }}: metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: - ~.policy.Policy: + ~.policy_pb2.Policy: Defines an Identity and Access Management (IAM) policy. It is used to specify access control policies for Cloud Platform resources. @@ -509,7 +509,7 @@ class {{ service.async_client_name }}: # The request isn't a proto-plus wrapped type, # so it must be constructed via keyword expansion. if isinstance(request, dict): - request = iam_policy.GetIamPolicyRequest(**request) + request = iam_policy_pb2.PolicyGetIamPolicyRequest(**request) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. @@ -533,17 +533,17 @@ class {{ service.async_client_name }}: async def test_iam_permissions( self, - request: iam_policy.TestIamPermissionsRequest = None, + request: iam_policy_pb2.PolicyTestIamPermissionsRequest = None, *, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> iam_policy.TestIamPermissionsResponse: + ) -> iam_policy_pb2.PolicyTestIamPermissionsResponse: r"""Tests the specified permissions against the IAM access control policy for a function. If the function does not exist, this will return an empty set of permissions, not a NOT_FOUND error. Args: - request (:class:`~.iam_policy.TestIamPermissionsRequest`): + request (:class:`~.iam_policy_pb2.PolicyTestIamPermissionsRequest`): The request object. Request message for `TestIamPermissions` method. retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -552,7 +552,7 @@ class {{ service.async_client_name }}: metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: - ~.iam_policy.TestIamPermissionsResponse: + ~.iam_policy_pb2.PolicyTestIamPermissionsResponse: Response message for ``TestIamPermissions`` method. """ # Create or coerce a protobuf request object. @@ -560,7 +560,7 @@ class {{ service.async_client_name }}: # The request isn't a proto-plus wrapped type, # so it must be constructed via keyword expansion. if isinstance(request, dict): - request = iam_policy.TestIamPermissionsRequest(**request) + request = iam_policy_pb2.PolicyTestIamPermissionsRequest(**request) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index 36b651309b..e9a22b2064 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -26,8 +26,8 @@ from google.oauth2 import service_account # type: ignore {% endfor %} {% endfor %} {% if opts.add_iam_methods %} -from google.iam.v1 import iam_policy_pb2 as iam_policy # type: ignore -from google.iam.v1 import policy_pb2 as policy # type: ignore +from google.iam.v1 import iam_policy_pb2 # type: ignore +from google.iam.v1 import policy_pb2 # type: ignore {% endif %} {% endfilter %} from .transports.base import {{ service.name }}Transport, DEFAULT_CLIENT_INFO @@ -459,16 +459,16 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): {% if opts.add_iam_methods %} def set_iam_policy( self, - request: iam_policy.SetIamPolicyRequest = None, + request: iam_policy_pb2.SetIamPolicyRequest = None, *, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> policy.Policy: + ) -> policy_pb2.Policy: r"""Sets the IAM access control policy on the specified function. Replaces any existing policy. Args: - request (:class:`~.iam_policy.SetIamPolicyRequest`): + request (:class:`~.iam_policy_pb2.SetIamPolicyRequest`): The request object. Request message for `SetIamPolicy` method. retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -477,7 +477,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: - ~.policy.Policy: + ~.policy_pb2.Policy: Defines an Identity and Access Management (IAM) policy. It is used to specify access control policies for Cloud Platform resources. @@ -541,7 +541,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): # The request isn't a proto-plus wrapped type, # so it must be constructed via keyword expansion. if isinstance(request, dict): - request = iam_policy.SetIamPolicyRequest(**request) + request = iam_policy_pb2.SetIamPolicyRequest(**request) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. @@ -565,17 +565,17 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): def get_iam_policy( self, - request: iam_policy.GetIamPolicyRequest = None, + request: iam_policy_pb2.GetIamPolicyRequest = None, *, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> policy.Policy: + ) -> policy_pb2.Policy: r"""Gets the IAM access control policy for a function. Returns an empty policy if the function exists and does not have a policy set. Args: - request (:class:`~.iam_policy.GetIamPolicyRequest`): + request (:class:`~.iam_policy_pb2.GetIamPolicyRequest`): The request object. Request message for `GetIamPolicy` method. retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -584,7 +584,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: - ~.policy.Policy: + ~.policy_pb2.Policy: Defines an Identity and Access Management (IAM) policy. It is used to specify access control policies for Cloud Platform resources. @@ -648,7 +648,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): # The request isn't a proto-plus wrapped type, # so it must be constructed via keyword expansion. if isinstance(request, dict): - request = iam_policy.GetIamPolicyRequest(**request) + request = iam_policy_pb2.GetIamPolicyRequest(**request) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. @@ -672,17 +672,17 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): def test_iam_permissions( self, - request: iam_policy.TestIamPermissionsRequest = None, + request: iam_policy_pb2.TestIamPermissionsRequest = None, *, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> iam_policy.TestIamPermissionsResponse: + ) -> iam_policy_pb2.TestIamPermissionsResponse: r"""Tests the specified permissions against the IAM access control policy for a function. If the function does not exist, this will return an empty set of permissions, not a NOT_FOUND error. Args: - request (:class:`~.iam_policy.TestIamPermissionsRequest`): + request (:class:`~.iam_policy_pb2.TestIamPermissionsRequest`): The request object. Request message for `TestIamPermissions` method. retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -691,7 +691,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: - ~.iam_policy.TestIamPermissionsResponse: + ~.iam_policy_pb2.TestIamPermissionsResponse: Response message for ``TestIamPermissions`` method. """ # Create or coerce a protobuf request object. @@ -699,7 +699,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): # The request isn't a proto-plus wrapped type, # so it must be constructed via keyword expansion. if isinstance(request, dict): - request = iam_policy.TestIamPermissionsRequest(**request) + request = iam_policy_pb2.TestIamPermissionsRequest(**request) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 index 92abaa8b1d..0a6c4d2b77 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 @@ -23,8 +23,8 @@ from google.auth import credentials # type: ignore {{ method.output.ident.python_import }} {% endfor %} {% if opts.add_iam_methods %} -from google.iam.v1 import iam_policy_pb2 as iam_policy # type: ignore -from google.iam.v1 import policy_pb2 as policy # type: ignore +from google.iam.v1 import iam_policy_pb2 # type: ignore +from google.iam.v1 import policy_pb2 # type: ignore {% endif %} {% endfilter %} @@ -214,8 +214,8 @@ class {{ service.name }}Transport(abc.ABC): def set_iam_policy( self, ) -> Callable[ - [iam_policy.SetIamPolicyRequest], - Union[policy.Policy, Awaitable[policy.Policy]], + [iam_policy_pb2.SetIamPolicyRequest], + Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]], ]: raise NotImplementedError() @@ -223,8 +223,8 @@ class {{ service.name }}Transport(abc.ABC): def get_iam_policy( self, ) -> Callable[ - [iam_policy.GetIamPolicyRequest], - Union[policy.Policy, Awaitable[policy.Policy]], + [iam_policy_pb2.GetIamPolicyRequest], + Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]], ]: raise NotImplementedError() @@ -232,10 +232,10 @@ class {{ service.name }}Transport(abc.ABC): def test_iam_permissions( self, ) -> Callable[ - [iam_policy.TestIamPermissionsRequest], + [iam_policy_pb2.TestIamPermissionsRequest], Union[ - iam_policy.TestIamPermissionsResponse, - Awaitable[iam_policy.TestIamPermissionsResponse], + iam_policy_pb2.TestIamPermissionsResponse, + Awaitable[iam_policy_pb2.TestIamPermissionsResponse], ], ]: raise NotImplementedError() diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 index 0aa0eef087..8ed6b1dafd 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 @@ -22,8 +22,8 @@ import grpc # type: ignore {{ method.output.ident.python_import }} {% endfor %} {% if opts.add_iam_methods %} -from google.iam.v1 import iam_policy_pb2 as iam_policy # type: ignore -from google.iam.v1 import policy_pb2 as policy # type: ignore +from google.iam.v1 import iam_policy_pb2 # type: ignore +from google.iam.v1 import policy_pb2 # type: ignore {% endif %} {% endfilter %} from .base import {{ service.name }}Transport, DEFAULT_CLIENT_INFO @@ -274,7 +274,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): @property def set_iam_policy( self, - ) -> Callable[[iam_policy.SetIamPolicyRequest], policy.Policy]: + ) -> Callable[[iam_policy_pb2.SetIamPolicyRequest], policy_pb2.Policy]: r"""Return a callable for the set iam policy method over gRPC. Sets the IAM access control policy on the specified function. Replaces any existing policy. @@ -291,15 +291,15 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): if "set_iam_policy" not in self._stubs: self._stubs["set_iam_policy"] = self.grpc_channel.unary_unary( "/google.iam.v1.IAMPolicy/SetIamPolicy", - request_serializer=iam_policy.SetIamPolicyRequest.SerializeToString, - response_deserializer=policy.Policy.FromString, + request_serializer=iam_policy_pb2.SetIamPolicyRequest.SerializeToString, + response_deserializer=policy_pb2.Policy.FromString, ) return self._stubs["set_iam_policy"] @property def get_iam_policy( self, - ) -> Callable[[iam_policy.GetIamPolicyRequest], policy.Policy]: + ) -> Callable[[iam_policy_pb2.GetIamPolicyRequest], policy_pb2.Policy]: r"""Return a callable for the get iam policy method over gRPC. Gets the IAM access control policy for a function. Returns an empty policy if the function exists and does @@ -317,8 +317,8 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): if "get_iam_policy" not in self._stubs: self._stubs["get_iam_policy"] = self.grpc_channel.unary_unary( "/google.iam.v1.IAMPolicy/GetIamPolicy", - request_serializer=iam_policy.GetIamPolicyRequest.SerializeToString, - response_deserializer=policy.Policy.FromString, + request_serializer=iam_policy_pb2.GetIamPolicyRequest.SerializeToString, + response_deserializer=policy_pb2.Policy.FromString, ) return self._stubs["get_iam_policy"] @@ -326,7 +326,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): def test_iam_permissions( self, ) -> Callable[ - [iam_policy.TestIamPermissionsRequest], iam_policy.TestIamPermissionsResponse + [iam_policy_pb2.TestIamPermissionsRequest], iam_policy_pb2.TestIamPermissionsResponse ]: r"""Return a callable for the test iam permissions method over gRPC. Tests the specified permissions against the IAM access control @@ -345,8 +345,8 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): if "test_iam_permissions" not in self._stubs: self._stubs["test_iam_permissions"] = self.grpc_channel.unary_unary( "/google.iam.v1.IAMPolicy/TestIamPermissions", - request_serializer=iam_policy.TestIamPermissionsRequest.SerializeToString, - response_deserializer=iam_policy.TestIamPermissionsResponse.FromString, + request_serializer=iam_policy_pb2.TestIamPermissionsRequest.SerializeToString, + response_deserializer=iam_policy_pb2.TestIamPermissionsResponse.FromString, ) return self._stubs["test_iam_permissions"] {% endif %} diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 index c491b98a46..9f5a8607f8 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 @@ -23,8 +23,8 @@ from grpc.experimental import aio # type: ignore {{ method.output.ident.python_import }} {% endfor %} {% if opts.add_iam_methods %} -from google.iam.v1 import iam_policy_pb2 as iam_policy # type: ignore -from google.iam.v1 import policy_pb2 as policy # type: ignore +from google.iam.v1 import iam_policy_pb2 # type: ignore +from google.iam.v1 import policy_pb2 # type: ignore {% endif %} {% endfilter %} from .base import {{ service.name }}Transport, DEFAULT_CLIENT_INFO @@ -277,7 +277,7 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport): @property def set_iam_policy( self, - ) -> Callable[[iam_policy.SetIamPolicyRequest], Awaitable[policy.Policy]]: + ) -> Callable[[iam_policy_pb2.SetIamPolicyRequest], Awaitable[policy_pb2.Policy]]: r"""Return a callable for the set iam policy method over gRPC. Sets the IAM access control policy on the specified function. Replaces any existing policy. @@ -294,15 +294,15 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport): if "set_iam_policy" not in self._stubs: self._stubs["set_iam_policy"] = self.grpc_channel.unary_unary( "/google.iam.v1.IAMPolicy/SetIamPolicy", - request_serializer=iam_policy.SetIamPolicyRequest.SerializeToString, - response_deserializer=policy.Policy.FromString, + request_serializer=iam_policy_pb2.SetIamPolicyRequest.SerializeToString, + response_deserializer=policy_pb2.Policy.FromString, ) return self._stubs["set_iam_policy"] @property def get_iam_policy( self, - ) -> Callable[[iam_policy.GetIamPolicyRequest], Awaitable[policy.Policy]]: + ) -> Callable[[iam_policy_pb2.GetIamPolicyRequest], Awaitable[policy_pb2.Policy]]: r"""Return a callable for the get iam policy method over gRPC. Gets the IAM access control policy for a function. Returns an empty policy if the function exists and does @@ -320,8 +320,8 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport): if "get_iam_policy" not in self._stubs: self._stubs["get_iam_policy"] = self.grpc_channel.unary_unary( "/google.iam.v1.IAMPolicy/GetIamPolicy", - request_serializer=iam_policy.GetIamPolicyRequest.SerializeToString, - response_deserializer=policy.Policy.FromString, + request_serializer=iam_policy_pb2.GetIamPolicyRequest.SerializeToString, + response_deserializer=policy_pb2.Policy.FromString, ) return self._stubs["get_iam_policy"] @@ -329,8 +329,8 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport): def test_iam_permissions( self, ) -> Callable[ - [iam_policy.TestIamPermissionsRequest], - Awaitable[iam_policy.TestIamPermissionsResponse], + [iam_policy_pb2.TestIamPermissionsRequest], + Awaitable[iam_policy_pb2.TestIamPermissionsResponse], ]: r"""Return a callable for the test iam permissions method over gRPC. Tests the specified permissions against the IAM access control @@ -349,8 +349,8 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport): if "test_iam_permissions" not in self._stubs: self._stubs["test_iam_permissions"] = self.grpc_channel.unary_unary( "/google.iam.v1.IAMPolicy/TestIamPermissions", - request_serializer=iam_policy.TestIamPermissionsRequest.SerializeToString, - response_deserializer=iam_policy.TestIamPermissionsResponse.FromString, + request_serializer=iam_policy_pb2.TestIamPermissionsRequest.SerializeToString, + response_deserializer=iam_policy_pb2.TestIamPermissionsResponse.FromString, ) return self._stubs["test_iam_permissions"] {% endif %} diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index 18e0197870..18a03f722a 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -23,8 +23,8 @@ from google.auth.transport.requests import AuthorizedSession {{ method.output.ident.python_import }} {% endfor %} {% if opts.add_iam_methods %} -from google.iam.v1 import iam_policy_pb2 as iam_policy # type: ignore -from google.iam.v1 import policy_pb2 as policy # type: ignore +from google.iam.v1 import iam_policy_pb2 # type: ignore +from google.iam.v1 import policy_pb2 # type: ignore {% endif %} {% endfilter %} diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index a04ecc9812..efdd3dd105 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -48,8 +48,8 @@ from google.api_core import gapic_v1 {% endfor %} {% endfor %} {% if opts.add_iam_methods %} -from google.iam.v1 import iam_policy_pb2 as iam_policy # type: ignore -from google.iam.v1 import options_pb2 as options # type: ignore +from google.iam.v1 import iam_policy_pb2 # type: ignore +from google.iam.v1 import options_pb2 # type: ignore from google.iam.v1 import policy_pb2 as policy # type: ignore {% endif %} {% endfilter %} @@ -2023,12 +2023,12 @@ def test_set_iam_policy(transport: str = "grpc"): # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = iam_policy.SetIamPolicyRequest() + request = iam_policy_pb2.SetIamPolicyRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.set_iam_policy), "__call__") as call: # Designate an appropriate return value for the call. - call.return_value = policy.Policy(version=774, etag=b"etag_blob",) + call.return_value = policy_pb2.Policy(version=774, etag=b"etag_blob",) response = client.set_iam_policy(request) @@ -2039,7 +2039,7 @@ def test_set_iam_policy(transport: str = "grpc"): assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, policy.Policy) + assert isinstance(response, policy_pb2.Policy) assert response.version == 774 @@ -2054,7 +2054,7 @@ async def test_set_iam_policy_async(transport: str = "grpc_asyncio"): # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = iam_policy.SetIamPolicyRequest() + request = iam_policy_pb2.SetIamPolicyRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -2062,7 +2062,7 @@ async def test_set_iam_policy_async(transport: str = "grpc_asyncio"): ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - policy.Policy(version=774, etag=b"etag_blob",) + policy_pb2.Policy(version=774, etag=b"etag_blob",) ) response = await client.set_iam_policy(request) @@ -2074,7 +2074,7 @@ async def test_set_iam_policy_async(transport: str = "grpc_asyncio"): assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, policy.Policy) + assert isinstance(response, policy_pb2.Policy) assert response.version == 774 @@ -2088,12 +2088,12 @@ def test_set_iam_policy_field_headers(): # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. - request = iam_policy.SetIamPolicyRequest() + request = iam_policy_pb2.SetIamPolicyRequest() request.resource = "resource/value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.set_iam_policy), "__call__") as call: - call.return_value = policy.Policy() + call.return_value = policy_pb2.Policy() client.set_iam_policy(request) @@ -2115,14 +2115,14 @@ async def test_set_iam_policy_field_headers_async(): # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. - request = iam_policy.SetIamPolicyRequest() + request = iam_policy_pb2.SetIamPolicyRequest() request.resource = "resource/value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( type(client.transport.set_iam_policy), "__call__" ) as call: - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(policy.Policy()) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(policy_pb2.Policy()) await client.set_iam_policy(request) @@ -2143,12 +2143,12 @@ def test_set_iam_policy_from_dict(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.set_iam_policy), "__call__") as call: # Designate an appropriate return value for the call. - call.return_value = policy.Policy() + call.return_value = policy_pb2.Policy() response = client.set_iam_policy( request={ "resource": "resource_value", - "policy": policy.Policy(version=774), + "policy": policy_pb2.Policy(version=774), } ) call.assert_called() @@ -2163,13 +2163,13 @@ async def test_set_iam_policy_from_dict_async(): with mock.patch.object(type(client.transport.set_iam_policy), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - policy.Policy() + policy_pb2.Policy() ) response = await client.set_iam_policy( request={ "resource": "resource_value", - "policy": policy.Policy(version=774), + "policy": policy_pb2.Policy(version=774), } ) call.assert_called() @@ -2182,12 +2182,12 @@ def test_get_iam_policy(transport: str = "grpc"): # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = iam_policy.GetIamPolicyRequest() + request = iam_policy_pb2.GetIamPolicyRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.get_iam_policy), "__call__") as call: # Designate an appropriate return value for the call. - call.return_value = policy.Policy(version=774, etag=b"etag_blob",) + call.return_value = policy_pb2.Policy(version=774, etag=b"etag_blob",) response = client.get_iam_policy(request) @@ -2198,7 +2198,7 @@ def test_get_iam_policy(transport: str = "grpc"): assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, policy.Policy) + assert isinstance(response, policy_pb2.Policy) assert response.version == 774 @@ -2213,7 +2213,7 @@ async def test_get_iam_policy_async(transport: str = "grpc_asyncio"): # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = iam_policy.GetIamPolicyRequest() + request = iam_policy_pb2.GetIamPolicyRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -2221,7 +2221,7 @@ async def test_get_iam_policy_async(transport: str = "grpc_asyncio"): ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - policy.Policy(version=774, etag=b"etag_blob",) + policy_pb2.Policy(version=774, etag=b"etag_blob",) ) response = await client.get_iam_policy(request) @@ -2233,7 +2233,7 @@ async def test_get_iam_policy_async(transport: str = "grpc_asyncio"): assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, policy.Policy) + assert isinstance(response, policy_pb2.Policy) assert response.version == 774 @@ -2247,12 +2247,12 @@ def test_get_iam_policy_field_headers(): # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. - request = iam_policy.GetIamPolicyRequest() + request = iam_policy_pb2.GetIamPolicyRequest() request.resource = "resource/value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.get_iam_policy), "__call__") as call: - call.return_value = policy.Policy() + call.return_value = policy_pb2.Policy() client.get_iam_policy(request) @@ -2274,14 +2274,14 @@ async def test_get_iam_policy_field_headers_async(): # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. - request = iam_policy.GetIamPolicyRequest() + request = iam_policy_pb2.GetIamPolicyRequest() request.resource = "resource/value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( type(client.transport.get_iam_policy), "__call__" ) as call: - call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(policy.Policy()) + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(policy_pb2.Policy()) await client.get_iam_policy(request) @@ -2302,7 +2302,7 @@ def test_get_iam_policy_from_dict(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.get_iam_policy), "__call__") as call: # Designate an appropriate return value for the call. - call.return_value = policy.Policy() + call.return_value = policy_pb2.Policy() response = client.get_iam_policy( request={ @@ -2321,7 +2321,7 @@ async def test_get_iam_policy_from_dict_async(): with mock.patch.object(type(client.transport.get_iam_policy), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - policy.Policy() + policy_pb2.Policy() ) response = await client.get_iam_policy( @@ -2340,14 +2340,14 @@ def test_test_iam_permissions(transport: str = "grpc"): # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = iam_policy.TestIamPermissionsRequest() + request = iam_policy_pb2.TestIamPermissionsRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( type(client.transport.test_iam_permissions), "__call__" ) as call: # Designate an appropriate return value for the call. - call.return_value = iam_policy.TestIamPermissionsResponse( + call.return_value = iam_policy_pb2.TestIamPermissionsResponse( permissions=["permissions_value"], ) @@ -2360,7 +2360,7 @@ def test_test_iam_permissions(transport: str = "grpc"): assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, iam_policy.TestIamPermissionsResponse) + assert isinstance(response, iam_policy_pb2.TestIamPermissionsResponse) assert response.permissions == ["permissions_value"] @@ -2373,7 +2373,7 @@ async def test_test_iam_permissions_async(transport: str = "grpc_asyncio"): # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = iam_policy.TestIamPermissionsRequest() + request = iam_policy_pb2.TestIamPermissionsRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -2381,7 +2381,7 @@ async def test_test_iam_permissions_async(transport: str = "grpc_asyncio"): ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - iam_policy.TestIamPermissionsResponse(permissions=["permissions_value"],) + iam_policy_pb2.TestIamPermissionsResponse(permissions=["permissions_value"],) ) response = await client.test_iam_permissions(request) @@ -2393,7 +2393,7 @@ async def test_test_iam_permissions_async(transport: str = "grpc_asyncio"): assert args[0] == request # Establish that the response is the type that we expect. - assert isinstance(response, iam_policy.TestIamPermissionsResponse) + assert isinstance(response, iam_policy_pb2.TestIamPermissionsResponse) assert response.permissions == ["permissions_value"] @@ -2405,14 +2405,14 @@ def test_test_iam_permissions_field_headers(): # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. - request = iam_policy.TestIamPermissionsRequest() + request = iam_policy_pb2.TestIamPermissionsRequest() request.resource = "resource/value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( type(client.transport.test_iam_permissions), "__call__" ) as call: - call.return_value = iam_policy.TestIamPermissionsResponse() + call.return_value = iam_policy_pb2.TestIamPermissionsResponse() client.test_iam_permissions(request) @@ -2434,7 +2434,7 @@ async def test_test_iam_permissions_field_headers_async(): # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. - request = iam_policy.TestIamPermissionsRequest() + request = iam_policy_pb2.TestIamPermissionsRequest() request.resource = "resource/value" # Mock the actual call within the gRPC stub, and fake the request. @@ -2442,7 +2442,7 @@ async def test_test_iam_permissions_field_headers_async(): type(client.transport.test_iam_permissions), "__call__" ) as call: call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - iam_policy.TestIamPermissionsResponse() + iam_policy_pb2.TestIamPermissionsResponse() ) await client.test_iam_permissions(request) @@ -2466,7 +2466,7 @@ def test_test_iam_permissions_from_dict(): type(client.transport.test_iam_permissions), "__call__" ) as call: # Designate an appropriate return value for the call. - call.return_value = iam_policy.TestIamPermissionsResponse() + call.return_value = iam_policy_pb2.TestIamPermissionsResponse() response = client.test_iam_permissions( request={ @@ -2487,7 +2487,7 @@ async def test_test_iam_permissions_from_dict_async(): ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( - iam_policy.TestIamPermissionsResponse() + iam_policy_pb2.TestIamPermissionsResponse() ) response = await client.test_iam_permissions( From ee12e1574c2083aaf86343bdee7a22414d717dd1 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 22:00:43 +0000 Subject: [PATCH 04/10] fix: remove collision for credentials --- .../%sub/services/%service/client.py.j2 | 4 +- .../services/%service/transports/base.py.j2 | 4 +- .../services/%service/transports/grpc.py.j2 | 6 +- gapic/ads-templates/examples/sample.py.j2 | 2 +- .../%name_%version/%sub/test_%service.py.j2 | 54 +++---- .../%sub/services/%service/async_client.py.j2 | 4 +- .../%sub/services/%service/client.py.j2 | 4 +- .../services/%service/transports/base.py.j2 | 4 +- .../services/%service/transports/grpc.py.j2 | 6 +- .../%service/transports/grpc_asyncio.py.j2 | 6 +- .../services/%service/transports/rest.py.j2 | 4 +- gapic/templates/examples/sample.py.j2 | 2 +- .../%name_%version/%sub/test_%service.py.j2 | 140 +++++++++--------- 13 files changed, 120 insertions(+), 120 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 index 9e48007d26..7327bf0c99 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 @@ -13,7 +13,7 @@ from google.api_core import client_options as client_options_lib # type: ignore from google.api_core import exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore -from google.auth import credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore from google.auth.exceptions import MutualTLSChannelError # type: ignore @@ -174,7 +174,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): {% endfor %} {# common resources #} def __init__(self, *, - credentials: Optional[credentials.Credentials] = None, + credentials: Optional[ga_credentials.Credentials] = None, transport: Union[str, {{ service.name }}Transport, None] = None, client_options: Optional[client_options_lib.ClientOptions] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 index b3e51b4138..1795b3e3bf 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 @@ -12,7 +12,7 @@ from google.api_core import retry as retries # type: ignore {% if service.has_lro %} from google.api_core import operations_v1 # type: ignore {% endif %} -from google.auth import credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore {% filter sort_lines %} {% for method in service.methods.values() %} @@ -43,7 +43,7 @@ class {{ service.name }}Transport(metaclass=abc.ABCMeta): def __init__( self, *, host: str{% if service.host %} = '{{ service.host }}'{% endif %}, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the transport. diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 index b8c809a13c..7d1d931c98 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 @@ -11,7 +11,7 @@ from google.api_core import operations_v1 # type: ignore {% endif %} from google.api_core import gapic_v1 # type: ignore import google.auth # type: ignore -from google.auth import credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore import grpc # type: ignore @@ -39,7 +39,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): """ def __init__(self, *, host: str{% if service.host %} = '{{ service.host }}'{% endif %}, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, credentials_file: str = None, scopes: Sequence[str] = None, channel: grpc.Channel = None, @@ -162,7 +162,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): @classmethod def create_channel(cls, host: str{% if service.host %} = '{{ service.host }}'{% endif %}, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, scopes: Optional[Sequence[str]] = None, **kwargs) -> grpc.Channel: """Create and return a gRPC channel object. diff --git a/gapic/ads-templates/examples/sample.py.j2 b/gapic/ads-templates/examples/sample.py.j2 index 05ff8e1ee2..9c2ff58c84 100644 --- a/gapic/ads-templates/examples/sample.py.j2 +++ b/gapic/ads-templates/examples/sample.py.j2 @@ -34,7 +34,7 @@ def sample_{{ frags.render_method_name(sample.rpc)|trim }}({{ frags.print_input_ """{{ sample.description }}""" client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport="grpc", ) diff --git a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 80f0c8b301..159552914d 100644 --- a/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -13,7 +13,7 @@ from proto.marshal.rules.dates import DurationRule, TimestampRule {# Import the service itself as well as every proto module that it imports. -#} {% filter sort_lines %} import google.auth -from google.auth import credentials +from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.oauth2 import service_account from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }} import {{ service.client_name }} @@ -63,7 +63,7 @@ def test__get_default_mtls_endpoint(): def test_{{ service.client_name|snake_case }}_from_service_account_info(): - creds = credentials.AnonymousCredentials() + creds = ga_credentials.AnonymousCredentials() with mock.patch.object(service_account.Credentials, 'from_service_account_info') as factory: factory.return_value = creds info = {"valid": True} @@ -76,7 +76,7 @@ def test_{{ service.client_name|snake_case }}_from_service_account_info(): def test_{{ service.client_name|snake_case }}_from_service_account_file(): - creds = credentials.AnonymousCredentials() + creds = ga_credentials.AnonymousCredentials() with mock.patch.object(service_account.Credentials, 'from_service_account_file') as factory: factory.return_value = creds client = {{ service.client_name }}.from_service_account_file("dummy/file/path.json") @@ -103,7 +103,7 @@ def test_{{ service.client_name|snake_case }}_client_options(): # Check that if channel is provided we won't create a new one. with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.{{ service.client_name }}.get_transport_class') as gtc: transport = transports.{{ service.name }}GrpcTransport( - credentials=credentials.AnonymousCredentials() + credentials=ga_credentials.AnonymousCredentials() ) client = {{ service.client_name }}(transport=transport) gtc.assert_not_called() @@ -254,7 +254,7 @@ def test_{{ service.client_name|snake_case }}_client_options_from_dict(): {% for method in service.methods.values() %} def test_{{ method.name|snake_case }}(transport: str = 'grpc', request_type={{ method.input.ident }}): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) @@ -340,7 +340,7 @@ def test_{{ method.name|snake_case }}_from_dict(): {% if method.field_headers and not method.client_streaming %} def test_{{ method.name|snake_case }}_field_headers(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -385,7 +385,7 @@ def test_{{ method.name|snake_case }}_field_headers(): {% if method.ident.package != method.input.ident.package %} def test_{{ method.name|snake_case }}_from_dict(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -414,7 +414,7 @@ def test_{{ method.name|snake_case }}_from_dict(): {% if method.flattened_fields %} def test_{{ method.name|snake_case }}_flattened(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -461,7 +461,7 @@ def test_{{ method.name|snake_case }}_flattened(): def test_{{ method.name|snake_case }}_flattened_error(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Attempting to call a method with both a request object and flattened @@ -479,7 +479,7 @@ def test_{{ method.name|snake_case }}_flattened_error(): {% if method.paged_result_field %} def test_{{ method.name|snake_case }}_pager(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials, + credentials=ga_credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. @@ -538,7 +538,7 @@ def test_{{ method.name|snake_case }}_pager(): def test_{{ method.name|snake_case }}_pages(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials, + credentials=ga_credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. @@ -587,11 +587,11 @@ def test_{{ method.name|snake_case }}_raw_page_lro(): def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.{{ service.name }}GrpcTransport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) @@ -599,7 +599,7 @@ def test_credentials_transport_error(): def test_transport_instance(): # A client may be instantiated with a custom transport instance. transport = transports.{{ service.name }}GrpcTransport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) client = {{ service.client_name }}(transport=transport) assert client.transport is transport @@ -608,7 +608,7 @@ def test_transport_instance(): def test_transport_get_channel(): # A client may be instantiated with a custom transport instance. transport = transports.{{ service.name }}GrpcTransport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) channel = transport.grpc_channel assert channel @@ -617,7 +617,7 @@ def test_transport_get_channel(): def test_transport_grpc_default(): # A client should use the gRPC transport by default. client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) assert isinstance( client.transport, @@ -630,7 +630,7 @@ def test_transport_grpc_default(): def test_transport_adc(transport_class): # Test default credentials are used if not provided. with mock.patch.object(google.auth, 'default') as adc: - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) transport_class() adc.assert_called_once() @@ -640,7 +640,7 @@ def test_{{ service.name|snake_case }}_base_transport(): with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport.__init__') as Transport: Transport.return_value = None transport = transports.{{ service.name }}Transport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Every method on the transport should just blindly @@ -666,7 +666,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_adc(): # Test the default credentials are used if credentials and credentials_file are None. with mock.patch.object(google.auth, 'default') as adc, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: Transport.return_value = None - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) transport = transports.{{ service.name }}Transport() adc.assert_called_once() @@ -674,7 +674,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_adc(): def test_{{ service.name|snake_case }}_auth_adc(): # If no credentials are provided, we should use ADC credentials. with mock.patch.object(google.auth, 'default') as adc: - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) {{ service.client_name }}() adc.assert_called_once_with(scopes=( {% for scope in service.oauth_scopes %} @@ -687,7 +687,7 @@ def test_{{ service.name|snake_case }}_transport_auth_adc(): # If credentials and host are not provided, the transport class should use # ADC credentials. with mock.patch.object(google.auth, 'default') as adc: - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) transports.{{ service.name }}GrpcTransport(host="squid.clam.whelk") adc.assert_called_once_with(scopes=( {% for scope in service.oauth_scopes %} @@ -699,7 +699,7 @@ def test_{{ service.name|snake_case }}_transport_auth_adc(): def test_{{ service.name|snake_case }}_host_no_port(): {% with host = (service.host|default('localhost', true)).split(':')[0] %} client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), client_options=client_options.ClientOptions(api_endpoint='{{ host }}'), ) assert client.transport._host == '{{ host }}:443' @@ -709,7 +709,7 @@ def test_{{ service.name|snake_case }}_host_no_port(): def test_{{ service.name|snake_case }}_host_with_port(): {% with host = (service.host|default('localhost', true)).split(':')[0] %} client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), client_options=client_options.ClientOptions(api_endpoint='{{ host }}:8000'), ) assert client.transport._host == '{{ host }}:8000' @@ -741,7 +741,7 @@ def test_{{ service.name|snake_case }}_transport_channel_mtls_with_client_cert_s mock_grpc_channel = mock.Mock() grpc_create_channel.return_value = mock_grpc_channel - cred = credentials.AnonymousCredentials() + cred = ga_credentials.AnonymousCredentials() with pytest.warns(DeprecationWarning): with mock.patch.object(google.auth, 'default') as adc: adc.return_value = (cred, None) @@ -820,7 +820,7 @@ def test_{{ service.name|snake_case }}_transport_channel_mtls_with_adc( {% if service.has_lro %} def test_{{ service.name|snake_case }}_grpc_lro_client(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport='grpc', ) transport = client.transport @@ -890,7 +890,7 @@ def test_client_withDEFAULT_CLIENT_INFO(): with mock.patch.object(transports.{{ service.name }}Transport, '_prep_wrapped_messages') as prep: client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) @@ -898,7 +898,7 @@ def test_client_withDEFAULT_CLIENT_INFO(): with mock.patch.object(transports.{{ service.name }}Transport, '_prep_wrapped_messages') as prep: transport_class = {{ service.client_name }}.get_transport_class() transport = transport_class( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 index 10070ce406..54190a5081 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 @@ -12,7 +12,7 @@ import google.api_core.client_options as ClientOptions # type: ignore from google.api_core import exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore -from google.auth import credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.oauth2 import service_account # type: ignore {% filter sort_lines %} @@ -94,7 +94,7 @@ class {{ service.async_client_name }}: get_transport_class = functools.partial(type({{ service.client_name }}).get_transport_class, type({{ service.client_name }})) def __init__(self, *, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, transport: Union[str, {{ service.name }}Transport] = 'grpc_asyncio', client_options: ClientOptions = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index e9a22b2064..42771d7383 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -13,7 +13,7 @@ from google.api_core import client_options as client_options_lib # type: ignore from google.api_core import exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore -from google.auth import credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore from google.auth.exceptions import MutualTLSChannelError # type: ignore @@ -191,7 +191,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): {% endfor %}{# common resources #} def __init__(self, *, - credentials: Optional[credentials.Credentials] = None, + credentials: Optional[ga_credentials.Credentials] = None, transport: Union[str, {{ service.name }}Transport, None] = None, client_options: Optional[client_options_lib.ClientOptions] = None, client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 index 0a6c4d2b77..2e519ce72c 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 @@ -15,7 +15,7 @@ from google.api_core import retry as retries # type: ignore {% if service.has_lro %} from google.api_core import operations_v1 # type: ignore {% endif %} -from google.auth import credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore {% filter sort_lines %} {% for method in service.methods.values() %} @@ -63,7 +63,7 @@ class {{ service.name }}Transport(abc.ABC): def __init__( self, *, host: str = DEFAULT_HOST, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, credentials_file: Optional[str] = None, scopes: Optional[Sequence[str]] = None, quota_project_id: Optional[str] = None, diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 index 8ed6b1dafd..c266473ace 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2 @@ -11,7 +11,7 @@ from google.api_core import operations_v1 # type: ignore {% endif %} from google.api_core import gapic_v1 # type: ignore import google.auth # type: ignore -from google.auth import credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore import grpc # type: ignore @@ -45,7 +45,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): def __init__(self, *, host: str{% if service.host %} = '{{ service.host }}'{% endif %}, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, credentials_file: str = None, scopes: Sequence[str] = None, channel: grpc.Channel = None, @@ -173,7 +173,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport): @classmethod def create_channel(cls, host: str{% if service.host %} = '{{ service.host }}'{% endif %}, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, credentials_file: str = None, scopes: Optional[Sequence[str]] = None, quota_project_id: Optional[str] = None, diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 index 9f5a8607f8..5c02730a19 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2 @@ -10,7 +10,7 @@ from google.api_core import grpc_helpers_async # type: ignore {% if service.has_lro %} from google.api_core import operations_v1 # type: ignore {% endif %} -from google.auth import credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore import packaging.version @@ -50,7 +50,7 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport): @classmethod def create_channel(cls, host: str{% if service.host %} = '{{ service.host }}'{% endif %}, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, credentials_file: Optional[str] = None, scopes: Optional[Sequence[str]] = None, quota_project_id: Optional[str] = None, @@ -90,7 +90,7 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport): def __init__(self, *, host: str{% if service.host %} = '{{ service.host }}'{% endif %}, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, credentials_file: Optional[str] = None, scopes: Optional[Sequence[str]] = None, channel: aio.Channel = None, diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index 18a03f722a..dbb21bfa72 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -9,7 +9,7 @@ from typing import Callable, Dict, Optional, Sequence, Tuple from google.api_core import operations_v1 {% endif %} from google.api_core import gapic_v1 # type: ignore -from google.auth import credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore import grpc # type: ignore @@ -45,7 +45,7 @@ class {{ service.name }}RestTransport({{ service.name }}Transport): {# TODO(yon-mg): handle mtls stuff if that's relevant for rest transport #} def __init__(self, *, host: str{% if service.host %} = '{{ service.host }}'{% endif %}, - credentials: credentials.Credentials = None, + credentials: ga_credentials.Credentials = None, credentials_file: str = None, scopes: Sequence[str] = None, client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None, diff --git a/gapic/templates/examples/sample.py.j2 b/gapic/templates/examples/sample.py.j2 index 4cdb81e47c..cfc56d0432 100644 --- a/gapic/templates/examples/sample.py.j2 +++ b/gapic/templates/examples/sample.py.j2 @@ -34,7 +34,7 @@ def sample_{{ frags.render_method_name(sample.rpc)|trim }}({{ frags.print_input_ """{{ sample.description }}""" client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport="grpc", ) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index efdd3dd105..6daeeef06b 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -20,7 +20,7 @@ from requests.sessions import Session {# Import the service itself as well as every proto module that it imports. #} {% filter sort_lines %} import google.auth -from google.auth import credentials +from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.oauth2 import service_account from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }} import {{ service.client_name }} @@ -110,7 +110,7 @@ def test__get_default_mtls_endpoint(): {% endif %} ]) def test_{{ service.client_name|snake_case }}_from_service_account_info(client_class): - creds = credentials.AnonymousCredentials() + creds = ga_credentials.AnonymousCredentials() with mock.patch.object(service_account.Credentials, 'from_service_account_info') as factory: factory.return_value = creds info = {"valid": True} @@ -130,7 +130,7 @@ def test_{{ service.client_name|snake_case }}_from_service_account_info(client_c {% endif %} ]) def test_{{ service.client_name|snake_case }}_from_service_account_file(client_class): - creds = credentials.AnonymousCredentials() + creds = ga_credentials.AnonymousCredentials() with mock.patch.object(service_account.Credentials, 'from_service_account_file') as factory: factory.return_value = creds client = client_class.from_service_account_file("dummy/file/path.json") @@ -175,7 +175,7 @@ def test_{{ service.client_name|snake_case }}_client_options(client_class, trans # Check that if channel is provided we won't create a new one. with mock.patch.object({{ service.client_name }}, 'get_transport_class') as gtc: transport = transport_class( - credentials=credentials.AnonymousCredentials() + credentials=ga_credentials.AnonymousCredentials() ) client = client_class(transport=transport) gtc.assert_not_called() @@ -420,7 +420,7 @@ def test_{{ service.client_name|snake_case }}_client_options_from_dict(): {% for method in service.methods.values() if 'grpc' in opts.transport %} def test_{{ method.name|snake_case }}(transport: str = 'grpc', request_type={{ method.input.ident }}): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) @@ -508,7 +508,7 @@ def test_{{ method.name|snake_case }}_empty_call(): # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport='grpc', ) @@ -530,7 +530,7 @@ def test_{{ method.name|snake_case }}_empty_call(): @pytest.mark.asyncio async def test_{{ method.name|snake_case }}_async(transport: str = 'grpc_asyncio', request_type={{ method.input.ident }}): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) @@ -620,7 +620,7 @@ async def test_{{ method.name|snake_case }}_async_from_dict(): {% if method.field_headers and not method.client_streaming %} def test_{{ method.name|snake_case }}_field_headers(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -665,7 +665,7 @@ def test_{{ method.name|snake_case }}_field_headers(): @pytest.mark.asyncio async def test_{{ method.name|snake_case }}_field_headers_async(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -711,7 +711,7 @@ async def test_{{ method.name|snake_case }}_field_headers_async(): {% if method.ident.package != method.input.ident.package %} def test_{{ method.name|snake_case }}_from_dict_foreign(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -740,7 +740,7 @@ def test_{{ method.name|snake_case }}_from_dict_foreign(): {% if method.flattened_fields %} def test_{{ method.name|snake_case }}_flattened(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -788,7 +788,7 @@ def test_{{ method.name|snake_case }}_flattened(): def test_{{ method.name|snake_case }}_flattened_error(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Attempting to call a method with both a request object and flattened @@ -805,7 +805,7 @@ def test_{{ method.name|snake_case }}_flattened_error(): @pytest.mark.asyncio async def test_{{ method.name|snake_case }}_flattened_async(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. @@ -873,7 +873,7 @@ async def test_{{ method.name|snake_case }}_flattened_async(): @pytest.mark.asyncio async def test_{{ method.name|snake_case }}_flattened_error_async(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Attempting to call a method with both a request object and flattened @@ -891,7 +891,7 @@ async def test_{{ method.name|snake_case }}_flattened_error_async(): {% if method.paged_result_field %} def test_{{ method.name|snake_case }}_pager(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials, + credentials=ga_credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. @@ -950,7 +950,7 @@ def test_{{ method.name|snake_case }}_pager(): def test_{{ method.name|snake_case }}_pages(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials, + credentials=ga_credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. @@ -992,7 +992,7 @@ def test_{{ method.name|snake_case }}_pages(): @pytest.mark.asyncio async def test_{{ method.name|snake_case }}_async_pager(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials, + credentials=ga_credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. @@ -1040,7 +1040,7 @@ async def test_{{ method.name|snake_case }}_async_pager(): @pytest.mark.asyncio async def test_{{ method.name|snake_case }}_async_pages(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials, + credentials=ga_credentials.AnonymousCredentials, ) # Mock the actual call within the gRPC stub, and fake the request. @@ -1091,7 +1091,7 @@ def test_{{ method.name|snake_case }}_raw_page_lro(): {% for method in service.methods.values() if 'rest' in opts.transport %} def test_{{ method.name|snake_case }}_rest(transport: str = 'rest', request_type={{ method.input.ident }}): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) @@ -1159,7 +1159,7 @@ def test_{{ method.name|snake_case }}_rest_from_dict(): def test_{{ method.name|snake_case }}_rest_flattened(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the http request call within the method and fake a response. @@ -1214,7 +1214,7 @@ def test_{{ method.name|snake_case }}_rest_flattened(): def test_{{ method.name|snake_case }}_rest_flattened_error(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Attempting to call a method with both a request object and flattened @@ -1231,7 +1231,7 @@ def test_{{ method.name|snake_case }}_rest_flattened_error(): {% if method.paged_result_field %} def test_{{ method.name|snake_case }}_pager(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the http request call within the method and fake a response. @@ -1351,17 +1351,17 @@ def test_{{ method.name|snake_case }}_pager(): def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.{{ service.name }}{{ opts.transport[0].capitalize() }}Transport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # It is an error to provide a credentials file and a transport instance. transport = transports.{{ service.name }}{{ opts.transport[0].capitalize() }}Transport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = {{ service.client_name }}( @@ -1371,7 +1371,7 @@ def test_credentials_transport_error(): # It is an error to provide scopes and a transport instance. transport = transports.{{ service.name }}{{ opts.transport[0].capitalize() }}Transport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) with pytest.raises(ValueError): client = {{ service.client_name }}( @@ -1383,7 +1383,7 @@ def test_credentials_transport_error(): def test_transport_instance(): # A client may be instantiated with a custom transport instance. transport = transports.{{ service.name }}{{ opts.transport[0].capitalize() }}Transport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) client = {{ service.client_name }}(transport=transport) assert client.transport is transport @@ -1392,13 +1392,13 @@ def test_transport_instance(): def test_transport_get_channel(): # A client may be instantiated with a custom transport instance. transport = transports.{{ service.name }}GrpcTransport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) channel = transport.grpc_channel assert channel transport = transports.{{ service.grpc_asyncio_transport_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) channel = transport.grpc_channel assert channel @@ -1415,7 +1415,7 @@ def test_transport_get_channel(): def test_transport_adc(transport_class): # Test default credentials are used if not provided. with mock.patch.object(google.auth, 'default') as adc: - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) transport_class() adc.assert_called_once() @@ -1423,7 +1423,7 @@ def test_transport_adc(transport_class): def test_transport_grpc_default(): # A client should use the gRPC transport by default. client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) assert isinstance( client.transport, @@ -1435,7 +1435,7 @@ def test_{{ service.name|snake_case }}_base_transport_error(): # Passing both a credentials object and credentials_file should raise an error with pytest.raises(exceptions.DuplicateCredentialArgs): transport = transports.{{ service.name }}Transport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), credentials_file="credentials.json" ) @@ -1445,7 +1445,7 @@ def test_{{ service.name|snake_case }}_base_transport(): with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport.__init__') as Transport: Transport.return_value = None transport = transports.{{ service.name }}Transport( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Every method on the transport should just blindly @@ -1477,7 +1477,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_credentials_file(): # Instantiate the base transport with a credentials file with mock.patch.object(google.auth, 'load_credentials_from_file', autospec=True) as load_creds, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: Transport.return_value = None - load_creds.return_value = (credentials.AnonymousCredentials(), None) + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) transport = transports.{{ service.name }}Transport( credentials_file="credentials.json", quota_project_id="octopus", @@ -1497,7 +1497,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_credentials_file_old_ # Instantiate the base transport with a credentials file with mock.patch.object(google.auth, 'load_credentials_from_file', autospec=True) as load_creds, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: Transport.return_value = None - load_creds.return_value = (credentials.AnonymousCredentials(), None) + load_creds.return_value = (ga_credentials.AnonymousCredentials(), None) transport = transports.{{ service.name }}Transport( credentials_file="credentials.json", quota_project_id="octopus", @@ -1515,7 +1515,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_adc(): # Test the default credentials are used if credentials and credentials_file are None. with mock.patch.object(google.auth, 'default', autospec=True) as adc, mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}Transport._prep_wrapped_messages') as Transport: Transport.return_value = None - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) transport = transports.{{ service.name }}Transport() adc.assert_called_once() @@ -1524,7 +1524,7 @@ def test_{{ service.name|snake_case }}_base_transport_with_adc(): def test_{{ service.name|snake_case }}_auth_adc(): # If no credentials are provided, we should use ADC credentials. with mock.patch.object(google.auth, 'default', autospec=True) as adc: - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) {{ service.client_name }}() adc.assert_called_once_with( scopes=None, @@ -1540,7 +1540,7 @@ def test_{{ service.name|snake_case }}_auth_adc(): def test_{{ service.name|snake_case }}_auth_adc_old_google_auth(): # If no credentials are provided, we should use ADC credentials. with mock.patch.object(google.auth, 'default', autospec=True) as adc: - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) {{ service.client_name }}() adc.assert_called_once_with( scopes=( @@ -1564,7 +1564,7 @@ def test_{{ service.name|snake_case }}_transport_auth_adc(transport_class): # If credentials and host are not provided, the transport class should use # ADC credentials. with mock.patch.object(google.auth, 'default', autospec=True) as adc: - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) transport_class(quota_project_id="octopus", scopes=["1", "2"]) adc.assert_called_once_with( scopes=["1", "2"], @@ -1588,7 +1588,7 @@ def test_{{ service.name|snake_case }}_transport_auth_adc_old_google_auth(transp # If credentials and host are not provided, the transport class should use # ADC credentials. with mock.patch.object(google.auth, "default", autospec=True) as adc: - adc.return_value = (credentials.AnonymousCredentials(), None) + adc.return_value = (ga_credentials.AnonymousCredentials(), None) transport_class(quota_project_id="octopus") adc.assert_called_once_with(scopes=( {% for scope in service.oauth_scopes %} @@ -1612,7 +1612,7 @@ def test_{{ service.name|snake_case }}_transport_create_channel(transport_class, with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch.object( grpc_helpers, "create_channel", autospec=True ) as create_channel: - creds = credentials.AnonymousCredentials() + creds = ga_credentials.AnonymousCredentials() adc.return_value = (creds, None) transport_class( quota_project_id="octopus", @@ -1654,7 +1654,7 @@ def test_{{ service.name|snake_case }}_transport_create_channel_old_api_core(tra with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch.object( grpc_helpers, "create_channel", autospec=True ) as create_channel: - creds = credentials.AnonymousCredentials() + creds = ga_credentials.AnonymousCredentials() adc.return_value = (creds, None) transport_class(quota_project_id="octopus") @@ -1691,7 +1691,7 @@ def test_{{ service.name|snake_case }}_transport_create_channel_user_scopes(tran with mock.patch.object(google.auth, "default", autospec=True) as adc, mock.patch.object( grpc_helpers, "create_channel", autospec=True ) as create_channel: - creds = credentials.AnonymousCredentials() + creds = ga_credentials.AnonymousCredentials() adc.return_value = (creds, None) {% with host = (service.host|default('localhost', true)) %} @@ -1718,7 +1718,7 @@ def test_{{ service.name|snake_case }}_transport_create_channel_user_scopes(tran def test_{{ service.name|snake_case }}_grpc_transport_client_cert_source_for_mtls( transport_class ): - cred = credentials.AnonymousCredentials() + cred = ga_credentials.AnonymousCredentials() # Check ssl_channel_credentials is used if provided. with mock.patch.object(transport_class, "create_channel") as mock_create_channel: @@ -1762,7 +1762,7 @@ def test_{{ service.name|snake_case }}_grpc_transport_client_cert_source_for_mtl {% if 'rest' in opts.transport %} def test_{{ service.name|snake_case }}_http_transport_client_cert_source_for_mtls(): - cred = credentials.AnonymousCredentials() + cred = ga_credentials.AnonymousCredentials() with mock.patch("google.auth.transport.requests.AuthorizedSession.configure_mtls_channel") as mock_configure_mtls_channel: transports.{{ service.rest_transport_name }} ( credentials=cred, @@ -1774,7 +1774,7 @@ def test_{{ service.name|snake_case }}_http_transport_client_cert_source_for_mtl def test_{{ service.name|snake_case }}_host_no_port(): {% with host = (service.host|default('localhost', true)).split(':')[0] %} client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), client_options=client_options.ClientOptions(api_endpoint='{{ host }}'), ) assert client.transport._host == '{{ host }}:443' @@ -1784,7 +1784,7 @@ def test_{{ service.name|snake_case }}_host_no_port(): def test_{{ service.name|snake_case }}_host_with_port(): {% with host = (service.host|default('localhost', true)).split(':')[0] %} client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), client_options=client_options.ClientOptions(api_endpoint='{{ host }}:8000'), ) assert client.transport._host == '{{ host }}:8000' @@ -1831,7 +1831,7 @@ def test_{{ service.name|snake_case }}_transport_channel_mtls_with_client_cert_s mock_grpc_channel = mock.Mock() grpc_create_channel.return_value = mock_grpc_channel - cred = credentials.AnonymousCredentials() + cred = ga_credentials.AnonymousCredentials() with pytest.warns(DeprecationWarning): with mock.patch.object(google.auth, 'default') as adc: adc.return_value = (cred, None) @@ -1912,7 +1912,7 @@ def test_{{ service.name|snake_case }}_transport_channel_mtls_with_adc( {% if service.has_lro %} def test_{{ service.name|snake_case }}_grpc_lro_client(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport='grpc', ) transport = client.transport @@ -1929,7 +1929,7 @@ def test_{{ service.name|snake_case }}_grpc_lro_client(): def test_{{ service.name|snake_case }}_grpc_lro_async_client(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), transport='grpc_asyncio', ) transport = client.transport @@ -2001,7 +2001,7 @@ def test_client_withDEFAULT_CLIENT_INFO(): with mock.patch.object(transports.{{ service.name }}Transport, '_prep_wrapped_messages') as prep: client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) @@ -2009,7 +2009,7 @@ def test_client_withDEFAULT_CLIENT_INFO(): with mock.patch.object(transports.{{ service.name }}Transport, '_prep_wrapped_messages') as prep: transport_class = {{ service.client_name }}.get_transport_class() transport = transport_class( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) @@ -2018,7 +2018,7 @@ def test_client_withDEFAULT_CLIENT_INFO(): {% if opts.add_iam_methods and 'grpc' in opts.transport %} def test_set_iam_policy(transport: str = "grpc"): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, @@ -2049,7 +2049,7 @@ def test_set_iam_policy(transport: str = "grpc"): @pytest.mark.asyncio async def test_set_iam_policy_async(transport: str = "grpc_asyncio"): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, @@ -2083,7 +2083,7 @@ async def test_set_iam_policy_async(transport: str = "grpc_asyncio"): def test_set_iam_policy_field_headers(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2110,7 +2110,7 @@ def test_set_iam_policy_field_headers(): @pytest.mark.asyncio async def test_set_iam_policy_field_headers_async(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2138,7 +2138,7 @@ async def test_set_iam_policy_field_headers_async(): def test_set_iam_policy_from_dict(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.set_iam_policy), "__call__") as call: @@ -2157,7 +2157,7 @@ def test_set_iam_policy_from_dict(): @pytest.mark.asyncio async def test_set_iam_policy_from_dict_async(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.set_iam_policy), "__call__") as call: @@ -2177,7 +2177,7 @@ async def test_set_iam_policy_from_dict_async(): def test_get_iam_policy(transport: str = "grpc"): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, @@ -2208,7 +2208,7 @@ def test_get_iam_policy(transport: str = "grpc"): @pytest.mark.asyncio async def test_get_iam_policy_async(transport: str = "grpc_asyncio"): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, @@ -2242,7 +2242,7 @@ async def test_get_iam_policy_async(transport: str = "grpc_asyncio"): def test_get_iam_policy_field_headers(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2269,7 +2269,7 @@ def test_get_iam_policy_field_headers(): @pytest.mark.asyncio async def test_get_iam_policy_field_headers_async(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2297,7 +2297,7 @@ async def test_get_iam_policy_field_headers_async(): def test_get_iam_policy_from_dict(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.get_iam_policy), "__call__") as call: @@ -2315,7 +2315,7 @@ def test_get_iam_policy_from_dict(): @pytest.mark.asyncio async def test_get_iam_policy_from_dict_async(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.get_iam_policy), "__call__") as call: @@ -2335,7 +2335,7 @@ async def test_get_iam_policy_from_dict_async(): def test_test_iam_permissions(transport: str = "grpc"): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, @@ -2368,7 +2368,7 @@ def test_test_iam_permissions(transport: str = "grpc"): @pytest.mark.asyncio async def test_test_iam_permissions_async(transport: str = "grpc_asyncio"): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, @@ -2400,7 +2400,7 @@ async def test_test_iam_permissions_async(transport: str = "grpc_asyncio"): def test_test_iam_permissions_field_headers(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2429,7 +2429,7 @@ def test_test_iam_permissions_field_headers(): @pytest.mark.asyncio async def test_test_iam_permissions_field_headers_async(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as @@ -2459,7 +2459,7 @@ async def test_test_iam_permissions_field_headers_async(): def test_test_iam_permissions_from_dict(): client = {{ service.client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -2479,7 +2479,7 @@ def test_test_iam_permissions_from_dict(): @pytest.mark.asyncio async def test_test_iam_permissions_from_dict_async(): client = {{ service.async_client_name }}( - credentials=credentials.AnonymousCredentials(), + credentials=ga_credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( From 83682ef115ab3013f4634e2e3ce73c23958ee9dc Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 22:04:44 +0000 Subject: [PATCH 05/10] fix: fix collision with 'exceptions' --- .../%name/%version/%sub/services/%service/client.py.j2 | 2 +- .../%sub/services/%service/async_client.py.j2 | 2 +- .../%name_%version/%sub/services/%service/client.py.j2 | 2 +- .../%sub/services/%service/transports/base.py.j2 | 6 +++--- .../unit/gapic/%name_%version/%sub/test_%service.py.j2 | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 index 7327bf0c99..48d4fb612a 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 @@ -10,7 +10,7 @@ from typing import Callable, Dict, Optional, {% if service.any_server_streaming import pkg_resources from google.api_core import client_options as client_options_lib # type: ignore -from google.api_core import exceptions # type: ignore +from google.api_core import exceptions as core_exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore from google.auth import credentials as ga_credentials # type: ignore diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 index 54190a5081..d254bf42ac 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 @@ -9,7 +9,7 @@ from typing import Dict, {% if service.any_server_streaming %}AsyncIterable, Awa import pkg_resources import google.api_core.client_options as ClientOptions # type: ignore -from google.api_core import exceptions # type: ignore +from google.api_core import exceptions as core_exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore from google.auth import credentials as ga_credentials # type: ignore diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index 42771d7383..089519e706 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -10,7 +10,7 @@ from typing import Callable, Dict, Optional, {% if service.any_server_streaming import pkg_resources from google.api_core import client_options as client_options_lib # type: ignore -from google.api_core import exceptions # type: ignore +from google.api_core import exceptions as core_exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore from google.auth import credentials as ga_credentials # type: ignore diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 index 2e519ce72c..819448a18b 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/base.py.j2 @@ -9,7 +9,7 @@ import pkg_resources import google.auth # type: ignore import google.api_core # type: ignore -from google.api_core import exceptions # type: ignore +from google.api_core import exceptions as core_exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore {% if service.has_lro %} @@ -105,7 +105,7 @@ class {{ service.name }}Transport(abc.ABC): # If no credentials are provided, then determine the appropriate # defaults. if credentials and credentials_file: - raise exceptions.DuplicateCredentialArgs("'credentials_file' and 'credentials' are mutually exclusive") + raise core_exceptions.DuplicateCredentialArgs("'credentials_file' and 'credentials' are mutually exclusive") if credentials_file is not None: credentials, _ = google.auth.load_credentials_from_file( @@ -176,7 +176,7 @@ class {{ service.name }}Transport(abc.ABC): {% if method.retry.backoff_multiplier %}multiplier={{ method.retry.backoff_multiplier }},{% endif %} predicate=retries.if_exception_type( {% for ex in method.retry.retryable_exceptions|sort(attribute='__name__') %} - exceptions.{{ ex.__name__ }}, + core_exceptions.{{ ex.__name__ }}, {% endfor %} ), deadline={{ method.timeout }}, diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 6daeeef06b..ec56a2ce9b 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -31,7 +31,7 @@ from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + ser from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.base import _GOOGLE_AUTH_VERSION from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.base import _API_CORE_VERSION from google.api_core import client_options -from google.api_core import exceptions +from google.api_core import exceptions as core_exceptions from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async {% if service.has_lro %} @@ -1433,7 +1433,7 @@ def test_transport_grpc_default(): def test_{{ service.name|snake_case }}_base_transport_error(): # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(exceptions.DuplicateCredentialArgs): + with pytest.raises(core_exceptions.DuplicateCredentialArgs): transport = transports.{{ service.name }}Transport( credentials=ga_credentials.AnonymousCredentials(), credentials_file="credentials.json" From 1342cba57040e3e143f91b2dd8b117c0f0b22809 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 22:07:36 +0000 Subject: [PATCH 06/10] docs: preserve comments in reserved_names.py --- gapic/utils/reserved_names.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gapic/utils/reserved_names.py b/gapic/utils/reserved_names.py index 9bf1c9a914..cf87e83992 100644 --- a/gapic/utils/reserved_names.py +++ b/gapic/utils/reserved_names.py @@ -21,7 +21,9 @@ # They are explicitly allowed message, module, and field names. RESERVED_NAMES = frozenset( itertools.chain( + # We CANNOT make exceptions for keywords. keyword.kwlist, + # We make SOME exceptions for certain names that collide with builtins. set(dir(builtins)) - {"filter", "map", "id", "input", "property"}, ) ) From a06c699f1dc43c36b284747b1d127a9824e13524 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 22:11:29 +0000 Subject: [PATCH 07/10] chore: adjust import format --- .../%version/%sub/services/%service/transports/grpc.py.j2 | 2 +- .../%name_%version/%sub/services/%service/async_client.py.j2 | 2 +- .../%name_%version/%sub/services/%service/client.py.j2 | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 index 7d1d931c98..af6b11ad89 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2 @@ -11,7 +11,7 @@ from google.api_core import operations_v1 # type: ignore {% endif %} from google.api_core import gapic_v1 # type: ignore import google.auth # type: ignore -from google.auth import credentials as ga_credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore import grpc # type: ignore diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 index d254bf42ac..2911e327cb 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 @@ -9,7 +9,7 @@ from typing import Dict, {% if service.any_server_streaming %}AsyncIterable, Awa import pkg_resources import google.api_core.client_options as ClientOptions # type: ignore -from google.api_core import exceptions as core_exceptions # type: ignore +from google.api_core import exceptions as core_exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore from google.auth import credentials as ga_credentials # type: ignore diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index 089519e706..45efbc3cc1 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -10,10 +10,10 @@ from typing import Callable, Dict, Optional, {% if service.any_server_streaming import pkg_resources from google.api_core import client_options as client_options_lib # type: ignore -from google.api_core import exceptions as core_exceptions # type: ignore +from google.api_core import exceptions as core_exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore -from google.auth import credentials as ga_credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore from google.auth.exceptions import MutualTLSChannelError # type: ignore From fab9580936a834acc1e112c90894e1fafd8edc22 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 22:41:15 +0000 Subject: [PATCH 08/10] fix: fix typos --- .../services/%service/transports/base.py.j2 | 2 +- gapic/ads-templates/examples/sample.py.j2 | 2 +- .../%sub/services/%service/async_client.py.j2 | 22 +++++++++---------- gapic/templates/examples/sample.py.j2 | 2 +- .../%name_%version/%sub/test_%service.py.j2 | 6 ++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 index 1795b3e3bf..d630c8f023 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2 @@ -6,7 +6,7 @@ import abc import typing import pkg_resources -import google.auth +import google.auth # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore {% if service.has_lro %} diff --git a/gapic/ads-templates/examples/sample.py.j2 b/gapic/ads-templates/examples/sample.py.j2 index 9c2ff58c84..05ff8e1ee2 100644 --- a/gapic/ads-templates/examples/sample.py.j2 +++ b/gapic/ads-templates/examples/sample.py.j2 @@ -34,7 +34,7 @@ def sample_{{ frags.render_method_name(sample.rpc)|trim }}({{ frags.print_input_ """{{ sample.description }}""" client = {{ service.client_name }}( - credentials=ga_credentials.AnonymousCredentials(), + credentials=credentials.AnonymousCredentials(), transport="grpc", ) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 index 2911e327cb..47ccc596a8 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2 @@ -320,7 +320,7 @@ class {{ service.async_client_name }}: {% if opts.add_iam_methods %} async def set_iam_policy( self, - request: iam_policy_pb2.PolicySetIamPolicyRequest = None, + request: iam_policy_pb2.SetIamPolicyRequest = None, *, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, @@ -329,7 +329,7 @@ class {{ service.async_client_name }}: r"""Sets the IAM access control policy on the specified function. Replaces any existing policy. Args: - request (:class:`~.iam_policy_pb2.PolicySetIamPolicyRequest`): + request (:class:`~.policy_pb2.SetIamPolicyRequest`): The request object. Request message for `SetIamPolicy` method. retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -402,7 +402,7 @@ class {{ service.async_client_name }}: # The request isn't a proto-plus wrapped type, # so it must be constructed via keyword expansion. if isinstance(request, dict): - request = iam_policy_pb2.PolicySetIamPolicyRequest(**request) + request = iam_policy_pb2.SetIamPolicyRequest(**request) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. @@ -426,7 +426,7 @@ class {{ service.async_client_name }}: async def get_iam_policy( self, - request: iam_policy_pb2.PolicyGetIamPolicyRequest = None, + request: iam_policy_pb2.GetIamPolicyRequest = None, *, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, @@ -436,7 +436,7 @@ class {{ service.async_client_name }}: Returns an empty policy if the function exists and does not have a policy set. Args: - request (:class:`~.iam_policy_pb2.PolicyGetIamPolicyRequest`): + request (:class:`~.iam_policy_pb2.GetIamPolicyRequest`): The request object. Request message for `GetIamPolicy` method. retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -509,7 +509,7 @@ class {{ service.async_client_name }}: # The request isn't a proto-plus wrapped type, # so it must be constructed via keyword expansion. if isinstance(request, dict): - request = iam_policy_pb2.PolicyGetIamPolicyRequest(**request) + request = iam_policy_pb2.GetIamPolicyRequest(**request) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. @@ -533,17 +533,17 @@ class {{ service.async_client_name }}: async def test_iam_permissions( self, - request: iam_policy_pb2.PolicyTestIamPermissionsRequest = None, + request: iam_policy_pb2.TestIamPermissionsRequest = None, *, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> iam_policy_pb2.PolicyTestIamPermissionsResponse: + ) -> iam_policy_pb2.TestIamPermissionsResponse: r"""Tests the specified permissions against the IAM access control policy for a function. If the function does not exist, this will return an empty set of permissions, not a NOT_FOUND error. Args: - request (:class:`~.iam_policy_pb2.PolicyTestIamPermissionsRequest`): + request (:class:`~.iam_policy_pb2.TestIamPermissionsRequest`): The request object. Request message for `TestIamPermissions` method. retry (google.api_core.retry.Retry): Designation of what errors, if any, @@ -552,7 +552,7 @@ class {{ service.async_client_name }}: metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: - ~.iam_policy_pb2.PolicyTestIamPermissionsResponse: + ~iam_policy_pb2.PolicyTestIamPermissionsResponse: Response message for ``TestIamPermissions`` method. """ # Create or coerce a protobuf request object. @@ -560,7 +560,7 @@ class {{ service.async_client_name }}: # The request isn't a proto-plus wrapped type, # so it must be constructed via keyword expansion. if isinstance(request, dict): - request = iam_policy_pb2.PolicyTestIamPermissionsRequest(**request) + request = iam_policy_pb2.TestIamPermissionsRequest(**request) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. diff --git a/gapic/templates/examples/sample.py.j2 b/gapic/templates/examples/sample.py.j2 index cfc56d0432..4cdb81e47c 100644 --- a/gapic/templates/examples/sample.py.j2 +++ b/gapic/templates/examples/sample.py.j2 @@ -34,7 +34,7 @@ def sample_{{ frags.render_method_name(sample.rpc)|trim }}({{ frags.print_input_ """{{ sample.description }}""" client = {{ service.client_name }}( - credentials=ga_credentials.AnonymousCredentials(), + credentials=credentials.AnonymousCredentials(), transport="grpc", ) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index ec56a2ce9b..08a68d39bc 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -50,7 +50,7 @@ from google.api_core import gapic_v1 {% if opts.add_iam_methods %} from google.iam.v1 import iam_policy_pb2 # type: ignore from google.iam.v1 import options_pb2 # type: ignore -from google.iam.v1 import policy_pb2 as policy # type: ignore +from google.iam.v1 import policy_pb2 # type: ignore {% endif %} {% endfilter %} @@ -2307,7 +2307,7 @@ def test_get_iam_policy_from_dict(): response = client.get_iam_policy( request={ "resource": "resource_value", - "options": options.GetPolicyOptions(requested_policy_version=2598), + "options": options_pb2.GetPolicyOptions(requested_policy_version=2598), } ) call.assert_called() @@ -2327,7 +2327,7 @@ async def test_get_iam_policy_from_dict_async(): response = await client.get_iam_policy( request={ "resource": "resource_value", - "options": options.GetPolicyOptions(requested_policy_version=2598), + "options": options_pb2.GetPolicyOptions(requested_policy_version=2598), } ) call.assert_called() From 9b5eb6d4f4da297980a2ed21dddbdf4e95a32334 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 22:53:17 +0000 Subject: [PATCH 09/10] test: add test case for common proto --- tests/unit/schema/test_metadata.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/unit/schema/test_metadata.py b/tests/unit/schema/test_metadata.py index 693beffa9b..477fcf5ad1 100644 --- a/tests/unit/schema/test_metadata.py +++ b/tests/unit/schema/test_metadata.py @@ -43,6 +43,12 @@ def test_address_str_parent(): assert str(addr) == 'baz.spam.eggs.Bacon' +def test_address_str_different_proto_package(): + addr = metadata.Address(package=('google', 'iam', 'v1'), module='options', name='GetPolicyOptions', + api_naming=naming.NewNaming(proto_package='foo.bar.baz.v1')) + assert str(addr) == 'options_pb2.GetPolicyOptions' + + def test_address_proto(): addr = metadata.Address(package=('foo', 'bar'), module='baz', name='Bacon') assert addr.proto == 'foo.bar.Bacon' From 04439364e0f74683ebdffc8e09bcd3ef070f5eeb Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 28 Apr 2021 23:11:36 +0000 Subject: [PATCH 10/10] fix: fix formatting --- .../%name/%version/%sub/services/%service/client.py.j2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 index 48d4fb612a..3e02216712 100644 --- a/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 +++ b/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2 @@ -10,10 +10,10 @@ from typing import Callable, Dict, Optional, {% if service.any_server_streaming import pkg_resources from google.api_core import client_options as client_options_lib # type: ignore -from google.api_core import exceptions as core_exceptions # type: ignore +from google.api_core import exceptions as core_exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore -from google.auth import credentials as ga_credentials # type: ignore +from google.auth import credentials as ga_credentials # type: ignore from google.auth.transport import mtls # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore from google.auth.exceptions import MutualTLSChannelError # type: ignore