Skip to content

Commit e381237

Browse files
Add type hints and annotations to examples/misc
This change adds type hints and annotations to the Python files in the examples/misc directory to improve code readability and maintainability.
1 parent 4f350a8 commit e381237

File tree

4 files changed

+75
-37
lines changed

4 files changed

+75
-37
lines changed

examples/misc/add_ad_group_image_asset.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@
2323

2424
from google.ads.googleads.client import GoogleAdsClient
2525
from google.ads.googleads.errors import GoogleAdsException
26+
from google.ads.googleads.v19.services.types.ad_group_asset_service import AdGroupAssetServiceClient
27+
from google.ads.googleads.v19.services.types.ad_group_asset_service import MutateAdGroupAssetsResponse
28+
from google.ads.googleads.v19.services.types.google_ads_service import GoogleAdsServiceClient
2629
from google.api_core import protobuf_helpers
2730

2831

29-
def main(client, customer_id, ad_group_id, asset_id):
30-
ad_group_asset_service = client.get_service("AdGroupAssetService")
31-
ad_group_asset_resource_name = ad_group_asset_service.asset_path(
32+
def main(
33+
client: GoogleAdsClient,
34+
customer_id: str,
35+
ad_group_id: str,
36+
asset_id: str,
37+
) -> None:
38+
ad_group_asset_service: AdGroupAssetServiceClient = client.get_service(
39+
"AdGroupAssetService"
40+
)
41+
ad_group_asset_resource_name: str = ad_group_asset_service.asset_path(
3242
customer_id, asset_id
3343
)
3444

@@ -39,8 +49,10 @@ def main(client, customer_id, ad_group_id, asset_id):
3949
ad_group_asset_set.ad_group = ad_group_asset_service.ad_group_path(
4050
customer_id, ad_group_id
4151
)
42-
response = ad_group_asset_service.mutate_ad_group_assets(
43-
customer_id=customer_id, operations=[ad_group_asset_operation]
52+
response: MutateAdGroupAssetsResponse = (
53+
ad_group_asset_service.mutate_ad_group_assets(
54+
customer_id=customer_id, operations=[ad_group_asset_operation]
55+
)
4456
)
4557

4658
for result in response.results:

examples/misc/campaign_report_to_csv.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,32 @@
3333

3434
from google.ads.googleads.client import GoogleAdsClient
3535
from google.ads.googleads.errors import GoogleAdsException
36+
from google.ads.googleads.v19.services.services.google_ads_service import GoogleAdsServiceClient
37+
from google.ads.googleads.v19.services.types.google_ads_service import SearchGoogleAdsStreamRequest
38+
from google.api_core.call import GrpcStream
3639

3740

3841
_DEFAULT_FILE_NAME = "campaign_report_to_csv_results.csv"
3942

4043

41-
def main(client, customer_id, output_file, write_headers):
44+
def main(
45+
client: GoogleAdsClient,
46+
customer_id: str,
47+
output_file: str,
48+
write_headers: bool,
49+
) -> None:
4250
"""Writes rows returned from a search_stream request to a CSV file.
4351
Args:
4452
client: An initialized GoogleAdsClient instance.
4553
customer_id (str): The client customer ID string.
4654
output_file (str): Filename of the file to write the report data to.
4755
write_headers (bool): From argparse, True if arg is provided.
4856
"""
49-
file_dir = os.path.dirname(os.path.abspath(__file__))
50-
file_path = os.path.join(file_dir, output_file)
51-
ga_service = client.get_service("GoogleAdsService")
57+
file_dir: str = os.path.dirname(os.path.abspath(__file__))
58+
file_path: str = os.path.join(file_dir, output_file)
59+
ga_service: GoogleAdsServiceClient = client.get_service("GoogleAdsService")
5260

53-
query = """
61+
query: str = """
5462
SELECT
5563
customer.descriptive_name,
5664
segments.date,
@@ -65,16 +73,18 @@ def main(client, customer_id, output_file, write_headers):
6573
LIMIT 25"""
6674

6775
# Issues a search request using streaming.
68-
search_request = client.get_type("SearchGoogleAdsStreamRequest")
76+
search_request: SearchGoogleAdsStreamRequest = client.get_type(
77+
"SearchGoogleAdsStreamRequest"
78+
)
6979
search_request.customer_id = customer_id
7080
search_request.query = query
71-
stream = ga_service.search_stream(search_request)
81+
stream: GrpcStream = ga_service.search_stream(search_request)
7282

7383
with open(file_path, "w", newline="") as f:
74-
writer = csv.writer(f)
84+
writer: csv.writer = csv.writer(f)
7585

7686
# Define a list of headers for the first row.
77-
headers = [
87+
headers: list[str] = [
7888
"Account",
7989
"Date",
8090
"Campaign",

examples/misc/set_custom_client_timeouts.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,44 @@
2828

2929
from google.ads.googleads.client import GoogleAdsClient
3030
from google.ads.googleads.errors import GoogleAdsException
31+
from google.ads.googleads.v19.services.services.google_ads_service import GoogleAdsServiceClient
32+
from google.ads.googleads.v19.services.types.google_ads_service import SearchGoogleAdsRequest
33+
from google.ads.googleads.v19.services.types.google_ads_service import SearchGoogleAdsStreamRequest
34+
from google.ads.googleads.v19.services.types.google_ads_service import SearchGoogleAdsStreamResponse
3135
from google.api_core.exceptions import DeadlineExceeded
3236
from google.api_core.retry import Retry
3337

3438

35-
_CLIENT_TIMEOUT_SECONDS = 5 * 60 # 5 minutes.
36-
_QUERY = "SELECT campaign.id FROM campaign"
39+
_CLIENT_TIMEOUT_SECONDS: int = 5 * 60 # 5 minutes.
40+
_QUERY: str = "SELECT campaign.id FROM campaign"
3741

3842

39-
def main(client, customer_id):
43+
def main(client: GoogleAdsClient, customer_id: str) -> None:
4044
"""Main method, to run this code example as a standalone application."""
4145
make_server_streaming_call(client, customer_id)
4246
make_unary_call(client, customer_id)
4347

4448

4549
# [START set_custom_client_timeouts]
46-
def make_server_streaming_call(client, customer_id):
50+
def make_server_streaming_call(
51+
client: GoogleAdsClient, customer_id: str
52+
) -> None:
4753
"""Makes a server streaming call using a custom client timeout.
4854
4955
Args:
5056
client: An initialized GoogleAds client.
5157
customer_id: The str Google Ads customer ID.
5258
"""
53-
ga_service = client.get_service("GoogleAdsService")
54-
campaign_ids = []
59+
ga_service: GoogleAdsServiceClient = client.get_service("GoogleAdsService")
60+
campaign_ids: list[str] = []
5561

5662
try:
57-
search_request = client.get_type("SearchGoogleAdsStreamRequest")
63+
search_request: SearchGoogleAdsStreamRequest = client.get_type(
64+
"SearchGoogleAdsStreamRequest"
65+
)
5866
search_request.customer_id = customer_id
5967
search_request.query = _QUERY
60-
stream = ga_service.search_stream(
68+
stream: SearchGoogleAdsStreamResponse = ga_service.search_stream(
6169
request=search_request,
6270
# When making any request, an optional "timeout" parameter can be
6371
# provided to specify a client-side response deadline in seconds.
@@ -72,7 +80,7 @@ def make_server_streaming_call(client, customer_id):
7280
campaign_ids.append(row.campaign.id)
7381

7482
print("The server streaming call completed before the timeout.")
75-
except DeadlineExceeded as ex:
83+
except DeadlineExceeded:
7684
print("The server streaming call did not complete before the timeout.")
7785
sys.exit(1)
7886
except GoogleAdsException as ex:
@@ -92,21 +100,23 @@ def make_server_streaming_call(client, customer_id):
92100

93101

94102
# [START set_custom_client_timeouts_1]
95-
def make_unary_call(client, customer_id):
103+
def make_unary_call(client: GoogleAdsClient, customer_id: str) -> None:
96104
"""Makes a unary call using a custom client timeout.
97105
98106
Args:
99107
client: An initialized GoogleAds client.
100108
customer_id: The Google Ads customer ID.
101109
"""
102-
ga_service = client.get_service("GoogleAdsService")
103-
campaign_ids = []
110+
ga_service: GoogleAdsServiceClient = client.get_service("GoogleAdsService")
111+
campaign_ids: list[str] = []
104112

105113
try:
106-
search_request = client.get_type("SearchGoogleAdsRequest")
114+
search_request: SearchGoogleAdsRequest = client.get_type(
115+
"SearchGoogleAdsRequest"
116+
)
107117
search_request.customer_id = customer_id
108118
search_request.query = _QUERY
109-
results = ga_service.search(
119+
results: SearchGoogleAdsStreamResponse = ga_service.search(
110120
request=search_request,
111121
# When making any request, an optional "retry" parameter can be
112122
# provided to specify its retry behavior. Complete information about
@@ -134,7 +144,7 @@ def make_unary_call(client, customer_id):
134144
campaign_ids.append(row.campaign.id)
135145

136146
print("The unary call completed before the timeout.")
137-
except DeadlineExceeded as ex:
147+
except DeadlineExceeded:
138148
print("The unary call did not complete before the timeout.")
139149
sys.exit(1)
140150
except GoogleAdsException as ex:

examples/misc/upload_image_asset.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@
2323

2424
from google.ads.googleads.client import GoogleAdsClient
2525
from google.ads.googleads.errors import GoogleAdsException
26+
from google.ads.googleads.v19.services.services.asset_service import AssetServiceClient
27+
from google.ads.googleads.v19.services.types.asset_service import MutateAssetsResponse
28+
from google.ads.googleads.v19.resources.types.asset import Asset
29+
from google.ads.googleads.v19.services.types.asset_service import AssetOperation
2630
from examples.utils.example_helpers import get_image_bytes_from_url
2731

2832

2933
# [START upload_image_asset]
30-
def main(client, customer_id):
34+
def main(client: GoogleAdsClient, customer_id: str) -> None:
3135
"""Main method, to run this code example as a standalone application."""
3236

3337
# Download image from URL
34-
url = "https://gaagl.page.link/Eit5"
35-
image_content = get_image_bytes_from_url(url)
38+
url: str = "https://gaagl.page.link/Eit5"
39+
image_content: bytes = get_image_bytes_from_url(url)
3640

37-
asset_service = client.get_service("AssetService")
38-
asset_operation = client.get_type("AssetOperation")
39-
asset = asset_operation.create
41+
asset_service: AssetServiceClient = client.get_service("AssetService")
42+
asset_operation: AssetOperation = client.get_type("AssetOperation")
43+
asset: Asset = asset_operation.create
4044
asset.type_ = client.enums.AssetTypeEnum.IMAGE
4145
asset.image_asset.data = image_content
4246
asset.image_asset.file_size = len(image_content)
@@ -50,8 +54,10 @@ def main(client, customer_id):
5054
# name, the new name will be dropped silently.
5155
asset.name = "Marketing Image"
5256

53-
mutate_asset_response = asset_service.mutate_assets(
54-
customer_id=customer_id, operations=[asset_operation]
57+
mutate_asset_response: MutateAssetsResponse = (
58+
asset_service.mutate_assets(
59+
customer_id=customer_id, operations=[asset_operation]
60+
)
5561
)
5662
print("Uploaded file(s):")
5763
for row in mutate_asset_response.results:

0 commit comments

Comments
 (0)