Skip to content

Commit 511a670

Browse files
google-labs-jules[bot]BenRKarl
authored andcommitted
Add type hints to examples/targeting files
This change introduces type annotations to the Python scripts in the examples/targeting/ directory. Type hints were added for: - Function arguments and return values. - Key local variables, particularly those interacting with the Google Ads API. - GoogleAdsClient instances and argparse.Namespace objects. The typing.Any type was used for complex Google Ads API objects where specific types were not readily apparent. This improves code readability and maintainability by making type expectations explicit.
1 parent 862a074 commit 511a670

File tree

4 files changed

+93
-47
lines changed

4 files changed

+93
-47
lines changed

examples/targeting/add_campaign_targeting_criteria.py

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,33 @@
1616

1717

1818
import argparse
19+
from typing import Any, List
1920
import sys
2021

2122
from google.ads.googleads.client import GoogleAdsClient
2223
from google.ads.googleads.errors import GoogleAdsException
2324

2425

25-
def main(client, customer_id, campaign_id, keyword_text, location_id):
26-
campaign_criterion_service = client.get_service("CampaignCriterionService")
26+
def main(
27+
client: GoogleAdsClient,
28+
customer_id: str,
29+
campaign_id: str,
30+
keyword_text: str,
31+
location_id: str,
32+
) -> None:
33+
campaign_criterion_service: Any = client.get_service(
34+
"CampaignCriterionService"
35+
)
2736

28-
operations = [
37+
operations: List[Any] = [
2938
create_location_op(client, customer_id, campaign_id, location_id),
3039
create_negative_keyword_op(
3140
client, customer_id, campaign_id, keyword_text
3241
),
3342
create_proximity_op(client, customer_id, campaign_id),
3443
]
3544

36-
campaign_criterion_response = (
45+
campaign_criterion_response: Any = (
3746
campaign_criterion_service.mutate_campaign_criteria(
3847
customer_id=customer_id, operations=operations
3948
)
@@ -44,13 +53,19 @@ def main(client, customer_id, campaign_id, keyword_text, location_id):
4453

4554

4655
# [START add_campaign_targeting_criteria]
47-
def create_location_op(client, customer_id, campaign_id, location_id):
48-
campaign_service = client.get_service("CampaignService")
49-
geo_target_constant_service = client.get_service("GeoTargetConstantService")
56+
def create_location_op(
57+
client: GoogleAdsClient, customer_id: str, campaign_id: str, location_id: str
58+
) -> Any:
59+
campaign_service: Any = client.get_service("CampaignService")
60+
geo_target_constant_service: Any = client.get_service(
61+
"GeoTargetConstantService"
62+
)
5063

5164
# Create the campaign criterion.
52-
campaign_criterion_operation = client.get_type("CampaignCriterionOperation")
53-
campaign_criterion = campaign_criterion_operation.create
65+
campaign_criterion_operation: Any = client.get_type(
66+
"CampaignCriterionOperation"
67+
)
68+
campaign_criterion: Any = campaign_criterion_operation.create
5469
campaign_criterion.campaign = campaign_service.campaign_path(
5570
customer_id, campaign_id
5671
)
@@ -67,12 +82,16 @@ def create_location_op(client, customer_id, campaign_id, location_id):
6782
# [END add_campaign_targeting_criteria]
6883

6984

70-
def create_negative_keyword_op(client, customer_id, campaign_id, keyword_text):
71-
campaign_service = client.get_service("CampaignService")
85+
def create_negative_keyword_op(
86+
client: GoogleAdsClient, customer_id: str, campaign_id: str, keyword_text: str
87+
) -> Any:
88+
campaign_service: Any = client.get_service("CampaignService")
7289

7390
# Create the campaign criterion.
74-
campaign_criterion_operation = client.get_type("CampaignCriterionOperation")
75-
campaign_criterion = campaign_criterion_operation.create
91+
campaign_criterion_operation: Any = client.get_type(
92+
"CampaignCriterionOperation"
93+
)
94+
campaign_criterion: Any = campaign_criterion_operation.create
7695
campaign_criterion.campaign = campaign_service.campaign_path(
7796
customer_id, campaign_id
7897
)
@@ -85,12 +104,16 @@ def create_negative_keyword_op(client, customer_id, campaign_id, keyword_text):
85104

86105

87106
# [START add_campaign_targeting_criteria_1]
88-
def create_proximity_op(client, customer_id, campaign_id):
89-
campaign_service = client.get_service("CampaignService")
107+
def create_proximity_op(
108+
client: GoogleAdsClient, customer_id: str, campaign_id: str
109+
) -> Any:
110+
campaign_service: Any = client.get_service("CampaignService")
90111

91112
# Create the campaign criterion.
92-
campaign_criterion_operation = client.get_type("CampaignCriterionOperation")
93-
campaign_criterion = campaign_criterion_operation.create
113+
campaign_criterion_operation: Any = client.get_type(
114+
"CampaignCriterionOperation"
115+
)
116+
campaign_criterion: Any = campaign_criterion_operation.create
94117
campaign_criterion.campaign = campaign_service.campaign_path(
95118
customer_id, campaign_id
96119
)
@@ -146,11 +169,13 @@ def create_proximity_op(client, customer_id, campaign_id):
146169
"https://developers.google.com/google-ads/api/reference/data/geotargets"
147170
),
148171
)
149-
args = parser.parse_args()
172+
args: argparse.Namespace = parser.parse_args()
150173

151174
# GoogleAdsClient will read the google-ads.yaml configuration file in the
152175
# home directory if none is specified.
153-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
176+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
177+
version="v20"
178+
)
154179

