Skip to content

Commit 26f705c

Browse files
google-labs-jules[bot]BenRKarl
authored andcommitted
Add type hints to files in examples/advanced_operations
This commit introduces type hints to several files within the examples/advanced_operations directory for Google Ads API v19. The following files have been updated: - examples/advanced_operations/add_ad_customizer.py - examples/advanced_operations/add_ad_group_bid_modifier.py - examples/advanced_operations/add_app_campaign.py - examples/advanced_operations/add_bidding_data_exclusion.py - examples/advanced_operations/add_bidding_seasonality_adjustment.py - examples/advanced_operations/add_call_ad.py - examples/advanced_operations/add_demand_gen_campaign.py - examples/advanced_operations/add_display_upload_ad.py Changes include adding type hints for function arguments, return values, and relevant local variables. Necessary types were imported from the `typing` module and `google.ads.googleads.client`. In `add_display_upload_ad.py`, an `argparse` type for `ad_group_id` was corrected from `int` to `str` to match its usage.
1 parent d114d19 commit 26f705c

18 files changed

+319
-220
lines changed

examples/advanced_operations/add_ad_customizer.py

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,28 @@
2121

2222
import argparse
2323
import sys
24+
from typing import Any
2425
from uuid import uuid4
2526

2627
from google.ads.googleads.client import GoogleAdsClient
2728
from google.ads.googleads.errors import GoogleAdsException
2829

2930

30-
def main(client, customer_id, ad_group_id):
31+
def main(client: GoogleAdsClient, customer_id: str, ad_group_id: str) -> None:
3132
"""The main method that creates all necessary entities for the example.
3233
3334
Args:
3435
client: an initialized GoogleAdsClient instance.
3536
customer_id: a client customer ID.
3637
ad_group_id: an ad group ID.
3738
"""
38-
text_customizer_name = f"Planet_{uuid4().hex[:8]}"
39-
price_customizer_name = f"Price_{uuid4().hex[:8]}"
39+
text_customizer_name: str = f"Planet_{uuid4().hex[:8]}"
40+
price_customizer_name: str = f"Price_{uuid4().hex[:8]}"
4041

