1818import logging .config
1919import os
2020import re
21+ from typing import Any , Callable , List , Tuple , TypeVar , Union
2122import yaml
2223
2324
4546 "path_to_private_key_file" ,
4647 "delegated_account" ,
4748)
48- from typing import Any , Callable , TypeVar
4949
5050_KEYS_ENV_VARIABLES_MAP : dict [str , str ] = {
5151 key : _ENV_PREFIX + key .upper ()
@@ -71,12 +71,12 @@ def _config_validation_decorator(func: F) -> F:
7171 """
7272
7373 @functools .wraps (func )
74- def validation_wrapper (* args : Any , ** kwargs : Any ) -> Any :
74+ def validation_wrapper (* args : Any , ** kwargs : Any ) -> dict [ str , Any ] :
7575 config_dict : dict [str , Any ] = func (* args , ** kwargs )
7676 validate_dict (config_dict )
7777 return config_dict
7878
79- return validation_wrapper # type: ignore[return-value]
79+ return validation_wrapper
8080
8181
8282def _config_parser_decorator (func : F ) -> F :
@@ -94,17 +94,17 @@ def parser_wrapper(*args: Any, **kwargs: Any) -> Any:
9494 parsed_config : dict [str , Any ] = convert_login_customer_id_to_str (
9595 config_dict
9696 )
97- parsed_config = convert_linked_customer_id_to_str (parsed_config )
97+ parsed_config : dict [ str , Any ] = convert_linked_customer_id_to_str (parsed_config )
9898
99- config_keys = parsed_config .keys ()
99+ config_keys : List [ str ] = parsed_config .keys ()
100100
101101 if "logging" in config_keys :
102- logging_config = parsed_config ["logging" ]
102+ logging_config : dict [ str , Any ] = parsed_config ["logging" ]
103103 # If the logging config is a dict then it is already in the format
104104 # that needs to be returned by this method.
105105 if type (logging_config ) is not dict :
106106 try :
107- parsed_config ["logging" ] = json .loads (logging_config )
107+ parsed_config ["logging" ]: dict [ str , Any ] = json .loads (logging_config )
108108 # The logger is configured here in case deprecation warnings
109109 # need to be logged further down in this method. The logger
110110 # is otherwise configured by the GoogleAdsClient class.
@@ -156,12 +156,12 @@ def parser_wrapper(*args: Any, **kwargs: Any) -> Any:
156156 # variable we need to manually change it to the bool False because
157157 # the string "False" is truthy and can easily be incorrectly
158158 # converted to the boolean True.
159- value = parsed_config .get ("use_proto_plus" , False )
160- parsed_config ["use_proto_plus" ] = disambiguate_string_bool (value )
159+ value : Union [ str , bool ] = parsed_config .get ("use_proto_plus" , False )
160+ parsed_config ["use_proto_plus" ]: bool = disambiguate_string_bool (value )
161161
162162 return parsed_config
163163
164- return parser_wrapper # type: ignore[return-value]
164+ return parser_wrapper
165165
166166
167167def validate_dict (config_data : dict [str , Any ]) -> None :
@@ -178,7 +178,7 @@ def validate_dict(config_data: dict[str, Any]) -> None:
178178 Raises:
179179 ValueError: If the dict does not contain all required config keys.
180180 """
181- if not "use_proto_plus" in config_data .keys ():
181+ if "use_proto_plus" not in config_data .keys ():
182182 raise ValueError (
183183 "The client library configuration is missing the required "
184184 '"use_proto_plus" key. Please set this option to either "True" '
@@ -200,7 +200,7 @@ def validate_dict(config_data: dict[str, Any]) -> None:
200200 validate_linked_customer_id (str (config_data ["linked_customer_id" ]))
201201
202202
203- def _validate_customer_id (customer_id : str | None , id_type : str ) -> None :
203+ def _validate_customer_id (customer_id : Union [ str , None ] , id_type : str ) -> None :
204204 """Validates a customer ID.
205205
206206 Args:
@@ -214,15 +214,15 @@ def _validate_customer_id(customer_id: str | None, id_type: str) -> None:
214214 """
215215 if customer_id is not None :
216216 # Checks that the string is comprised only of 10 digits.
217- pattern = re .compile (r"^\d{10}" , re .ASCII )
217+ pattern : re . Pattern = re .compile (r"^\d{10}" , re .ASCII )
218218 if not pattern .fullmatch (customer_id ):
219219 raise ValueError (
220220 f"The specified { id_type } customer ID is invalid. It must be a "
221221 "ten digit number represented as a string, i.e. '1234567890'"
222222 )
223223
224224
225- def validate_login_customer_id (login_customer_id : str | None ) -> None :
225+ def validate_login_customer_id (login_customer_id : Union [ str , None ] ) -> None :
226226 """Validates a login customer ID.
227227 Args:
228228 login_customer_id: a str from config indicating a login customer ID.
@@ -233,7 +233,7 @@ def validate_login_customer_id(login_customer_id: str | None) -> None:
233233 _validate_customer_id (login_customer_id , "login" )
234234
235235
236- def validate_linked_customer_id (linked_customer_id : str | None ) -> None :
236+ def validate_linked_customer_id (linked_customer_id : Union [ str , None ] ) -> None :
237237 """Validates a linked customer ID.
238238 Args:
239239 linked_customer_id: a str from config indicating a linked customer ID.
@@ -246,7 +246,7 @@ def validate_linked_customer_id(linked_customer_id: str | None) -> None:
246246
247247@_config_validation_decorator
248248@_config_parser_decorator
249- def load_from_yaml_file (path : str | None = None ) -> dict [str , Any ]:
249+ def load_from_yaml_file (path : Union [ str , None ] = None ) -> dict [str , Any ]:
250250 """Loads configuration data from a YAML file and returns it as a dict.
251251
252252 Args:
@@ -264,20 +264,20 @@ def load_from_yaml_file(path: str | None = None) -> dict[str, Any]:
264264 # If no path is specified then we check for the environment variable
265265 # that may define the path. If that is not defined then we use the
266266 # default path.
267- path_from_env_var = os .environ .get (
267+ path_from_env_var : str = os .environ .get (
268268 _ENV_PREFIX + _CONFIG_FILE_PATH_KEY [0 ].upper ()
269269 )
270- path = (
270+ path : Tuple [ str ] = (
271271 path_from_env_var
272272 if path_from_env_var
273273 else os .path .join (os .path .expanduser ("~" ), "google-ads.yaml" )
274274 )
275275
276276 if not os .path .isabs (path ):
277- path = os .path .expanduser (path )
277+ path : str = os .path .expanduser (path )
278278
279279 with open (path , "rb" ) as handle :
280- yaml_doc = handle .read ()
280+ yaml_doc : bytes = handle .read ()
281281
282282 return parse_yaml_document_to_dict (yaml_doc )
283283
@@ -308,7 +308,7 @@ def load_from_dict(config_dict: dict[str, Any]) -> dict[str, Any]:
308308
309309@_config_validation_decorator
310310@_config_parser_decorator
311- def parse_yaml_document_to_dict (yaml_doc : str | bytes ) -> dict [str , Any ]:
311+ def parse_yaml_document_to_dict (yaml_doc : Union [ str , bytes ] ) -> dict [str , Any ]:
312312 """Parses a YAML document to a dict.
313313
314314 Args:
@@ -382,10 +382,10 @@ def convert_login_customer_id_to_str(
382382 Returns:
383383 The same config dict object with a mutated login_customer_id attr.
384384 """
385- login_customer_id : Any = config_data .get ("login_customer_id" )
385+ login_customer_id : str = config_data .get ("login_customer_id" )
386386
387387 if login_customer_id :
388- config_data ["login_customer_id" ] = str (login_customer_id )
388+ config_data ["login_customer_id" ]: str = str (login_customer_id )
389389
390390 return config_data
391391
@@ -405,15 +405,15 @@ def convert_linked_customer_id_to_str(
405405 Returns:
406406 The same config dict object with a mutated linked_customer_id attr.
407407 """
408- linked_customer_id : Any = config_data .get ("linked_customer_id" )
408+ linked_customer_id : str = config_data .get ("linked_customer_id" )
409409
410410 if linked_customer_id :
411- config_data ["linked_customer_id" ] = str (linked_customer_id )
411+ config_data ["linked_customer_id" ]: str = str (linked_customer_id )
412412
413413 return config_data
414414
415415
416- def disambiguate_string_bool (value : str | bool ) -> bool :
416+ def disambiguate_string_bool (value : Union [ str , bool ] ) -> bool :
417417 """Converts a stringified boolean to its bool representation.
418418
419419 Args:
0 commit comments