Skip to content

Commit c538816

Browse files
committed
Partially update client.py annotations.
Change-Id: Id71fb41c9a190b37e6e7865c862e8865ddd04005
1 parent 1f44f0d commit c538816

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

google/ads/googleads/client.py

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@
1717
import logging.config
1818

1919
from google.api_core.gapic_v1.client_info import ClientInfo
20-
import grpc.experimental
20+
import grpc
2121
from proto.enums import ProtoEnumMeta
2222

23+
from google.protobuf.message import Message as ProtobufMessageType
24+
from proto import Message as ProtoPlusMessageType
25+
2326
from google.ads.googleads import config, oauth2, util
2427
from google.ads.googleads.interceptors import (
2528
MetadataInterceptor,
2629
ExceptionInterceptor,
2730
LoggingInterceptor,
2831
)
2932

33+
from types import ModuleType
34+
from typing import Any, Dict, Tuple, Union
35+
3036
_logger = logging.getLogger(__name__)
3137

3238
_SERVICE_CLIENT_TEMPLATE = "{}Client"
@@ -60,18 +66,18 @@ class _EnumGetter:
6066
class instances when accessed.
6167
"""
6268

63-
def __init__(self, client: "GoogleAdsClient"):
69+
def __init__(self, client: "GoogleAdsClient") -> None:
6470
"""Initializer for the _EnumGetter class.
6571
6672
Args:
6773
client: An instance of the GoogleAdsClient class.
6874
"""
6975
self._client: "GoogleAdsClient" = client
7076
self._version: str = client.version or _DEFAULT_VERSION
71-
self._enums: list[str] | None = None
77+
self._enums: Tuple[str] | None = None
7278
self._use_proto_plus: bool = client.use_proto_plus
7379

74-
def __dir__(self) -> list[str]:
80+
def __dir__(self) -> Tuple[str]:
7581
"""Overrides behavior when dir() is called on instances of this class.
7682
7783
It's useful to use dir() to see a list of available attributes. Since
@@ -85,7 +91,7 @@ def __dir__(self) -> list[str]:
8591

8692
return self._enums
8793

88-
def __getattr__(self, name: str):
94+
def __getattr__(self, name: str) -> Union[ProtoPlusMessageType, ProtobufMessageType]:
8995
"""Dynamically loads the given enum class instance.
9096
9197
Args:
@@ -113,7 +119,7 @@ def __getattr__(self, name: str):
113119
f"'{type(self).__name__}' object has no attribute '{name}'"
114120
)
115121

116-
def __getstate__(self):
122+
def __getstate__(self) -> Dict[str, Any]:
117123
"""Returns self serialized as a dict.
118124
119125
Since this class overrides __getattr__ we define this method to help
@@ -125,7 +131,7 @@ def __getstate__(self):
125131
"""
126132
return self.__dict__.copy()
127133

128-
def __setstate__(self, d):
134+
def __setstate__(self, d: Dict[str, Any]) -> None:
129135
"""Deserializes self with the given dictionary.
130136
131137
Since this class overrides __getattr__ we define this method to help
@@ -142,7 +148,11 @@ class GoogleAdsClient:
142148
"""Google Ads client used to configure settings and fetch services."""
143149

144150
@classmethod
145-
def copy_from(cls, destination, origin): # type: ignore[no-untyped-def]
151+
def copy_from(
152+
cls,
153+
destination: Union[ProtoPlusMessageType, ProtobufMessageType],
154+
origin: Union[ProtoPlusMessageType, ProtobufMessageType]
155+
) -> Union[ProtoPlusMessageType, ProtobufMessageType]:
146156
"""Copies protobuf and proto-plus messages into one-another.
147157
148158
This method consolidates the CopyFrom logic of protobuf and proto-plus
@@ -156,7 +166,7 @@ def copy_from(cls, destination, origin): # type: ignore[no-untyped-def]
156166
return util.proto_copy_from(destination, origin)
157167

158168
@classmethod
159-
def _get_client_kwargs(cls, config_data: dict) -> dict:
169+
def _get_client_kwargs(cls, config_data: Dict[str, Any]) -> Dict[str, Any]:
160170
"""Converts configuration dict into kwargs required by the client.
161171
162172
Args:
@@ -170,7 +180,7 @@ def _get_client_kwargs(cls, config_data: dict) -> dict:
170180
ValueError: If the configuration lacks a required field.
171181
"""
172182
return {
173-
"credentials": oauth2.get_credentials(config_data), # type: ignore[no-untyped-call]
183+
"credentials": oauth2.get_credentials(config_data),
174184
"developer_token": config_data.get("developer_token"),
175185
"endpoint": config_data.get("endpoint"),
176186
"login_customer_id": config_data.get("login_customer_id"),
@@ -184,7 +194,7 @@ def _get_client_kwargs(cls, config_data: dict) -> dict:
184194
}
185195

186196
@classmethod
187-
def _get_api_services_by_version(cls, version: str): # type: ignore[no-untyped-def]
197+
def _get_api_services_by_version(cls, version: str) -> ModuleType:
188198
"""Returns a module with all services and types for a given API version.
189199
190200
Args:
@@ -207,7 +217,7 @@ def _get_api_services_by_version(cls, version: str): # type: ignore[no-untyped-
207217

