Skip to content

Commit 69a3b67

Browse files
authored
Add type hints to examples/misc (#935)
1 parent b1bb6b1 commit 69a3b67

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

examples/misc/add_ad_group_image_asset.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@
2626
from google.api_core import protobuf_helpers
2727

2828

29-
def main(client, customer_id, ad_group_id, asset_id):
29+
def main(
30+
client: GoogleAdsClient,
31+
customer_id: str,
32+
ad_group_id: str,
33+
asset_id: str,
34+
) -> None:
3035
ad_group_asset_service = client.get_service("AdGroupAssetService")
31-
ad_group_asset_resource_name = ad_group_asset_service.asset_path(
36+
ad_group_asset_resource_name: str = ad_group_asset_service.asset_path(
3237
customer_id, asset_id
3338
)
3439

examples/misc/campaign_report_to_csv.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,24 @@
3838
_DEFAULT_FILE_NAME = "campaign_report_to_csv_results.csv"
3939

4040

41-
def main(client, customer_id, output_file, write_headers):
41+
def main(
42+
client: GoogleAdsClient,
43+
customer_id: str,
44+
output_file: str,
45+
write_headers: bool,
46+
) -> None:
4247
"""Writes rows returned from a search_stream request to a CSV file.
4348
Args:
4449
client: An initialized GoogleAdsClient instance.
4550
customer_id (str): The client customer ID string.
4651
output_file (str): Filename of the file to write the report data to.
4752
write_headers (bool): From argparse, True if arg is provided.
4853
"""
49-
file_dir = os.path.dirname(os.path.abspath(__file__))
50-
file_path = os.path.join(file_dir, output_file)
54+
file_dir: str = os.path.dirname(os.path.abspath(__file__))
55+
file_path: str = os.path.join(file_dir, output_file)
5156
ga_service = client.get_service("GoogleAdsService")
5257

53-
query = """
58+
query: str = """
5459
SELECT
5560
customer.descriptive_name,
5661
segments.date,
@@ -74,7 +79,7 @@ def main(client, customer_id, output_file, write_headers):
7479
writer = csv.writer(f)
7580

7681
# Define a list of headers for the first row.
77-
headers = [
82+
headers: list[str] = [
7883
"Account",
7984
"Date",
8085
"Campaign",

examples/misc/set_custom_client_timeouts.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import argparse
2727
import sys
28+
from typing import List
2829

2930
from google.ads.googleads.client import GoogleAdsClient
3031
from google.ads.googleads.errors import GoogleAdsException
@@ -33,25 +34,27 @@
3334

3435

3536
_CLIENT_TIMEOUT_SECONDS = 5 * 60 # 5 minutes.
36-
_QUERY = "SELECT campaign.id FROM campaign"
37+
_QUERY: str = "SELECT campaign.id FROM campaign"
3738

3839

39-
def main(client, customer_id):
40+
def main(client: GoogleAdsClient, customer_id: str) -> None:
4041
"""Main method, to run this code example as a standalone application."""
4142
make_server_streaming_call(client, customer_id)
4243
make_unary_call(client, customer_id)
4344

4445

4546
# [START set_custom_client_timeouts]
46-
def make_server_streaming_call(client, customer_id):
47+
def make_server_streaming_call(
48+
client: GoogleAdsClient, customer_id: str
49+
) -> None:
4750
"""Makes a server streaming call using a custom client timeout.
4851
4952
Args:
5053
client: An initialized GoogleAds client.
5154
customer_id: The str Google Ads customer ID.
5255
"""
5356
ga_service = client.get_service("GoogleAdsService")
54-
campaign_ids = []
57+
campaign_ids: List[str] = []
5558

5659
try:
5760
search_request = client.get_type("SearchGoogleAdsStreamRequest")
@@ -92,15 +95,15 @@ def make_server_streaming_call(client, customer_id):
9295

9396

9497
# [START set_custom_client_timeouts_1]
95-
def make_unary_call(client, customer_id):
98+
def make_unary_call(client: GoogleAdsClient, customer_id: str) -> None:
9699
"""Makes a unary call using a custom client timeout.
97100
98101
Args:
99102
client: An initialized GoogleAds client.
100103
customer_id: The Google Ads customer ID.
101104
"""
102105
ga_service = client.get_service("GoogleAdsService")
103-
campaign_ids = []
106+
campaign_ids: List[str] = []
104107

105108
try:
106109
search_request = client.get_type("SearchGoogleAdsRequest")

examples/misc/upload_image_asset.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,25 @@
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 import (
27+
AssetOperation,
28+
MutateAssetsResponse,
29+
)
30+
from google.ads.googleads.v19.resources.types import Asset
2631
from examples.utils.example_helpers import get_image_bytes_from_url
2732

2833

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

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

3742
asset_service = client.get_service("AssetService")
38-
asset_operation = client.get_type("AssetOperation")
39-
asset = asset_operation.create
43+
asset_operation: AssetOperation = client.get_type("AssetOperation")
44+
asset: Asset = asset_operation.create
4045
asset.type_ = client.enums.AssetTypeEnum.IMAGE
4146
asset.image_asset.data = image_content
4247
asset.image_asset.file_size = len(image_content)
@@ -49,9 +54,10 @@ def main(client, customer_id):
4954
# When there is an existing image asset with the same content but a different
5055
# name, the new name will be dropped silently.
5156
asset.name = "Marketing Image"
52-
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)