Skip to content

Commit 26daa22

Browse files
Add type hints and annotations to examples/feeds
This change adds type hints to function signatures and variable annotations to the Python files in the examples/feeds directory. This improves code readability and helps with static analysis.
1 parent 4f350a8 commit 26daa22

File tree

2 files changed

+218
-90
lines changed

2 files changed

+218
-90
lines changed

examples/feeds/remove_flights_feed_item_attribute_value.py

Lines changed: 109 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,33 @@
2929

3030
import argparse
3131
import sys
32+
from typing import Dict, Mapping, Any
3233

3334
from google.ads.googleads.client import GoogleAdsClient
3435
from google.ads.googleads.errors import GoogleAdsException
36+
from google.ads.googleads.v19.enums.types import flight_placeholder_field as flight_placeholder_field_enum
37+
from google.ads.googleads.v19.types import feed_item as feed_item_type
38+
from google.ads.googleads.v19.types import (
39+
feed_item_operation as feed_item_operation_type,
40+
)
41+
from google.ads.googleads.v19.services.types import (
42+
feed_item_service as feed_item_service_type,
43+
)
44+
from google.ads.googleads.v19.services.types import (
45+
google_ads_service as google_ads_service_type,
46+
)
47+
from google.ads.googleads.v19.types import feed as feed_type
48+
3549
from google.api_core import protobuf_helpers
3650

3751

3852
def main(
39-
client, customer_id, feed_id, feed_item_id, flight_placeholder_field_name
40-
):
53+
client: GoogleAdsClient,
54+
customer_id: str,
55+
feed_id: str,
56+
feed_item_id: str,
57+
flight_placeholder_field_name: str,
58+
) -> None:
4159
"""Removes a feed item attribute value of a feed item in a flights feed.
4260
4361
Args:
@@ -50,19 +68,26 @@ def main(
5068
"""
5169
# [START remove_flights_feed_item_attribute_value]
5270
# Get the FeedItemService client.
53-
feed_item_service = client.get_service("FeedItemService")
71+
feed_item_service: feed_item_service_type.FeedItemServiceClient = client.get_service(
72+
"FeedItemService"
73+
)
5474

5575
# Create the FeedItemOperation.
56-
feed_item_operation = client.get_type("FeedItemOperation")
76+
feed_item_operation: feed_item_operation_type.FeedItemOperation = client.get_type(
77+
"FeedItemOperation"
78+
)
5779

5880
# Get a map of the FlightPlaceholderFields to FeedAttributes.
59-
placeholders_to_feed_attributes_map = get_feed(client, customer_id, feed_id)
81+
placeholders_to_feed_attributes_map: Mapping[
82+
flight_placeholder_field_enum.FlightPlaceholderFieldEnum,
83+
feed_type.FeedAttribute,
84+
] = get_feed(client, customer_id, feed_id)
6085

