Skip to content

Commit f4161c7

Browse files
I've added type hints and annotations to the Python files in the examples/campaign_management directory for you.
Here's what I did: 1. I added type hints to function arguments and return values for all functions. 2. I added type hints for variables within function scopes. 3. I imported necessary types from the `typing` module (e.g., List, Any, Dict, Coroutine). 4. I used specific types from the Google Ads library (e.g., GoogleAdsClient, various service clients, operation types, resource types) for better precision. 5. I addressed `asyncio` specifics for files using asynchronous operations. 6. I checked for type errors. Most errors related to name conflicts or missing stubs for external libraries were addressed or ignored where appropriate. 7. I ensured consistent code formatting across all modified files. The following files were modified: - examples/campaign_management/__init__.py - examples/campaign_management/add_campaign_labels.py - examples/campaign_management/add_complete_campaigns_using_batch_job.py - examples/campaign_management/create_experiment.py - examples/campaign_management/get_all_disapproved_ads.py - examples/campaign_management/set_ad_parameters.py - examples/campaign_management/update_campaign_criterion_bid_modifier.py - examples/campaign_management/validate_ad.py
1 parent 4f350a8 commit f4161c7

File tree

7 files changed

+488
-281
lines changed

7 files changed

+488
-281
lines changed

examples/campaign_management/add_campaign_labels.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,31 @@
1919

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

2324
from google.ads.googleads.client import GoogleAdsClient
2425
from google.ads.googleads.errors import GoogleAdsException
26+
from google.ads.googleads.v17.services.services.campaign_label_service import (
27+
CampaignLabelServiceClient,
28+
)
29+
from google.ads.googleads.v17.services.services.campaign_service import (
30+
CampaignServiceClient,
31+
)
32+
from google.ads.googleads.v17.services.services.label_service import LabelServiceClient
33+
from google.ads.googleads.v17.services.types.campaign_label_service import (
34+
MutateCampaignLabelsResponse,
35+
)
36+
from google.ads.googleads.v17.resources.types.campaign_label import CampaignLabel
37+
from google.ads.googleads.v17.services.types.google_ads_service import GoogleAdsRow
2538

2639

2740
# [START add_campaign_labels]
28-
def main(client, customer_id, label_id, campaign_ids):
41+
def main(
42+
client: GoogleAdsClient,
43+
customer_id: str,
44+
label_id: str,
45+
campaign_ids: List[str],
46+
) -> None:
2947
"""This code example adds a campaign label to a list of campaigns.
3048
3149
Args:
@@ -36,28 +54,32 @@ def main(client, customer_id, label_id, campaign_ids):
3654
"""
3755

3856
# Get an instance of CampaignLabelService client.
39-
campaign_label_service = client.get_service("CampaignLabelService")
40-
campaign_service = client.get_service("CampaignService")
41-
label_service = client.get_service("LabelService")
57+
campaign_label_service: CampaignLabelServiceClient = client.get_service(
58+
"CampaignLabelService"
59+
)
60+
campaign_service: CampaignServiceClient = client.get_service("CampaignService")
61+
label_service: LabelServiceClient = client.get_service("LabelService")
4262

4363
# Build the resource name of the label to be added across the campaigns.
44-
label_resource_name = label_service.label_path(customer_id, label_id)
64+
label_resource_name: str = label_service.label_path(customer_id, label_id)
4565

46-
operations = []
66+
operations: List[Any] = []
4767

4868
for campaign_id in campaign_ids:
49-
campaign_resource_name = campaign_service.campaign_path(
69+
campaign_resource_name: str = campaign_service.campaign_path(
5070
customer_id, campaign_id
5171
)
52-
campaign_label_operation = client.get_type("CampaignLabelOperation")
72+
campaign_label_operation: Any = client.get_type("CampaignLabelOperation")
5373

54-
campaign_label = campaign_label_operation.create
74+
campaign_label: CampaignLabel = campaign_label_operation.create
5575
campaign_label.campaign = campaign_resource_name
5676
campaign_label.label = label_resource_name
5777
operations.append(campaign_label_operation)
5878

59-
response = campaign_label_service.mutate_campaign_labels(
60-
customer_id=customer_id, operations=operations
79+
response: MutateCampaignLabelsResponse = (
80+
campaign_label_service.mutate_campaign_labels(
81+
customer_id=customer_id, operations=operations
82+
)
6183
)
6284
print(f"Added {len(response.results)} campaign labels:")
6385
for result in response.results:
@@ -67,8 +89,7 @@ def main(client, customer_id, label_id, campaign_ids):
6789

6890
if __name__ == "__main__":
6991
parser = argparse.ArgumentParser(
70-
description="This code example adds a campaign label to a list of "
71-
"campaigns."
92+
description="This code example adds a campaign label to a list of " "campaigns."
7293
)
7394
# The following argument(s) should be provided to run the example.
7495
parser.add_argument(
@@ -93,15 +114,13 @@ def main(client, customer_id, label_id, campaign_ids):
93114
required=True,
94115
help="The campaign IDs to receive the label.",
95116
)
96-
args = parser.parse_args()
117+
args: argparse.Namespace = parser.parse_args()
97118

98119
# GoogleAdsClient will read the google-ads.yaml configuration file in the
99120
# home directory if none is specified.
100-
googleads_client = GoogleAdsClient.load_from_storage(version="v19")
121+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(version="v19")
101122
try:
102-
main(
103-
googleads_client, args.customer_id, args.label_id, args.campaign_ids
104-
)
123+
main(googleads_client, args.customer_id, args.label_id, args.campaign_ids)
105124
except GoogleAdsException as ex:
106125
print(
107126
f'Request with ID "{ex.request_id}" failed with status '

0 commit comments

Comments
 (0)