208218
@classmethod
209219
def load_from_env(
210-
cls, version: str | None = None
220+
cls, version: Union[str, None] = None
211221
) -> "GoogleAdsClient":
212222
"""Creates a GoogleAdsClient with data stored in the env variables.
213223
@@ -221,13 +231,13 @@ def load_from_env(
221231
Raises:
222232
ValueError: If the configuration lacks a required field.
223233
"""
224-
config_data: dict = config.load_from_env() # type: ignore[no-untyped-call]
225-
kwargs: dict = cls._get_client_kwargs(config_data)
234+
config_data: Dict[str, Any] = config.load_from_env()
235+
kwargs: Dict[str, Any] = cls._get_client_kwargs(config_data)
226236
return cls(**dict(version=version, **kwargs))
227237

228238
@classmethod
229239
def load_from_string(
230-
cls, yaml_str: str, version: str | None = None
240+
cls, yaml_str: str, version: Union[str, None] = None
231241
) -> "GoogleAdsClient":
232242
"""Creates a GoogleAdsClient with data stored in the YAML string.
233243
@@ -243,13 +253,13 @@ def load_from_string(
243253
Raises:
244254
ValueError: If the configuration lacks a required field.
245255
"""
246-
config_data: dict = config.parse_yaml_document_to_dict(yaml_str) # type: ignore[no-untyped-call]
247-
kwargs: dict = cls._get_client_kwargs(config_data)
256+
config_data: Dict[str, Any] = config.parse_yaml_document_to_dict(yaml_str)
257+
kwargs: Dict[str, Any] = cls._get_client_kwargs(config_data)
248258
return cls(**dict(version=version, **kwargs))
249259

250260
@classmethod
251261
def load_from_dict(
252-
cls, config_dict: dict, version: str | None = None
262+
cls, config_dict: Dict[str, Any], version: Union[str, None] = None
253263
) -> "GoogleAdsClient":
254264
"""Creates a GoogleAdsClient with data stored in the config_dict.
255265
@@ -265,13 +275,13 @@ def load_from_dict(
265275
Raises:
266276
ValueError: If the configuration lacks a required field.
267277
"""
268-
config_data: dict = config.load_from_dict(config_dict) # type: ignore[no-untyped-call]
269-
kwargs: dict = cls._get_client_kwargs(config_data)
278+
config_data: Dict[str, Any] = config.load_from_dict(config_dict)
279+
kwargs: Dict[str, Any] = cls._get_client_kwargs(config_data)
270280
return cls(**dict(version=version, **kwargs))
271281

272282
@classmethod
273283
def load_from_storage(
274-
cls, path: str | None = None, version: str | None = None
284+
cls, path: Union[str, None] = None, version: Union[str, None] = None
275285
) -> "GoogleAdsClient":
276286
"""Creates a GoogleAdsClient with data stored in the specified file.
277287
@@ -289,22 +299,22 @@ def load_from_storage(
289299
IOError: If the configuration file can't be loaded.
290300
ValueError: If the configuration file lacks a required field.
291301
"""
292-
config_data: dict = config.load_from_yaml_file(path) # type: ignore[no-untyped-call]
293-
kwargs: dict = cls._get_client_kwargs(config_data)
302+
config_data: Dict[str, Any] = config.load_from_yaml_file(path)
303+
kwargs: Dict[str, Any] = cls._get_client_kwargs(config_data)
294304
return cls(**dict(version=version, **kwargs))
295305

