Skip to content

Commit dd0f1b8

Browse files
askmeegsengelke
andauthored
Type hints for s3 storage sdk (GoogleCloudPlatform#9947)
* Type hunts for s3 storage sdk * attempt to fix linter * type hint return values * fix return type hint * Failing tests * fix: minor type tweaks * fix: import typing for older Python versions * Update list_gcs_buckets.py * Update list_gcs_objects.py * Update s3_sdk_test.py * fix lint * lint fixes * fix type --------- Co-authored-by: Charles Engelke <engelke@google.com>
1 parent d5ec198 commit dd0f1b8

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

storage/s3-sdk/list_gcs_buckets.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
# limitations under the License.
1616

1717
import argparse
18+
from typing import List
1819

1920
# [START storage_s3_sdk_list_buckets]
20-
import boto3
21+
import boto3 # type: ignore
2122

2223

23-
def list_gcs_buckets(google_access_key_id, google_access_key_secret):
24+
def list_gcs_buckets(
25+
google_access_key_id: str, google_access_key_secret: str
26+
) -> List[str]:
2427
"""Lists all Cloud Storage buckets using AWS SDK for Python (boto3)
2528
Positional arguments:
2629
google_access_key_id: hash-based message authentication code (HMAC) access ID

storage/s3-sdk/list_gcs_objects.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
# limitations under the License.
1616

1717
import argparse
18+
from typing import List
1819

1920
# [START storage_s3_sdk_list_objects]
20-
import boto3
21+
import boto3 # type: ignore
2122

2223

23-
def list_gcs_objects(google_access_key_id, google_access_key_secret, bucket_name):
24+
def list_gcs_objects(
25+
google_access_key_id: str, google_access_key_secret: str, bucket_name: str
26+
) -> List[str]:
2427
"""Lists all Cloud Storage objects using AWS SDK for Python (boto3)
2528
Positional arguments:
2629
google_access_key_id: hash-based message authentication code (HMAC) access ID

storage/s3-sdk/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# RUN_TESTS_SESSION. The reason we can not use multiple project is
2222
# that our new projects are enforced to have
2323
# 'constraints/iam.disableServiceAccountKeyCreation' policy.
24-
def get_service_account_email():
24+
def get_service_account_email() -> str:
2525
session = os.environ.get("RUN_TESTS_SESSION")
2626
if session == "py-3.6":
2727
return "py36-storage-test@" "python-docs-samples-tests.iam.gserviceaccount.com"

storage/s3-sdk/s3_sdk_test.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16+
from typing import Tuple
1617
import uuid
1718

1819
import backoff
@@ -30,7 +31,7 @@
3031

3132

3233
@pytest.fixture(scope="module")
33-
def hmac_fixture():
34+
def hmac_fixture() -> Tuple[storage.hmac_key.HMACKeyMetadata, str]:
3435
"""
3536
Creates an HMAC Key and secret to supply to the S3 SDK tests. The key
3637
will be deleted after the test session.
@@ -45,7 +46,7 @@ def hmac_fixture():
4546

4647

4748
@pytest.fixture(scope="module")
48-
def test_bucket():
49+
def test_bucket() -> storage.Bucket:
4950
"""Yields a bucket that is deleted after the test completes."""
5051
bucket = None
5152
while bucket is None or bucket.exists():
@@ -57,7 +58,7 @@ def test_bucket():
5758

5859

5960
@pytest.fixture(scope="module")
60-
def test_blob(test_bucket):
61+
def test_blob(test_bucket: storage.Bucket) -> storage.Blob:
6162
"""Yields a blob that is deleted after the test completes."""
6263
bucket = test_bucket
6364
blob = bucket.blob(f"storage_snippets_test_sigil-{uuid.uuid4()}")
@@ -68,18 +69,20 @@ def test_blob(test_bucket):
6869
# Retry request because the created key may not be fully propagated for up
6970
# to 15s.
7071
@backoff.on_exception(backoff.constant, ClientError, interval=1, max_time=15)
71-
def test_list_buckets(hmac_fixture, test_bucket):
72-
results = list_gcs_buckets.list_gcs_buckets(
72+
def test_list_buckets(hmac_fixture: Tuple[storage.hmac_key.HMACKeyMetadata, str], test_bucket: storage.Bucket) -> None:
73+
result = list_gcs_buckets.list_gcs_buckets(
7374
google_access_key_id=hmac_fixture[0].access_id,
7475
google_access_key_secret=hmac_fixture[1],
7576
)
76-
assert test_bucket.name in results
77+
assert test_bucket.name in result
7778

7879

7980
# Retry request because the created key may not be fully propagated for up
8081
# to 15s.
8182
@backoff.on_exception(backoff.constant, ClientError, interval=1, max_time=15)
82-
def test_list_blobs(hmac_fixture, test_bucket, test_blob):
83+
def test_list_blobs(
84+
hmac_fixture: Tuple[storage.hmac_key.HMACKeyMetadata, str], test_bucket: storage.Bucket, test_blob: storage.Blob
85+
) -> None:
8386
result = list_gcs_objects.list_gcs_objects(
8487
google_access_key_id=hmac_fixture[0].access_id,
8588
google_access_key_secret=hmac_fixture[1],

0 commit comments

Comments
 (0)