Skip to content

Commit 274c8bd

Browse files
authored
fix: remove auth, policy, and options from the reserved names list (#851)
Fixes #835. Breakdown by name that was originally added in #824 - `auth`: `from google import auth` -> `import google.auth` - `credentials`: `from google.auth import credentials` -> `from google.auth import credentials as ga_credentials` - `exceptions`: `from google.api_core import exceptions` -> `from google.api_core import exceptions as core_exceptions` - `future`: skipped, as it is only used in the [generated tests](https://github.com/googleapis/gapic-generator-python/search?q=%22import+future%22) and has a low chance of colliding - `options` `from google.iam.v1 import options_pb2 as options` -> `from google.iam.v1 import options_pb2` - `policy` `from google.iam.v1 import policy_pb2 as policy` -> `from google.iam.v1 import policy_pb2` - `math` skipped as it is only used in [generated tests](https://github.com/googleapis/gapic-generator-python/search?q=%22import+math%22) For `options` and `policy` there is a small change to `gapic/schema/metadata.py` to not alias `_pb2` types
1 parent ba1f07a commit 274c8bd

File tree

14 files changed

+280
-276
lines changed

14 files changed

+280
-276
lines changed

packages/gapic-generator/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ from typing import Callable, Dict, Optional, {% if service.any_server_streaming
1010
import pkg_resources
1111

1212
from google.api_core import client_options as client_options_lib # type: ignore
13-
from google.api_core import exceptions # type: ignore
13+
from google.api_core import exceptions as core_exceptions # type: ignore
1414
from google.api_core import gapic_v1 # type: ignore
1515
from google.api_core import retry as retries # type: ignore
16-
from google.auth import credentials # type: ignore
16+
from google.auth import credentials as ga_credentials # type: ignore
1717
from google.auth.transport import mtls # type: ignore
1818
from google.auth.transport.grpc import SslCredentials # type: ignore
1919
from google.auth.exceptions import MutualTLSChannelError # type: ignore
@@ -174,7 +174,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
174174
{% endfor %} {# common resources #}
175175

176176
def __init__(self, *,
177-
credentials: Optional[credentials.Credentials] = None,
177+
credentials: Optional[ga_credentials.Credentials] = None,
178178
transport: Union[str, {{ service.name }}Transport, None] = None,
179179
client_options: Optional[client_options_lib.ClientOptions] = None,
180180
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,

packages/gapic-generator/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/base.py.j2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import abc
66
import typing
77
import pkg_resources
88

9-
from google import auth
9+
import google.auth # type: ignore
1010
from google.api_core import gapic_v1 # type: ignore
1111
from google.api_core import retry as retries # type: ignore
1212
{% if service.has_lro %}
1313
from google.api_core import operations_v1 # type: ignore
1414
{% endif %}
15-
from google.auth import credentials # type: ignore
15+
from google.auth import credentials as ga_credentials # type: ignore
1616

1717
{% filter sort_lines %}
1818
{% for method in service.methods.values() %}
@@ -43,7 +43,7 @@ class {{ service.name }}Transport(metaclass=abc.ABCMeta):
4343
def __init__(
4444
self, *,
4545
host: str{% if service.host %} = '{{ service.host }}'{% endif %},
46-
credentials: credentials.Credentials = None,
46+
credentials: ga_credentials.Credentials = None,
4747
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
4848
) -> None:
4949
"""Instantiate the transport.
@@ -70,7 +70,7 @@ class {{ service.name }}Transport(metaclass=abc.ABCMeta):
7070
# If no credentials are provided, then determine the appropriate
7171
# defaults.
7272
if credentials is None:
73-
credentials, _ = auth.default(scopes=self.AUTH_SCOPES)
73+
credentials, _ = google.auth.default(scopes=self.AUTH_SCOPES)
7474

7575
# Save the credentials.
7676
self._credentials = credentials

packages/gapic-generator/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/transports/grpc.py.j2

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ from google.api_core import grpc_helpers # type: ignore
1010
from google.api_core import operations_v1 # type: ignore
1111
{% endif %}
1212
from google.api_core import gapic_v1 # type: ignore
13-
from google import auth # type: ignore
14-
from google.auth import credentials # type: ignore
13+
import google.auth # type: ignore
14+
from google.auth import credentials as ga_credentials # type: ignore
1515
from google.auth.transport.grpc import SslCredentials # type: ignore
1616

1717
import grpc # type: ignore
@@ -39,7 +39,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
3939
"""
4040
def __init__(self, *,
4141
host: str{% if service.host %} = '{{ service.host }}'{% endif %},
42-
credentials: credentials.Credentials = None,
42+
credentials: ga_credentials.Credentials = None,
4343
credentials_file: str = None,
4444
scopes: Sequence[str] = None,
4545
channel: grpc.Channel = None,
@@ -105,7 +105,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
105105
host = api_mtls_endpoint if ":" in api_mtls_endpoint else api_mtls_endpoint + ":443"
106106

107107
if credentials is None:
108-
credentials, _ = auth.default(scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id)
108+
credentials, _ = google.auth.default(scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id)
109109

110110
# Create SSL credentials with client_cert_source or application
111111
# default SSL credentials.
@@ -135,7 +135,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
135135
host = host if ":" in host else host + ":443"
136136

137137
if credentials is None:
138-
credentials, _ = auth.default(scopes=self.AUTH_SCOPES)
138+
credentials, _ = google.auth.default(scopes=self.AUTH_SCOPES)
139139

140140
# create a new channel. The provided one is ignored.
141141
self._grpc_channel = type(self).create_channel(
@@ -162,7 +162,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
162162
@classmethod
163163
def create_channel(cls,
164164
host: str{% if service.host %} = '{{ service.host }}'{% endif %},
165-
credentials: credentials.Credentials = None,
165+
credentials: ga_credentials.Credentials = None,
166166
scopes: Optional[Sequence[str]] = None,
167167
**kwargs) -> grpc.Channel:
168168
"""Create and return a gRPC channel object.

packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ from proto.marshal.rules.dates import DurationRule, TimestampRule
1212

1313
{# Import the service itself as well as every proto module that it imports. -#}
1414
{% filter sort_lines %}
15-
from google import auth
16-
from google.auth import credentials
15+
import google.auth
16+
from google.auth import credentials as ga_credentials
1717
from google.auth.exceptions import MutualTLSChannelError
1818
from google.oauth2 import service_account
1919
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():
6363

6464

6565
def test_{{ service.client_name|snake_case }}_from_service_account_info():
66-
creds = credentials.AnonymousCredentials()
66+
creds = ga_credentials.AnonymousCredentials()
6767
with mock.patch.object(service_account.Credentials, 'from_service_account_info') as factory:
6868
factory.return_value = creds
6969
info = {"valid": True}
@@ -76,7 +76,7 @@ def test_{{ service.client_name|snake_case }}_from_service_account_info():
7676

7777

7878
def test_{{ service.client_name|snake_case }}_from_service_account_file():
79-
creds = credentials.AnonymousCredentials()
79+
creds = ga_credentials.AnonymousCredentials()
8080
with mock.patch.object(service_account.Credentials, 'from_service_account_file') as factory:
8181
factory.return_value = creds
8282
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():
103103
# Check that if channel is provided we won't create a new one.
104104
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:
105105
transport = transports.{{ service.name }}GrpcTransport(
106-
credentials=credentials.AnonymousCredentials()
106+
credentials=ga_credentials.AnonymousCredentials()
107107
)
108108
client = {{ service.client_name }}(transport=transport)
109109
gtc.assert_not_called()
@@ -254,7 +254,7 @@ def test_{{ service.client_name|snake_case }}_client_options_from_dict():
254254
{% for method in service.methods.values() %}
255255
def test_{{ method.name|snake_case }}(transport: str = 'grpc', request_type={{ method.input.ident }}):
256256
client = {{ service.client_name }}(
257-
credentials=credentials.AnonymousCredentials(),
257+
credentials=ga_credentials.AnonymousCredentials(),
258258
transport=transport,
259259
)
260260

@@ -340,7 +340,7 @@ def test_{{ method.name|snake_case }}_from_dict():
340340
{% if method.field_headers and not method.client_streaming %}
341341
def test_{{ method.name|snake_case }}_field_headers():
342342
client = {{ service.client_name }}(
343-
credentials=credentials.AnonymousCredentials(),
343+
credentials=ga_credentials.AnonymousCredentials(),
344344
)
345345

346346
# 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():
385385
{% if method.ident.package != method.input.ident.package %}
386386
def test_{{ method.name|snake_case }}_from_dict():
387387
client = {{ service.client_name }}(
388-
credentials=credentials.AnonymousCredentials(),
388+
credentials=ga_credentials.AnonymousCredentials(),
389389
)
390390
# Mock the actual call within the gRPC stub, and fake the request.
391391
with mock.patch.object(
@@ -414,7 +414,7 @@ def test_{{ method.name|snake_case }}_from_dict():
414414
{% if method.flattened_fields %}
415415
def test_{{ method.name|snake_case }}_flattened():
416416
client = {{ service.client_name }}(
417-
credentials=credentials.AnonymousCredentials(),
417+
credentials=ga_credentials.AnonymousCredentials(),
418418
)
419419

420420
# Mock the actual call within the gRPC stub, and fake the request.
@@ -461,7 +461,7 @@ def test_{{ method.name|snake_case }}_flattened():
461461

462462
def test_{{ method.name|snake_case }}_flattened_error():
463463
client = {{ service.client_name }}(
464-
credentials=credentials.AnonymousCredentials(),
464+
credentials=ga_credentials.AnonymousCredentials(),
465465
)
466466

467467
# Attempting to call a method with both a request object and flattened
@@ -479,7 +479,7 @@ def test_{{ method.name|snake_case }}_flattened_error():
479479
{% if method.paged_result_field %}
480480
def test_{{ method.name|snake_case }}_pager():
481481
client = {{ service.client_name }}(
482-
credentials=credentials.AnonymousCredentials,
482+
credentials=ga_credentials.AnonymousCredentials,
483483
)
484484

485485
# Mock the actual call within the gRPC stub, and fake the request.
@@ -538,7 +538,7 @@ def test_{{ method.name|snake_case }}_pager():
538538

539539
def test_{{ method.name|snake_case }}_pages():
540540
client = {{ service.client_name }}(
541-
credentials=credentials.AnonymousCredentials,
541+
credentials=ga_credentials.AnonymousCredentials,
542542
)
543543

544544
# Mock the actual call within the gRPC stub, and fake the request.
@@ -587,19 +587,19 @@ def test_{{ method.name|snake_case }}_raw_page_lro():
587587
def test_credentials_transport_error():
588588
# It is an error to provide credentials and a transport instance.
589589
transport = transports.{{ service.name }}GrpcTransport(
590-
credentials=credentials.AnonymousCredentials(),
590+
credentials=ga_credentials.AnonymousCredentials(),
591591
)
592592
with pytest.raises(ValueError):
593593
client = {{ service.client_name }}(
594-
credentials=credentials.AnonymousCredentials(),
594+
credentials=ga_credentials.AnonymousCredentials(),
595595
transport=transport,
596596
)
597597

598598

599599
def test_transport_instance():
600600
# A client may be instantiated with a custom transport instance.
601601
transport = transports.{{ service.name }}GrpcTransport(
602-
credentials=credentials.AnonymousCredentials(),
602+
credentials=ga_credentials.AnonymousCredentials(),
603603
)
604604
client = {{ service.client_name }}(transport=transport)
605605
assert client.transport is transport
@@ -608,7 +608,7 @@ def test_transport_instance():
608608
def test_transport_get_channel():
609609
# A client may be instantiated with a custom transport instance.
610610
transport = transports.{{ service.name }}GrpcTransport(
611-
credentials=credentials.AnonymousCredentials(),
611+
credentials=ga_credentials.AnonymousCredentials(),
612612
)
613613
channel = transport.grpc_channel
614614
assert channel
@@ -617,7 +617,7 @@ def test_transport_get_channel():
617617
def test_transport_grpc_default():
618618
# A client should use the gRPC transport by default.
619619
client = {{ service.client_name }}(
620-
credentials=credentials.AnonymousCredentials(),
620+
credentials=ga_credentials.AnonymousCredentials(),
621621
)
622622
assert isinstance(
623623
client.transport,
@@ -629,8 +629,8 @@ def test_transport_grpc_default():
629629
])
630630
def test_transport_adc(transport_class):
631631
# Test default credentials are used if not provided.
632-
with mock.patch.object(auth, 'default') as adc:
633-
adc.return_value = (credentials.AnonymousCredentials(), None)
632+
with mock.patch.object(google.auth, 'default') as adc:
633+
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
634634
transport_class()
635635
adc.assert_called_once()
636636

@@ -640,7 +640,7 @@ def test_{{ service.name|snake_case }}_base_transport():
640640
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:
641641
Transport.return_value = None
642642
transport = transports.{{ service.name }}Transport(
643-
credentials=credentials.AnonymousCredentials(),
643+
credentials=ga_credentials.AnonymousCredentials(),
644644
)
645645

646646
# Every method on the transport should just blindly
@@ -664,17 +664,17 @@ def test_{{ service.name|snake_case }}_base_transport():
664664

665665
def test_{{ service.name|snake_case }}_base_transport_with_adc():
666666
# Test the default credentials are used if credentials and credentials_file are None.
667-
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:
667+
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:
668668
Transport.return_value = None
669-
adc.return_value = (credentials.AnonymousCredentials(), None)
669+
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
670670
transport = transports.{{ service.name }}Transport()
671671
adc.assert_called_once()
672672

673673

674674
def test_{{ service.name|snake_case }}_auth_adc():
675675
# If no credentials are provided, we should use ADC credentials.
676-
with mock.patch.object(auth, 'default') as adc:
677-
adc.return_value = (credentials.AnonymousCredentials(), None)
676+
with mock.patch.object(google.auth, 'default') as adc:
677+
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
678678
{{ service.client_name }}()
679679
adc.assert_called_once_with(scopes=(
680680
{% for scope in service.oauth_scopes %}
@@ -686,8 +686,8 @@ def test_{{ service.name|snake_case }}_auth_adc():
686686
def test_{{ service.name|snake_case }}_transport_auth_adc():
687687
# If credentials and host are not provided, the transport class should use
688688
# ADC credentials.
689-
with mock.patch.object(auth, 'default') as adc:
690-
adc.return_value = (credentials.AnonymousCredentials(), None)
689+
with mock.patch.object(google.auth, 'default') as adc:
690+
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
691691
transports.{{ service.name }}GrpcTransport(host="squid.clam.whelk")
692692
adc.assert_called_once_with(scopes=(
693693
{% for scope in service.oauth_scopes %}
@@ -699,7 +699,7 @@ def test_{{ service.name|snake_case }}_transport_auth_adc():
699699
def test_{{ service.name|snake_case }}_host_no_port():
700700
{% with host = (service.host|default('localhost', true)).split(':')[0] %}
701701
client = {{ service.client_name }}(
702-
credentials=credentials.AnonymousCredentials(),
702+
credentials=ga_credentials.AnonymousCredentials(),
703703
client_options=client_options.ClientOptions(api_endpoint='{{ host }}'),
704704
)
705705
assert client.transport._host == '{{ host }}:443'
@@ -709,7 +709,7 @@ def test_{{ service.name|snake_case }}_host_no_port():
709709
def test_{{ service.name|snake_case }}_host_with_port():
710710
{% with host = (service.host|default('localhost', true)).split(':')[0] %}
711711
client = {{ service.client_name }}(
712-
credentials=credentials.AnonymousCredentials(),
712+
credentials=ga_credentials.AnonymousCredentials(),
713713
client_options=client_options.ClientOptions(api_endpoint='{{ host }}:8000'),
714714
)
715715
assert client.transport._host == '{{ host }}:8000'
@@ -741,9 +741,9 @@ def test_{{ service.name|snake_case }}_transport_channel_mtls_with_client_cert_s
741741
mock_grpc_channel = mock.Mock()
742742
grpc_create_channel.return_value = mock_grpc_channel
743743

744-
cred = credentials.AnonymousCredentials()
744+
cred = ga_credentials.AnonymousCredentials()
745745
with pytest.warns(DeprecationWarning):
746-
with mock.patch.object(auth, 'default') as adc:
746+
with mock.patch.object(google.auth, 'default') as adc:
747747
adc.return_value = (cred, None)
748748
transport = transport_class(
749749
host="squid.clam.whelk",
@@ -820,7 +820,7 @@ def test_{{ service.name|snake_case }}_transport_channel_mtls_with_adc(
820820
{% if service.has_lro %}
821821
def test_{{ service.name|snake_case }}_grpc_lro_client():
822822
client = {{ service.client_name }}(
823-
credentials=credentials.AnonymousCredentials(),
823+
credentials=ga_credentials.AnonymousCredentials(),
824824
transport='grpc',
825825
)
826826
transport = client.transport
@@ -890,15 +890,15 @@ def test_client_withDEFAULT_CLIENT_INFO():
890890

891891
with mock.patch.object(transports.{{ service.name }}Transport, '_prep_wrapped_messages') as prep:
892892
client = {{ service.client_name }}(
893-
credentials=credentials.AnonymousCredentials(),
893+
credentials=ga_credentials.AnonymousCredentials(),
894894
client_info=client_info,
895895
)
896896
prep.assert_called_once_with(client_info)
897897

898898
with mock.patch.object(transports.{{ service.name }}Transport, '_prep_wrapped_messages') as prep:
899899
transport_class = {{ service.client_name }}.get_transport_class()
900900
transport = transport_class(
901-
credentials=credentials.AnonymousCredentials(),
901+
credentials=ga_credentials.AnonymousCredentials(),
902902
client_info=client_info,
903903
)
904904
prep.assert_called_once_with(client_info)

packages/gapic-generator/gapic/schema/metadata.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,16 @@ def __str__(self) -> str:
7474
"""
7575
# Most (but not all) types are in a module.
7676
if self.module:
77+
module_name = self.module
78+
79+
# This module is from a different proto package
80+
# Most commonly happens for a common proto
81+
# https://pypi.org/project/googleapis-common-protos/
82+
if not self.proto_package.startswith(self.api_naming.proto_package):
83+
module_name = f'{self.module}_pb2'
84+
7785
# If collisions are registered and conflict with our module,
7886
# use the module alias instead.
79-
module_name = self.module
8087
if self.module_alias:
8188
module_name = self.module_alias
8289

@@ -170,7 +177,6 @@ def python_import(self) -> imp.Import:
170177
return imp.Import(
171178
package=self.package,
172179
module=f'{self.module}_pb2',
173-
alias=self.module_alias if self.module_alias else self.module,
174180
)
175181

176182
@property

0 commit comments

Comments
 (0)