296306
def __init__(
297307
self,
298-
credentials, # type: ignore[no-untyped-def]
308+
credentials: Dict[str, Any],
299309
developer_token: str,
300-
endpoint: str | None = None,
301-
login_customer_id: str | None = None,
302-
logging_config: dict | None = None,
303-
linked_customer_id: str | None = None,
304-
version: str | None = None,
305-
http_proxy: str | None = None,
310+
endpoint: Union[str, None] = None,
311+
login_customer_id: Union[str, None] = None,
312+
logging_config: Union[Dict[str, Any], None] = None,
313+
linked_customer_id: Union[str, None] = None,
314+
version: Union[str, None] = None,
315+
http_proxy: Union[str, None] = None,
306316
use_proto_plus: bool = False,
307-
use_cloud_org_for_api_access: str | None = None,
317+
use_cloud_org_for_api_access: Union[str, None] = None,
308318
):
309319
"""Initializer for the GoogleAdsClient.
310320
@@ -328,15 +338,15 @@ def __init__(
328338
if logging_config:
329339
logging.config.dictConfig(logging_config)
330340

331-
self.credentials = credentials
341+
self.credentials: Dict[str, Any] = credentials
332342
self.developer_token: str = developer_token
333-
self.endpoint: str | None = endpoint
334-
self.login_customer_id: str | None = login_customer_id
335-
self.linked_customer_id: str | None = linked_customer_id
336-
self.version: str | None = version
337-
self.http_proxy: str | None = http_proxy
343+
self.endpoint: Union[str, None] = endpoint
344+
self.login_customer_id: Union[str, None] = login_customer_id
345+
self.linked_customer_id: Union[str, None] = linked_customer_id
346+
self.version: Union[str, None] = version
347+
self.http_proxy: Union[str, None] = http_proxy
338348
self.use_proto_plus: bool = use_proto_plus
339-
self.use_cloud_org_for_api_access: str | None = (
349+
self.use_cloud_org_for_api_access: Union[str, None] = (
340350
use_cloud_org_for_api_access
341351
)
342352
self.enums: _EnumGetter = _EnumGetter(self)
@@ -345,12 +355,12 @@ def __init__(
345355
if http_proxy:
346356
_GRPC_CHANNEL_OPTIONS.append(("grpc.http_proxy", http_proxy))
347357

348-
def get_service( # type: ignore[no-untyped-def]
358+
def get_service(
349359
self,
350360
name: str,
351361
version: str = _DEFAULT_VERSION,
352-
interceptors: list | None = None,
353-
):
362+
interceptors: Union[list, None] = None,
363+
) -> Any:
354364
"""Returns a service client instance for the specified service_name.
355365
356366
Args:
@@ -376,12 +386,12 @@ def get_service( # type: ignore[no-untyped-def]
376386
services_path: str = (
377387
f"google.ads.googleads.{version}.services.services"
378388
)
379-
snaked: str = util.convert_upper_case_to_snake_case(name) # type: ignore[no-untyped-call]
389+
snaked: str = util.convert_upper_case_to_snake_case(name)
380390
interceptors = interceptors or []
381391

382392
try:
383-
service_module = import_module(f"{services_path}.{snaked}")
384-
service_client_class = util.get_nested_attr( # type: ignore[no-untyped-call]
393+
service_module: Any = import_module(f"{services_path}.{snaked}")
394+
service_client_class: Any = util.get_nested_attr(
385395
service_module, _SERVICE_CLIENT_TEMPLATE.format(name)
386396
)
387397
except (AttributeError, ModuleNotFoundError):
@@ -390,21 +400,21 @@ def get_service( # type: ignore[no-untyped-def]
390400
"Ads API {}.".format(name, version)
391401
)
392402

393-
service_transport_class = service_client_class.get_transport_class()
403+
service_transport_class: Any = service_client_class.get_transport_class()
394404

395-
endpoint = (
405+
endpoint: str = (
396406
self.endpoint
397407
if self.endpoint
398408
else service_client_class.DEFAULT_ENDPOINT
399409
)
400410

401-
channel = service_transport_class.create_channel(
411+
channel: grpc.Channel = service_transport_class.create_channel(
402412
host=endpoint,
403413
credentials=self.credentials,
404414
options=_GRPC_CHANNEL_OPTIONS,
405415
)
406416

407-
interceptors = interceptors + [
417+
interceptors: List[grpc.UnaryUnaryClientInterceptor, grpc.UnaryStreamClientInterceptor] = interceptors + [
408418
MetadataInterceptor( # type: ignore[no-untyped-call]
409419
self.developer_token,
410420
self.login_customer_id,

0 commit comments

Comments
 (0)