Skip to content

Commit cb47de5

Browse files
[Storage] Blob Typing Final Revision (#35606)
1 parent 8326a20 commit cb47de5

File tree

65 files changed

+5426
-3674
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+5426
-3674
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: str
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: str
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore

sdk/storage/azure-storage-blob/azure/storage/blob/__init__.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import os
99

10-
from typing import Union, Iterable, AnyStr, IO, Any, Dict # pylint: disable=unused-import
10+
from typing import Any, AnyStr, cast, Dict, IO, Iterable, Optional, Union, TYPE_CHECKING
1111
from ._version import VERSION
1212
from ._blob_client import BlobClient
1313
from ._container_client import ContainerClient
@@ -18,17 +18,15 @@
1818
from ._shared_access_signature import generate_account_sas, generate_container_sas, generate_blob_sas
1919
from ._shared.policies import ExponentialRetry, LinearRetry
2020
from ._shared.response_handlers import PartialBatchErrorException
21-
from ._shared.models import(
21+
from ._shared.models import (
2222
LocationMode,
2323
ResourceTypes,
2424
AccountSasPermissions,
2525
StorageErrorCode,
2626
UserDelegationKey,
2727
Services
2828
)
29-
from ._generated.models import (
30-
RehydratePriority,
31-
)
29+
from ._generated.models import RehydratePriority
3230
from ._models import (
3331
BlobType,
3432
BlockState,
@@ -67,15 +65,18 @@
6765
)
6866
from ._list_blobs_helper import BlobPrefix
6967

68+
if TYPE_CHECKING:
69+
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
70+
7071
__version__ = VERSION
7172

7273

7374
def upload_blob_to_url(
74-
blob_url, # type: str
75-
data, # type: Union[Iterable[AnyStr], IO[AnyStr]]
76-
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
77-
**kwargs):
78-
# type: (...) -> Dict[str, Any]
75+
blob_url: str,
76+
data: Union[Iterable[AnyStr], IO[AnyStr]],
77+
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
78+
**kwargs: Any
79+
) -> Dict[str, Any]:
7980
"""Upload data to a given URL
8081
8182
The data will be uploaded as a block blob.
@@ -125,10 +126,10 @@ def upload_blob_to_url(
125126
:rtype: dict(str, Any)
126127
"""
127128
with BlobClient.from_blob_url(blob_url, credential=credential) as client:
128-
return client.upload_blob(data=data, blob_type=BlobType.BlockBlob, **kwargs)
129+
return cast(BlobClient, client).upload_blob(data=data, blob_type=BlobType.BLOCKBLOB, **kwargs)
129130

130131

131-
def _download_to_stream(client, handle, **kwargs):
132+
def _download_to_stream(client: BlobClient, handle: IO[bytes], **kwargs: Any) -> None:
132133
"""
133134
Download data to specified open file-handle.
134135
@@ -140,11 +141,11 @@ def _download_to_stream(client, handle, **kwargs):
140141

141142

142143
def download_blob_from_url(
143-
blob_url, # type: str
144-
output, # type: str
145-
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
146-
**kwargs):
147-
# type: (...) -> None
144+
blob_url: str,
145+
output: Union[str, IO[bytes]],
146+
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
147+
**kwargs: Any
148+
) -> None:
148149
"""Download the contents of a blob to a local file or stream.
149150
150151
:param str blob_url:
@@ -194,7 +195,7 @@ def download_blob_from_url(
194195
overwrite = kwargs.pop('overwrite', False)
195196
with BlobClient.from_blob_url(blob_url, credential=credential) as client:
196197
if hasattr(output, 'write'):
197-
_download_to_stream(client, output, **kwargs)
198+
_download_to_stream(client, cast(IO[bytes], output), **kwargs)
198199
else:
199200
if not overwrite and os.path.isfile(output):
200201
raise ValueError(f"The file '{output}' already exists.")

0 commit comments

Comments
 (0)