Skip to content

Commit 4450786

Browse files
committed
chore(storage): improve type annotations in client and bucket modules
1 parent 705e23d commit 4450786

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

packages/google-cloud-storage/google/cloud/storage/bucket.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
import copy
1919
import datetime
2020
import json
21+
from typing import Any, Optional, Set, Tuple, Union
2122
import warnings
2223
from urllib.parse import urlsplit
2324

2425
from google.api_core import datetime_helpers
2526
from google.api_core.iam import Policy
27+
from google.api_core.retry import Retry
2628
from google.cloud._helpers import _datetime_to_rfc3339, _rfc3339_nanos_to_datetime
2729
from google.cloud.exceptions import NotFound
2830

29-
from google.cloud.storage import _signing
31+
from google.cloud.storage import Client, _signing
3032
from google.cloud.storage._helpers import (
3133
_NOW,
3234
_UTC,
@@ -65,6 +67,7 @@
6567
DEFAULT_RETRY_IF_ETAG_IN_JSON,
6668
DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
6769
DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED,
70+
ConditionalRetryPolicy,
6871
)
6972

7073
_UBLA_BPO_ENABLED_MESSAGE = (
@@ -846,12 +849,12 @@ def from_string(cls, uri, client=None):
846849

847850
def blob(
848851
self,
849-
blob_name,
850-
chunk_size=None,
851-
encryption_key=None,
852-
kms_key_name=None,
853-
generation=None,
854-
):
852+
blob_name: str,
853+
chunk_size: Optional[int] = None,
854+
encryption_key: Optional[bytes] = None,
855+
kms_key_name: Optional[str] = None,
856+
generation: Optional[int] = None,
857+
) -> Blob:
855858
"""Factory constructor for blob object.
856859
857860
.. note::
@@ -862,9 +865,11 @@ def blob(
862865
:param blob_name: The name of the blob to be instantiated.
863866
864867
:type chunk_size: int
865-
:param chunk_size: The size of a chunk of data whenever iterating
866-
(in bytes). This must be a multiple of 256 KB per
867-
the API specification.
868+
:param chunk_size:
869+
(Optional) The size of a chunk of data whenever iterating (in bytes).
870+
This must be a multiple of 256 KB per the API specification. If not
871+
specified, the chunk_size of the blob itself is used. If that is not
872+
specified, a default value of 40 MB is used.
868873
869874
:type encryption_key: bytes
870875
:param encryption_key:
@@ -1289,21 +1294,21 @@ def path(self):
12891294

12901295
def get_blob(
12911296
self,
1292-
blob_name,
1293-
client=None,
1294-
encryption_key=None,
1295-
generation=None,
1296-
if_etag_match=None,
1297-
if_etag_not_match=None,
1298-
if_generation_match=None,
1299-
if_generation_not_match=None,
1300-
if_metageneration_match=None,
1301-
if_metageneration_not_match=None,
1302-
timeout=_DEFAULT_TIMEOUT,
1303-
retry=DEFAULT_RETRY,
1304-
soft_deleted=None,
1305-
**kwargs,
1306-
):
1297+
blob_name: str,
1298+
client: Optional[Client] = None,
1299+
encryption_key: Optional[bytes] = None,
1300+
generation: Optional[int] = None,
1301+
if_etag_match: Optional[Union[str, Set[str]]] = None,
1302+
if_etag_not_match: Optional[Union[str, Set[str]]] = None,
1303+
if_generation_match: Optional[int] = None,
1304+
if_generation_not_match: Optional[int] = None,
1305+
if_metageneration_match: Optional[int] = None,
1306+
if_metageneration_not_match: Optional[int] = None,
1307+
timeout: Optional[Union[float, Tuple[float, float]]] = _DEFAULT_TIMEOUT,
1308+
retry: Optional[Union[Retry, ConditionalRetryPolicy]] = DEFAULT_RETRY,
1309+
soft_deleted: Optional[bool] = None,
1310+
**kwargs: Any,
1311+
) -> Optional[Blob]:
13071312
"""Get a blob object by name.
13081313
13091314
See a [code sample](https://cloud.google.com/storage/docs/samples/storage-get-metadata#storage_get_metadata-python)

packages/google-cloud-storage/google/cloud/storage/client.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import functools
2222
import json
2323
import os
24+
from typing import Optional, Sequence, Tuple, Union
2425
import warnings
2526

2627
import google.api_core.client_options
2728
from google.api_core import page_iterator
29+
from google.api_core.retry import Retry
2830
from google.auth.credentials import AnonymousCredentials
2931
from google.auth.transport import mtls
3032
from google.cloud._helpers import _LocalStack
@@ -58,7 +60,7 @@
5860
from google.cloud.storage.bucket import Bucket, _blobs_page_start, _item_to_blob
5961
from google.cloud.storage.constants import _DEFAULT_TIMEOUT
6062
from google.cloud.storage.hmac_key import HMACKeyMetadata
61-
from google.cloud.storage.retry import DEFAULT_RETRY
63+
from google.cloud.storage.retry import DEFAULT_RETRY, ConditionalRetryPolicy
6264

6365
_marker = object()
6466

@@ -881,15 +883,15 @@ def _bucket_arg_to_bucket(self, bucket_or_name, generation=None):
881883

882884
def get_bucket(
883885
self,
884-
bucket_or_name,
885-
timeout=_DEFAULT_TIMEOUT,
886-
if_metageneration_match=None,
887-
if_metageneration_not_match=None,
888-
retry=DEFAULT_RETRY,
886+
bucket_or_name: Union[Bucket, str],
887+
timeout: Optional[Union[float, Tuple[float, float]]] = _DEFAULT_TIMEOUT,
888+
if_metageneration_match: Optional[int] = None,
889+
if_metageneration_not_match: Optional[int] = None,
890+
retry: Optional[Union[Retry, ConditionalRetryPolicy]] = DEFAULT_RETRY,
889891
*,
890-
generation=None,
891-
soft_deleted=None,
892-
):
892+
generation: Optional[int] = None,
893+
soft_deleted: Optional[bool] = None,
894+
) -> Bucket:
893895
"""Retrieve a bucket via a GET request.
894896
895897
See [API reference docs](https://cloud.google.com/storage/docs/json_api/v1/buckets/get) and a [code sample](https://cloud.google.com/storage/docs/samples/storage-get-bucket-metadata#storage_get_bucket_metadata-python).
@@ -1012,18 +1014,18 @@ def lookup_bucket(
10121014

10131015
def create_bucket(
10141016
self,
1015-
bucket_or_name,
1016-
requester_pays=None,
1017-
project=None,
1018-
user_project=None,
1019-
location=None,
1020-
data_locations=None,
1021-
predefined_acl=None,
1022-
predefined_default_object_acl=None,
1023-
enable_object_retention=False,
1024-
timeout=_DEFAULT_TIMEOUT,
1025-
retry=DEFAULT_RETRY,
1026-
):
1017+
bucket_or_name: Union[Bucket, str],
1018+
requester_pays: Optional[bool] = None,
1019+
project: Optional[str] = None,
1020+
user_project: Optional[str] = None,
1021+
location: Optional[str] = None,
1022+
data_locations: Optional[Sequence[str]] = None,
1023+
predefined_acl: Optional[str] = None,
1024+
predefined_default_object_acl: Optional[str] = None,
1025+
enable_object_retention: Optional[bool] = False,
1026+
timeout: Optional[Union[float, Tuple[float, float]]] = _DEFAULT_TIMEOUT,
1027+
retry: Optional[Union[Retry, ConditionalRetryPolicy]] = DEFAULT_RETRY,
1028+
) -> Bucket:
10271029
"""Create a new bucket via a POST request.
10281030
10291031
See [API reference docs](https://cloud.google.com/storage/docs/json_api/v1/buckets/insert) and a [code sample](https://cloud.google.com/storage/docs/samples/storage-create-bucket#storage_create_bucket-python).

0 commit comments

Comments
 (0)