155180
try:
156181
main(

examples/targeting/add_customer_negative_criteria.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,44 @@
1919

2020

2121
import argparse
22+
from typing import Any, List
2223
import sys
2324

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

2728

28-
def main(client, customer_id):
29+
def main(client: GoogleAdsClient, customer_id: str) -> None:
2930
"""The main method that creates all necessary entities for the example.
3031
3132
Args:
3233
client: an initialized GoogleAdsClient instance.
3334
customer_id: a client customer ID.
3435
"""
35-
tragedy_criterion_op = client.get_type("CustomerNegativeCriterionOperation")
36-
tragedy_criterion = tragedy_criterion_op.create
36+
tragedy_criterion_op: Any = client.get_type(
37+
"CustomerNegativeCriterionOperation"
38+
)
39+
tragedy_criterion: Any = tragedy_criterion_op.create
3740
# Creates a negative customer criterion excluding the content label type
3841
# of 'TRAGEDY'.
3942
tragedy_criterion.content_label.type_ = (
4043
client.enums.ContentLabelTypeEnum.TRAGEDY
4144
)
4245

43-
placement_criterion_op = client.get_type(
46+
placement_criterion_op: Any = client.get_type(
4447
"CustomerNegativeCriterionOperation"
4548
)
46-
placement_criterion = placement_criterion_op.create
49+
placement_criterion: Any = placement_criterion_op.create
4750
# Creates a negative customer criterion excluding the placement with URL
4851
# 'http://www.example.com'.
4952
placement_criterion.placement.url = "http://www.example.com"
5053

51-
customer_negative_criterion_service = client.get_service(
54+
customer_negative_criterion_service: Any = client.get_service(
5255
"CustomerNegativeCriterionService"
5356
)
5457

5558
# Issues a mutate request to add the negative customer criteria.
56-
response = (
59+
response: Any = (
5760
customer_negative_criterion_service.mutate_customer_negative_criteria(
5861
customer_id=customer_id,
5962
operations=[tragedy_criterion_op, placement_criterion_op],
@@ -80,11 +83,13 @@ def main(client, customer_id):
8083
required=True,
8184
help="The Google Ads customer ID.",
8285
)
83-
args = parser.parse_args()
86+
args: argparse.Namespace = parser.parse_args()
8487

8588
# GoogleAdsClient will read the google-ads.yaml configuration file in the
8689
# home directory if none is specified.
87-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
90+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
91+
version="v20"
92+
)
8893

8994
try:
9095
main(

examples/targeting/add_demographic_targeting_criteria.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,49 @@
1818

1919

2020
import argparse
21+
from typing import Any, List
2122
import sys
2223

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

2627

27-
def main(client, customer_id, ad_group_id):
28-
ad_group_service = client.get_service("AdGroupService")
29-
ad_group_criterion_service = client.get_service("AdGroupCriterionService")
28+
def main(
29+
client: GoogleAdsClient, customer_id: str, ad_group_id: str
30+
) -> None:
31+
ad_group_service: Any = client.get_service("AdGroupService")
32+
ad_group_criterion_service: Any = client.get_service(
33+
"AdGroupCriterionService"
34+
)
3035

31-
ad_group_resource_name = ad_group_service.ad_group_path(
36+
ad_group_resource_name: str = ad_group_service.ad_group_path(
3237
customer_id, ad_group_id
3338
)
3439
# Create a positive ad group criterion for the gender MALE.
35-
gender_ad_group_criterion_operation = client.get_type(
40+
gender_ad_group_criterion_operation: Any = client.get_type(
3641
"AdGroupCriterionOperation"
3742
)
38-
gender_ad_group_criterion = gender_ad_group_criterion_operation.create
43+
gender_ad_group_criterion: Any = (
44+
gender_ad_group_criterion_operation.create
45+
)
3946
gender_ad_group_criterion.ad_group = ad_group_resource_name
4047
gender_ad_group_criterion.gender.type_ = client.enums.GenderTypeEnum.MALE
4148

4249
# Create a negative ad group criterion for age range of 18 to 24.
43-
age_range_ad_group_criterion_operation = client.get_type(
50+
age_range_ad_group_criterion_operation: Any = client.get_type(
4451
"AdGroupCriterionOperation"
4552
)
46-
age_range_ad_group_criterion = age_range_ad_group_criterion_operation.create
53+
age_range_ad_group_criterion: Any = (
54+
age_range_ad_group_criterion_operation.create
55+
)
4756
age_range_ad_group_criterion.ad_group = ad_group_resource_name
4857
age_range_ad_group_criterion.negative = True
4958
age_range_ad_group_criterion.age_range.type_ = (
5059
client.enums.AgeRangeTypeEnum.AGE_RANGE_18_24
5160
)
5261

5362
# Add two ad group criteria
54-
ad_group_criterion_response = (
63+
ad_group_criterion_response: Any = (
5564
ad_group_criterion_service.mutate_ad_group_criteria(
5665
customer_id=customer_id,
5766
operations=[
@@ -83,11 +92,13 @@ def main(client, customer_id, ad_group_id):
8392
parser.add_argument(
8493
"-a", "--ad_group_id", type=str, required=True, help="The ad group ID."
8594
)
86-
args = parser.parse_args()
95+
args: argparse.Namespace = parser.parse_args()
8796

8897
# GoogleAdsClient will read the google-ads.yaml configuration file in the
8998
# home directory if none is specified.
90-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
99+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
100+
version="v20"
101+
)
91102

92103
try:
93104
main(googleads_client, args.customer_id, args.ad_group_id)

examples/targeting/get_geo_target_constants_by_names.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,41 @@
1515
"""This example illustrates getting GeoTargetConstants by given location names."""
1616

1717

18+
from typing import Any, List
1819
import sys
1920

2021
from google.ads.googleads.client import GoogleAdsClient
2122
from google.ads.googleads.errors import GoogleAdsException
2223

2324
# Locale is using ISO 639-1 format. If an invalid locale is given,
2425
# 'en' is used by default.
25-
LOCALE = "en"
26+
LOCALE: str = "en"
2627

2728
# A list of country codes can be referenced here:
2829
# https://developers.google.com/google-ads/api/reference/data/geotargets
29-
COUNTRY_CODE = "FR"
30+
COUNTRY_CODE: str = "FR"
3031

3132

3233
# [START get_geo_target_constants_by_names]
33-
def main(client):
34-
gtc_service = client.get_service("GeoTargetConstantService")
34+
def main(client: GoogleAdsClient) -> None:
35+
gtc_service: Any = client.get_service("GeoTargetConstantService")
3536

36-
gtc_request = client.get_type("SuggestGeoTargetConstantsRequest")
37+
gtc_request: Any = client.get_type("SuggestGeoTargetConstantsRequest")
3738

3839
gtc_request.locale = LOCALE
3940
gtc_request.country_code = COUNTRY_CODE
4041

4142
# The location names to get suggested geo target constants.
43+
# Type hint for gtc_request.location_names.names is not straightforward
44+
# as it's part of a complex protobuf object.
4245
gtc_request.location_names.names.extend(
4346
["Paris", "Quebec", "Spain", "Deutschland"]
4447
)
4548

46-
results = gtc_service.suggest_geo_target_constants(gtc_request)
49+
results: Any = gtc_service.suggest_geo_target_constants(gtc_request)
4750

4851
for suggestion in results.geo_target_constant_suggestions:
49-
geo_target_constant = suggestion.geo_target_constant
52+
geo_target_constant: Any = suggestion.geo_target_constant
5053
print(
5154
f"{geo_target_constant.resource_name} "
5255
f"({geo_target_constant.name}, "
@@ -63,7 +66,9 @@ def main(client):
6366
if __name__ == "__main__":
6467
# GoogleAdsClient will read the google-ads.yaml configuration file in the
6568
# home directory if none is specified.
66-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
69+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
70+
version="v20"
71+
)
6772

6873
try:
6974
main(googleads_client)

0 commit comments

Comments
 (0)