41-
text_customizer_resource_name = create_text_customizer_attribute(
42+
text_customizer_resource_name: str = create_text_customizer_attribute(
4243
client, customer_id, text_customizer_name
4344
)
44-
price_customizer_resource_name = create_price_customizer_attribute(
45+
price_customizer_resource_name: str = create_price_customizer_attribute(
4546
client, customer_id, price_customizer_name
4647
)
4748
link_customizer_attributes(
@@ -61,7 +62,9 @@ def main(client, customer_id, ad_group_id):
6162

6263

6364
# [START add_ad_customizer]
64-
def create_text_customizer_attribute(client, customer_id, customizer_name):
65+
def create_text_customizer_attribute(
66+
client: GoogleAdsClient, customer_id: str, customizer_name: str
67+
) -> str:
6568
"""Creates a text customizer attribute and returns its resource name.
6669
6770
Args:
@@ -72,22 +75,22 @@ def create_text_customizer_attribute(client, customer_id, customizer_name):
7275
Returns:
7376
a resource name for a text customizer attribute.
7477
"""
75-
customizer_attribute_service = client.get_service(
78+
customizer_attribute_service: Any = client.get_service(
7679
"CustomizerAttributeService"
7780
)
7881

7982
# Creates a text customizer attribute. The customizer attribute name is
8083
# arbitrary and will be used as a placeholder in the ad text fields.
81-
operation = client.get_type("CustomizerAttributeOperation")
82-
text_attribute = operation.create
84+
operation: Any = client.get_type("CustomizerAttributeOperation")
85+
text_attribute: Any = operation.create
8386
text_attribute.name = customizer_name
8487
text_attribute.type_ = client.enums.CustomizerAttributeTypeEnum.TEXT
8588

86-
response = customizer_attribute_service.mutate_customizer_attributes(
89+
response: Any = customizer_attribute_service.mutate_customizer_attributes(
8790
customer_id=customer_id, operations=[operation]
8891
)
8992

90-
resource_name = response.results[0].resource_name
93+
resource_name: str = response.results[0].resource_name
9194
print(
9295
f"Added text customizer attribute with resource name '{resource_name}'"
9396
)
@@ -96,7 +99,9 @@ def create_text_customizer_attribute(client, customer_id, customizer_name):
9699

97100

98101
# [START add_ad_customizer_1]
99-
def create_price_customizer_attribute(client, customer_id, customizer_name):
102+
def create_price_customizer_attribute(
103+
client: GoogleAdsClient, customer_id: str, customizer_name: str
104+
) -> str:
100105
"""Creates a price customizer attribute and returns its resource name.
101106
102107
Args:
@@ -107,22 +112,22 @@ def create_price_customizer_attribute(client, customer_id, customizer_name):
107112
Returns:
108113
a resource name for a text customizer attribute.
109114
"""
110-
customizer_attribute_service = client.get_service(
115+
customizer_attribute_service: Any = client.get_service(
111116
"CustomizerAttributeService"
112117
)
113118

114119
# Creates a price customizer attribute. The customizer attribute name is
115120
# arbitrary and will be used as a placeholder in the ad text fields.
116-
operation = client.get_type("CustomizerAttributeOperation")
117-
price_attribute = operation.create
121+
operation: Any = client.get_type("CustomizerAttributeOperation")
122+
price_attribute: Any = operation.create
118123
price_attribute.name = customizer_name
119124
price_attribute.type_ = client.enums.CustomizerAttributeTypeEnum.PRICE
120125

121-
response = customizer_attribute_service.mutate_customizer_attributes(
126+
response: Any = customizer_attribute_service.mutate_customizer_attributes(
122127
customer_id=customer_id, operations=[operation]
123128
)
124129

125-
resource_name = response.results[0].resource_name
130+
resource_name: str = response.results[0].resource_name
126131
print(
127132
f"Added price customizer attribute with resource name '{resource_name}'"
128133
)
@@ -132,12 +137,12 @@ def create_price_customizer_attribute(client, customer_id, customizer_name):
132137

133138
# [START add_ad_customizer_2]
134139
def link_customizer_attributes(
135-
client,
136-
customer_id,
137-
ad_group_id,
138-
text_customizer_resource_name,
139-
price_customizer_resource_name,
140-
):
140+
client: GoogleAdsClient,
141+
customer_id: str,
142+
ad_group_id: str,
143+
text_customizer_resource_name: str,
144+
price_customizer_resource_name: str,
145+
) -> None:
141146
"""Restricts the ad customizer attributes to work with a specific ad group.
142147
143148
This prevents the customizer attributes from being used elsewhere and makes
@@ -150,13 +155,15 @@ def link_customizer_attributes(
150155
text_customizer_resource_name: the resource name of the text customizer attribute.
151156
price_customizer_resource_name: the resource name of the price customizer attribute.
152157
"""
153-
googleads_service = client.get_service("GoogleAdsService")
154-
ad_group_customizer_service = client.get_service("AdGroupCustomizerService")
158+
googleads_service: Any = client.get_service("GoogleAdsService")
159+
ad_group_customizer_service: Any = client.get_service(
160+
"AdGroupCustomizerService"
161+
)
155162

156163
# Binds the text attribute customizer to a specific ad group to make sure
157164
# it will only be used to customize ads inside that ad group.
158-
mars_operation = client.get_type("AdGroupCustomizerOperation")
159-
mars_customizer = mars_operation.create
165+
mars_operation: Any = client.get_type("AdGroupCustomizerOperation")
166+
mars_customizer: Any = mars_operation.create
160167
mars_customizer.customizer_attribute = text_customizer_resource_name
161168
mars_customizer.value.type_ = client.enums.CustomizerAttributeTypeEnum.TEXT
162169
mars_customizer.value.string_value = "Mars"
@@ -166,8 +173,8 @@ def link_customizer_attributes(
166173

167174
# Binds the price attribute customizer to a specific ad group to make sure
168175
# it will only be used to customize ads inside that ad group.
169-
price_operation = client.get_type("AdGroupCustomizerOperation")
170-
price_customizer = price_operation.create
176+
price_operation: Any = client.get_type("AdGroupCustomizerOperation")
177+
price_customizer: Any = price_operation.create
171178
price_customizer.customizer_attribute = price_customizer_resource_name
172179
price_customizer.value.type_ = (
173180
client.enums.CustomizerAttributeTypeEnum.PRICE
@@ -177,7 +184,7 @@ def link_customizer_attributes(
177184
customer_id, ad_group_id
178185
)
179186

180-
response = ad_group_customizer_service.mutate_ad_group_customizers(
187+
response: Any = ad_group_customizer_service.mutate_ad_group_customizers(
181188
customer_id=customer_id, operations=[mars_operation, price_operation]
182189
)
183190

@@ -191,12 +198,12 @@ def link_customizer_attributes(
191198

192199
# [START add_ad_customizer_3]
193200
def create_ad_with_customizations(
194-
client,
195-
customer_id,
196-
ad_group_id,
197-
text_customizer_name,
198-
price_customizer_name,
199-
):
201+
client: GoogleAdsClient,
202+
customer_id: str,
203+
ad_group_id: str,
204+
text_customizer_name: str,
205+
price_customizer_name: str,
206+
) -> None:
200207
"""Creates a responsive search ad (RSA).
201208
202209
The RSA uses the ad customizer attributes to populate the placeholders.
@@ -208,41 +215,41 @@ def create_ad_with_customizations(
208215
text_customizer_name: name of the text customizer.
209216
price_customizer_name: name of the price customizer.
210217
"""
211-
googleads_service = client.get_service("GoogleAdsService")
212-
ad_group_ad_service = client.get_service("AdGroupAdService")
218+
googleads_service: Any = client.get_service("GoogleAdsService")
219+
ad_group_ad_service: Any = client.get_service("AdGroupAdService")
213220

214221
# Creates a responsive search ad using the attribute customizer names as
215222
# placeholders and default values to be used in case there are no attribute
216223
# customizer values.
217-
operation = client.get_type("AdGroupAdOperation")
218-
ad_group_ad = operation.create
224+
operation: Any = client.get_type("AdGroupAdOperation")
225+
ad_group_ad: Any = operation.create
219226
ad_group_ad.ad.final_urls.append("https://www.example.com")
220227
ad_group_ad.ad_group = googleads_service.ad_group_path(
221228
customer_id, ad_group_id
222229
)
223230

224-
headline_1 = client.get_type("AdTextAsset")
231+
headline_1: Any = client.get_type("AdTextAsset")
225232
headline_1.text = (
226233
f"Luxury cruise to {{CUSTOMIZER.{text_customizer_name}:Venus}}"
227234
)
228235
headline_1.pinned_field = client.enums.ServedAssetFieldTypeEnum.HEADLINE_1
229236

230-
headline_2 = client.get_type("AdTextAsset")
237+
headline_2: Any = client.get_type("AdTextAsset")
231238
headline_2.text = f"Only {{CUSTOMIZER.{price_customizer_name}:10.0€}}"
232239

233-
headline_3 = client.get_type("AdTextAsset")
240+
headline_3: Any = client.get_type("AdTextAsset")
234241
headline_3.text = f"Cruise to {{CUSTOMIZER.{text_customizer_name}:Venus}} for {{CUSTOMIZER.{price_customizer_name}:10.0€}}"
235242

236243
ad_group_ad.ad.responsive_search_ad.headlines.extend(
237244
[headline_1, headline_2, headline_3]
238245
)
239246

240-
description_1 = client.get_type("AdTextAsset")
247+
description_1: Any = client.get_type("AdTextAsset")
241248
description_1.text = (
242249
f"Tickets are only {{CUSTOMIZER.{price_customizer_name}:10.0€}}!"
243250
)
244251

245-
description_2 = client.get_type("AdTextAsset")
252+
description_2: Any = client.get_type("AdTextAsset")
246253
description_2.text = (
247254
f"Buy your tickets to {{CUSTOMIZER.{text_customizer_name}:Venus}} now!"
248255
)
@@ -251,10 +258,10 @@ def create_ad_with_customizations(
251258
[description_1, description_2]
252259
)
253260

254-
response = ad_group_ad_service.mutate_ad_group_ads(
261+
response: Any = ad_group_ad_service.mutate_ad_group_ads(
255262
customer_id=customer_id, operations=[operation]
256263
)
257-
resource_name = response.results[0].resource_name
264+
resource_name: str = response.results[0].resource_name
258265
print(f"Added an ad with resource name '{resource_name}'")
259266
# [END add_ad_customizer_3]
260267

@@ -282,11 +289,21 @@ def create_ad_with_customizations(
282289
required=True,
283290
help="An ad group ID.",
284291
)
285-
args = parser.parse_args()
292+
args: argparse.Namespace = parser.parse_args()
286293

287294
# GoogleAdsClient will read the google-ads.yaml configuration file in the
288295
# home directory if none is specified.
296+
<<<<<<< HEAD
289297
googleads_client = GoogleAdsClient.load_from_storage(version="v21")
298+
=======
299+
<<<<<<< HEAD
300+
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
301+
=======
302+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
303+
version="v19"
304+
)
305+
>>>>>>> 83bf66141 (Add type hints to files in examples/advanced_operations)
306+
>>>>>>> f8ca36136 (Add type hints to files in examples/advanced_operations)
290307

291308
try:
292309
main(googleads_client, args.customer_id, args.ad_group_id)

examples/advanced_operations/add_ad_group_bid_modifier.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,28 @@
2020

2121
import argparse
2222
import sys
23+
from typing import Any
2324

2425
from google.ads.googleads.client import GoogleAdsClient
2526
from google.ads.googleads.errors import GoogleAdsException
2627

2728

2829
# [START add_ad_group_bid_modifier]
29-
def main(client, customer_id, ad_group_id, bid_modifier_value):
30-
ad_group_service = client.get_service("AdGroupService")
31-
ad_group_bm_service = client.get_service("AdGroupBidModifierService")
30+
def main(
31+
client: GoogleAdsClient,
32+
customer_id: str,
33+
ad_group_id: str,
34+
bid_modifier_value: float,
35+
) -> None:
36+
ad_group_service: Any = client.get_service("AdGroupService")
37+
ad_group_bm_service: Any = client.get_service("AdGroupBidModifierService")
3238

3339
# Create ad group bid modifier for mobile devices with the specified ad
3440
# group ID and bid modifier value.
35-
ad_group_bid_modifier_operation = client.get_type(
41+
ad_group_bid_modifier_operation: Any = client.get_type(
3642
"AdGroupBidModifierOperation"
3743
)
38-
ad_group_bid_modifier = ad_group_bid_modifier_operation.create
44+
ad_group_bid_modifier: Any = ad_group_bid_modifier_operation.create
3945

4046
# Set the ad group.
4147
ad_group_bid_modifier.ad_group = ad_group_service.ad_group_path(
@@ -46,13 +52,15 @@ def main(client, customer_id, ad_group_id, bid_modifier_value):
4652
ad_group_bid_modifier.bid_modifier = bid_modifier_value
4753

4854
# Sets the device.
49-
device_enum = client.enums.DeviceEnum
55+
device_enum: Any = client.enums.DeviceEnum
5056
ad_group_bid_modifier.device.type_ = device_enum.MOBILE
5157

5258
# Add the ad group bid modifier.
53-
ad_group_bm_response = ad_group_bm_service.mutate_ad_group_bid_modifiers(
54-
customer_id=customer_id,
55-
operations=[ad_group_bid_modifier_operation],
59+
ad_group_bm_response: Any = (
60+
ad_group_bm_service.mutate_ad_group_bid_modifiers(
61+
customer_id=customer_id,
62+
operations=[ad_group_bid_modifier_operation],
63+
)
5664
)
5765
# [END add_ad_group_bid_modifier]
5866

@@ -88,11 +96,15 @@ def main(client, customer_id, ad_group_id, bid_modifier_value):
8896
default=1.5,
8997
help="The bid modifier value.",
9098
)
91-
args = parser.parse_args()
99+
args: argparse.Namespace = parser.parse_args()
92100

93101
# GoogleAdsClient will read the google-ads.yaml configuration file in the
94102
# home directory if none is specified.
95-
googleads_client = GoogleAdsClient.load_from_storage(version="v21")
103+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
104+
# home directory if none is specified.
105+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
106+
version="v21"
107+
)
96108

97109
try:
98110
main(

0 commit comments

Comments
 (0)