77
88import 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
1111from ._version import VERSION
1212from ._blob_client import BlobClient
1313from ._container_client import ContainerClient
1818from ._shared_access_signature import generate_account_sas , generate_container_sas , generate_blob_sas
1919from ._shared .policies import ExponentialRetry , LinearRetry
2020from ._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
3230from ._models import (
3331 BlobType ,
3432 BlockState ,
6765)
6866from ._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
7374def 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
142143def 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