6186
# Remove the attribute from the feed item.
62-
flight_placeholder_field = client.enums.FlightPlaceholderFieldEnum[
87+
flight_placeholder_field: flight_placeholder_field_enum.FlightPlaceholderFieldEnum = client.enums.FlightPlaceholderFieldEnum[
6388
flight_placeholder_field_name
6489
].value
65-
feed_item = remove_attribute_value_from_feed_item(
90+
feed_item: feed_item_type.FeedItem = remove_attribute_value_from_feed_item(
6691
client,
6792
customer_id,
6893
feed_id,
@@ -78,7 +103,7 @@ def main(
78103
)
79104

80105
# Update the feed item and print the results.
81-
response = feed_item_service.mutate_feed_items(
106+
response: feed_item_service_type.MutateFeedItemsResponse = feed_item_service.mutate_feed_items(
82107
customer_id=customer_id, operations=[feed_item_operation]
83108
)
84109
# [END remove_flights_feed_item_attribute_value]
@@ -90,7 +115,12 @@ def main(
90115
)
91116

92117

93-
def get_feed(client, customer_id, feed_id):
118+
def get_feed(
119+
client: GoogleAdsClient, customer_id: str, feed_id: str
120+
) -> Mapping[
121+
flight_placeholder_field_enum.FlightPlaceholderFieldEnum,
122+
feed_type.FeedAttribute,
123+
]:
94124
"""Retrieves details about a feed.
95125
96126
Args:
@@ -102,53 +132,64 @@ def get_feed(client, customer_id, feed_id):
102132
requested Feed's FeedAttributes.
103133
"""
104134
# Get the GoogleAdsService client.
105-
googleads_service = client.get_service("GoogleAdsService")
135+
googleads_service: google_ads_service_type.GoogleAdsServiceClient = client.get_service(
136+
"GoogleAdsService"
137+
)
106138

107-
feed_resource_name = client.get_service("FeedService").feed_path(
139+
feed_resource_name: str = client.get_service("FeedService").feed_path(
108140
customer_id, feed_id
109141
)
110142

111143
# Construct the query.
112-
query = f"""
144+
query: str = f"""
113145
SELECT feed.attributes
114146
FROM feed
115147
WHERE feed.resource_name = '{feed_resource_name}'"""
116148

117149
# Issue the search request and get the first result, since we only need the
118150
# single feed item we created previously.
119-
search_request = client.get_type("SearchGoogleAdsRequest")
151+
search_request: google_ads_service_type.SearchGoogleAdsRequest = client.get_type(
152+
"SearchGoogleAdsRequest"
153+
)
120154
search_request.customer_id = customer_id
121155
search_request.query = query
122-
row = next(iter(googleads_service.search(request=search_request)))
156+
row: google_ads_service_type.GoogleAdsRow = next(
157+
iter(googleads_service.search(request=search_request))
158+
)
123159

124160
# Get the attributes list from the feed and create a map with keys of each
125161
# attribute and values of each corresponding ID.
126-
flight_placeholder_field_enum = client.enums.FlightPlaceholderFieldEnum
127-
feed_attributes = dict()
162+
flight_placeholder_field_enum_type: Any = (
163+
client.enums.FlightPlaceholderFieldEnum
164+
)
165+
feed_attributes: Dict[
166+
flight_placeholder_field_enum.FlightPlaceholderFieldEnum,
167+
feed_type.FeedAttribute,
168+
] = {}
128169

129170
# Loop through the feed attributes to populate the map.
130171
# The full list of FlightPlaceholderFields can be found here:
131172
# https://developers.google.com/google-ads/api/reference/rpc/latest/FlightPlaceholderFieldEnum.FlightPlaceholderField
132173
for feed_attribute in row.feed.attributes:
133174
if feed_attribute.name == "Flight Description":
134175
feed_attributes[
135-
flight_placeholder_field_enum.FLIGHT_DESCRIPTION
176+
flight_placeholder_field_enum_type.FLIGHT_DESCRIPTION
136177
] = feed_attribute
137178
elif feed_attribute.name == "Destination ID":
138179
feed_attributes[
139-
flight_placeholder_field_enum.DESTINATION_ID
180+
flight_placeholder_field_enum_type.DESTINATION_ID
140181
] = feed_attribute
141182
elif feed_attribute.name == "Flight Price":
142183
feed_attributes[
143-
flight_placeholder_field_enum.FLIGHT_PRICE
184+
flight_placeholder_field_enum_type.FLIGHT_PRICE
144185
] = feed_attribute
145186
elif feed_attribute.name == "Flight Sale Price":
146187
feed_attributes[
147-
flight_placeholder_field_enum.FLIGHT_SALE_PRICE
188+
flight_placeholder_field_enum_type.FLIGHT_SALE_PRICE
148189
] = feed_attribute
149190
elif feed_attribute.name == "Final URLs":
150191
feed_attributes[
151-
flight_placeholder_field_enum.FINAL_URLS
192+
flight_placeholder_field_enum_type.FINAL_URLS
152193
] = feed_attribute
153194
else:
154195
raise ValueError("Invalid attribute name.")
@@ -157,48 +198,57 @@ def get_feed(client, customer_id, feed_id):
157198

158199

159200
def remove_attribute_value_from_feed_item(
160-
client,
161-
customer_id,
162-
feed_id,
163-
feed_item_id,
164-
placeholders_to_feed_attributes_map,
165-
flight_placeholder_field_name,
166-
):
201+
client: GoogleAdsClient,
202+
customer_id: str,
203+
feed_id: str,
204+
feed_item_id: str,
205+
placeholders_to_feed_attributes_map: Mapping[
206+
flight_placeholder_field_enum.FlightPlaceholderFieldEnum,
207+
feed_type.FeedAttribute,
208+
],
209+
flight_placeholder_field_name: flight_placeholder_field_enum.FlightPlaceholderFieldEnum,
210+
) -> feed_item_type.FeedItem:
167211
"""Removes an attribute value from the specified feed item.
168212
169213
Args:
170-
client:
171-
customer_id:
172-
feed_id:
173-
feed_item_id:
174-
placeholders_to_feed_attributes_map:
175-
flight_placeholder_field_name:
214+
client: An initialized GoogleAdsClient instance.
215+
customer_id: The Google Ads customer ID.
216+
feed_id: The feed ID to which the feed item belongs.
217+
feed_item_id: The ID of the feed item to be updated.
218+
placeholders_to_feed_attributes_map: A map of placeholder fields to
219+
feed attributes.
220+
flight_placeholder_field_name: The flight placeholder field name for the
221+
attribute to be removed.
176222
Returns:
177223
The modified FeedItem.
178224
"""
179225
# [START remove_flights_feed_item_attribute_value_1]
180226
# Gets the ID of the FeedAttribute for the placeholder field.
181-
attribute_id = placeholders_to_feed_attributes_map[
227+
attribute_id: int = placeholders_to_feed_attributes_map[
182228
flight_placeholder_field_name
183229
].id
184230

185231
# Retrieve the feed item and its associated attributes based on its resource
186232
# name.
187-
feed_item = get_feed_item(client, customer_id, feed_id, feed_item_id)
233+
feed_item: feed_item_type.FeedItem = get_feed_item(
234+
client, customer_id, feed_id, feed_item_id
235+
)
188236

189237
# Create the FeedItemAttributeValue that will be updated.
190-
feed_item_attribute_value = client.get_type("FeedItemAttributeValue")
238+
feed_item_attribute_value: feed_item_type.FeedItemAttributeValue = client.get_type(
239+
"FeedItemAttributeValue"
240+
)
191241
feed_item_attribute_value.feed_attribute_id = attribute_id
192242

193243
# Loop through the attribute values to find the index of the
194244
# FeedItemAttributeValue to update.
195-
attribute_index = -1
196-
for attribute_value in feed_item.attribute_values:
197-
attribute_index += 1
245+
attribute_index: int = -1
246+
for i, attribute_value in enumerate(feed_item.attribute_values):
198247
if (
199248
attribute_value.feed_attribute_id
200249
== feed_item_attribute_value.feed_attribute_id
201250
):
251+
attribute_index = i
202252
break
203253

204254
if attribute_index == -1:
@@ -216,7 +266,12 @@ def remove_attribute_value_from_feed_item(
216266
# [END remove_flights_feed_item_attribute_value_1]
217267

218268

219-
def get_feed_item(client, customer_id, feed_id, feed_item_id):
269+
def get_feed_item(
270+
client: GoogleAdsClient,
271+
customer_id: str,
272+
feed_id: str,
273+
feed_item_id: str,
274+
) -> feed_item_type.FeedItem:
220275
"""Retrieves a feed item and its attribute values given a resource name.
221276
222277
Args:
@@ -228,22 +283,26 @@ def get_feed_item(client, customer_id, feed_id, feed_item_id):
228283
A FeedItem with the given resource name.
229284
"""
230285
# Get the GoogleAdsService client.
231-
googleads_service = client.get_service("GoogleAdsService")
286+
googleads_service: google_ads_service_type.GoogleAdsServiceClient = client.get_service(
287+
"GoogleAdsService"
288+
)
232289

233290
# Construct the resource name for the feed item.
234-
feed_item_resource_name = client.get_service(
291+
feed_item_resource_name: str = client.get_service(
235292
"FeedItemService"
236293
).feed_item_path(customer_id, feed_id, feed_item_id)
237294

238295
# Construct the query.
239-
query = f"""
296+
query: str = f"""
240297
SELECT feed_item.attribute_values
241298
FROM feed_item
242299
WHERE feed_item.resource_name = '{feed_item_resource_name}'"""
243300

244301
# Issue the search request and return the first result, since the query will
245302
# match only a single feed item.
246-
search_request = client.get_type("SearchGoogleAdsRequest")
303+
search_request: google_ads_service_type.SearchGoogleAdsRequest = client.get_type(
304+
"SearchGoogleAdsRequest"
305+
)
247306
search_request.customer_id = customer_id
248307
search_request.query = query
249308

@@ -253,7 +312,7 @@ def get_feed_item(client, customer_id, feed_id, feed_item_id):
253312

254313

255314
if __name__ == "__main__":
256-
parser = argparse.ArgumentParser(
315+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
257316
description="Removes a feed item attribute value of a feed item in a "
258317
"flights feed."
259318
)
@@ -286,11 +345,13 @@ def get_feed_item(client, customer_id, feed_id, feed_item_id):
286345
required=True,
287346
help="The flight placeholder field name for the attribute to be removed.",
288347
)
289-
args = parser.parse_args()
348+
args: argparse.Namespace = parser.parse_args()
290349

291350
# GoogleAdsClient will read the google-ads.yaml configuration file in the
292351
# home directory if none is specified.
293-
googleads_client = GoogleAdsClient.load_from_storage(version="v19")
352+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
353+
version="v19"
354+
)
294355

295356
try:
296357
main(

0 commit comments

Comments
 (0)