Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 93f34e8

Browse files
fix: Fix client template type hints (#593)
Minor fix and cosmetic changes in client.py.j2 template
1 parent ea6ca42 commit 93f34e8

2 files changed

Lines changed: 36 additions & 36 deletions

File tree

  • gapic
    • ads-templates/%namespace/%name/%version/%sub/services/%service
    • templates/%namespace/%name_%version/%sub/services/%service

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ from collections import OrderedDict
55
from distutils import util
66
import os
77
import re
8-
from typing import Callable, Dict, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union
8+
from typing import Callable, Dict, Optional, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union
99
import pkg_resources
1010

11-
import google.api_core.client_options as ClientOptions # type: ignore
12-
from google.api_core import exceptions # type: ignore
13-
from google.api_core import gapic_v1 # type: ignore
14-
from google.api_core import retry as retries # type: ignore
15-
from google.auth import credentials # type: ignore
16-
from google.auth.transport import mtls # type: ignore
17-
from google.auth.transport.grpc import SslCredentials # type: ignore
18-
from google.auth.exceptions import MutualTLSChannelError # type: ignore
19-
from google.oauth2 import service_account # type: ignore
11+
from google.api_core import client_options as client_options_lib # type: ignore
12+
from google.api_core import exceptions # type: ignore
13+
from google.api_core import gapic_v1 # type: ignore
14+
from google.api_core import retry as retries # type: ignore
15+
from google.auth import credentials # type: ignore
16+
from google.auth.transport import mtls # type: ignore
17+
from google.auth.transport.grpc import SslCredentials # type: ignore
18+
from google.auth.exceptions import MutualTLSChannelError # type: ignore
19+
from google.oauth2 import service_account # type: ignore
2020

2121
{% filter sort_lines -%}
2222
{% for method in service.methods.values() -%}
@@ -134,9 +134,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
134134
{% endfor %}
135135

136136
def __init__(self, *,
137-
credentials: credentials.Credentials = None,
138-
transport: Union[str, {{ service.name }}Transport] = None,
139-
client_options: ClientOptions = None,
137+
credentials: Optional[credentials.Credentials] = None,
138+
transport: Union[str, {{ service.name }}Transport, None] = None,
139+
client_options: Optional[client_options_lib.ClientOptions] = None,
140140
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
141141
) -> None:
142142
"""Instantiate the {{ (service.client_name|snake_case).replace('_', ' ') }}.
@@ -150,8 +150,8 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
150150
transport (Union[str, ~.{{ service.name }}Transport]): The
151151
transport to use. If set to None, a transport is chosen
152152
automatically.
153-
client_options (ClientOptions): Custom options for the client. It
154-
won't take effect unless ``transport`` is None.
153+
client_options (client_options_lib.ClientOptions): Custom options for the
154+
client. It won't take effect if a ``transport`` instance is provided.
155155
(1) The ``api_endpoint`` property can be used to override the
156156
default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT
157157
environment variable can also be used to override the endpoint:
@@ -171,19 +171,19 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
171171
API requests. If ``None``, then default info will be used.
172172
Generally, you only need to set this if you're developing
173173
your own client library.
174-
174+
175175
Raises:
176176
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
177177
creation failed for any reason.
178178
"""
179179
if isinstance(client_options, dict):
180-
client_options = ClientOptions.from_dict(client_options)
180+
client_options = client_options_lib.from_dict(client_options)
181181
if client_options is None:
182-
client_options = ClientOptions.ClientOptions()
182+
client_options = client_options_lib.ClientOptions()
183183

184184
# Create SSL credentials for mutual TLS if needed.
185185
use_client_cert = bool(util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")))
186-
186+
187187
ssl_credentials = None
188188
is_mtls = False
189189
if use_client_cert:

gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ from collections import OrderedDict
55
from distutils import util
66
import os
77
import re
8-
from typing import Callable, Dict, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union
8+
from typing import Callable, Dict, Optional, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union
99
import pkg_resources
1010

11-
import google.api_core.client_options as ClientOptions # type: ignore
12-
from google.api_core import exceptions # type: ignore
13-
from google.api_core import gapic_v1 # type: ignore
14-
from google.api_core import retry as retries # type: ignore
15-
from google.auth import credentials # type: ignore
16-
from google.auth.transport import mtls # type: ignore
17-
from google.auth.transport.grpc import SslCredentials # type: ignore
18-
from google.auth.exceptions import MutualTLSChannelError # type: ignore
19-
from google.oauth2 import service_account # type: ignore
11+
from google.api_core import client_options as client_options_lib # type: ignore
12+
from google.api_core import exceptions # type: ignore
13+
from google.api_core import gapic_v1 # type: ignore
14+
from google.api_core import retry as retries # type: ignore
15+
from google.auth import credentials # type: ignore
16+
from google.auth.transport import mtls # type: ignore
17+
from google.auth.transport.grpc import SslCredentials # type: ignore
18+
from google.auth.exceptions import MutualTLSChannelError # type: ignore
19+
from google.oauth2 import service_account # type: ignore
2020

2121
{% filter sort_lines -%}
2222
{% for method in service.methods.values() -%}
@@ -140,9 +140,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
140140
{% endfor %}
141141

142142
def __init__(self, *,
143-
credentials: credentials.Credentials = None,
144-
transport: Union[str, {{ service.name }}Transport] = None,
145-
client_options: ClientOptions = None,
143+
credentials: Optional[credentials.Credentials] = None,
144+
transport: Union[str, {{ service.name }}Transport, None] = None,
145+
client_options: Optional[client_options_lib.ClientOptions] = None,
146146
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
147147
) -> None:
148148
"""Instantiate the {{ (service.client_name|snake_case).replace('_', ' ') }}.
@@ -156,8 +156,8 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
156156
transport (Union[str, ~.{{ service.name }}Transport]): The
157157
transport to use. If set to None, a transport is chosen
158158
automatically.
159-
client_options (ClientOptions): Custom options for the client. It
160-
won't take effect if a ``transport`` instance is provided.
159+
client_options (client_options_lib.ClientOptions): Custom options for the
160+
client. It won't take effect if a ``transport`` instance is provided.
161161
(1) The ``api_endpoint`` property can be used to override the
162162
default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT
163163
environment variable can also be used to override the endpoint:
@@ -183,9 +183,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
183183
creation failed for any reason.
184184
"""
185185
if isinstance(client_options, dict):
186-
client_options = ClientOptions.from_dict(client_options)
186+
client_options = client_options_lib.from_dict(client_options)
187187
if client_options is None:
188-
client_options = ClientOptions.ClientOptions()
188+
client_options = client_options_lib.ClientOptions()
189189

190190
# Create SSL credentials for mutual TLS if needed.
191191
use_client_cert = bool(util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")))

0 commit comments

Comments
 (0)