Skip to content

[Storage] Fix type hints for classmethods in Blob and Datalake #26611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 12, 2022
46 changes: 21 additions & 25 deletions sdk/storage/azure-storage-blob/azure/storage/blob/_blob_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from functools import partial
from io import BytesIO
from typing import (
Any, AnyStr, Dict, IO, Iterable, List, Optional, overload, Tuple, Type, TypeVar, Union,
Any, AnyStr, Dict, IO, Iterable, List, Optional, overload, Tuple, Union,
TYPE_CHECKING
)
from urllib.parse import urlparse, quote, unquote
Expand Down Expand Up @@ -66,6 +66,7 @@
)

if TYPE_CHECKING:
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
from datetime import datetime
from ._generated.models import BlockList
from ._models import (
Expand All @@ -80,8 +81,6 @@
'The require_encryption flag is set, but encryption is not supported'
' for this method.')

ClassType = TypeVar("ClassType")


class BlobClient(StorageAccountHostsMixin, StorageEncryptionMixin): # pylint: disable=too-many-public-methods
"""A client to interact with a specific blob, although that blob may not yet exist.
Expand Down Expand Up @@ -149,14 +148,13 @@ class BlobClient(StorageAccountHostsMixin, StorageEncryptionMixin): # pylint: d
:caption: Creating the BlobClient from a SAS URL to a blob.
"""
def __init__(
self, account_url, # type: str
container_name, # type: str
blob_name, # type: str
snapshot=None, # type: Optional[Union[str, Dict[str, Any]]]
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
):
# type: (...) -> None
self, account_url: str,
container_name: str,
blob_name: str,
snapshot: Optional[Union[str, Dict[str, Any]]] = None,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> None:
try:
if not account_url.lower().startswith('http'):
account_url = "https://" + account_url
Expand Down Expand Up @@ -213,12 +211,11 @@ def _encode_source_url(self, source_url):

@classmethod
def from_blob_url(
cls, # type: Type[ClassType]
blob_url, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
snapshot=None, # type: Optional[Union[str, Dict[str, Any]]]
**kwargs # type: Any
): # type: (...) -> ClassType
cls, blob_url: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
snapshot: Optional[Union[str, Dict[str, Any]]] = None,
**kwargs: Any
) -> "BlobClient":
"""Create BlobClient from a blob url. This doesn't support customized blob url with '/' in blob name.

:param str blob_url:
Expand Down Expand Up @@ -295,14 +292,13 @@ def from_blob_url(

@classmethod
def from_connection_string(
cls, # type: Type[ClassType]
conn_str, # type: str
container_name, # type: str
blob_name, # type: str
snapshot=None, # type: Optional[str]
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
): # type: (...) -> ClassType
cls, conn_str: str,
container_name: str,
blob_name: str,
snapshot: Optional[Union[str, Dict[str, Any]]] = None,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> "BlobClient":
"""Create BlobClient from a Connection String.

:param str conn_str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import functools
import warnings
from typing import ( # pylint: disable=unused-import
Any, Dict, List, Optional, TypeVar, Union,
from typing import (
Any, Dict, List, Optional, Union,
TYPE_CHECKING
)
from urllib.parse import urlparse
Expand Down Expand Up @@ -36,6 +36,7 @@
from ._serialize import get_api_version

if TYPE_CHECKING:
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
from datetime import datetime
from ._shared.models import UserDelegationKey
from ._lease import BlobLeaseClient
Expand All @@ -51,8 +52,6 @@
FilteredBlob
)

ClassType = TypeVar("ClassType")


class BlobServiceClient(StorageAccountHostsMixin, StorageEncryptionMixin):
"""A client to interact with the Blob Service at the account level.
Expand Down Expand Up @@ -119,11 +118,10 @@ class BlobServiceClient(StorageAccountHostsMixin, StorageEncryptionMixin):
"""

def __init__(
self, account_url, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
):
# type: (...) -> None
self, account_url: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> None:
try:
if not account_url.lower().startswith('http'):
account_url = "https://" + account_url
Expand All @@ -148,11 +146,10 @@ def _format_url(self, hostname):

@classmethod
def from_connection_string(
cls, # type: Type[ClassType]
conn_str, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
): # type: (...) -> ClassType
cls, conn_str: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> "BlobServiceClient":
"""Create BlobServiceClient from a Connection String.

:param str conn_str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# --------------------------------------------------------------------------

import functools
from typing import ( # pylint: disable=unused-import
Any, AnyStr, Dict, List, IO, Iterable, Iterator, Optional, overload, TypeVar, Union,
from typing import (
Any, AnyStr, Dict, List, IO, Iterable, Iterator, Optional, overload, Union,
TYPE_CHECKING
)
from urllib.parse import urlparse, quote, unquote
Expand Down Expand Up @@ -50,6 +50,7 @@
from ._serialize import get_modify_conditions, get_container_cpk_scope_info, get_api_version, get_access_conditions

if TYPE_CHECKING:
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
from datetime import datetime
from ._models import ( # pylint: disable=unused-import
PublicAccess,
Expand All @@ -70,9 +71,6 @@ def _get_blob_name(blob):
return blob


ClassType = TypeVar("ClassType")


class ContainerClient(StorageAccountHostsMixin, StorageEncryptionMixin): # pylint: disable=too-many-public-methods
"""A client to interact with a specific container, although that container
may not yet exist.
Expand Down Expand Up @@ -138,12 +136,11 @@ class ContainerClient(StorageAccountHostsMixin, StorageEncryptionMixin): # py
:caption: Creating the container client directly.
"""
def __init__(
self, account_url, # type: str
container_name, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
):
# type: (...) -> None
self, account_url: str,
container_name: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> None:
try:
if not account_url.lower().startswith('http'):
account_url = "https://" + account_url
Expand Down Expand Up @@ -182,11 +179,10 @@ def _format_url(self, hostname):

@classmethod
def from_container_url(
cls, # type: Type[ClassType]
container_url, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
): # type: (...) -> ClassType
cls, container_url: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> "ContainerClient":
"""Create ContainerClient from a container url.

:param str container_url:
Expand Down Expand Up @@ -231,12 +227,11 @@ def from_container_url(

@classmethod
def from_connection_string(
cls, # type: Type[ClassType]
conn_str, # type: str
container_name, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
): # type: (...) -> ClassType
cls, conn_str: str,
container_name: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> "ContainerClient":
"""Create ContainerClient from a Connection String.

:param str conn_str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
)

if TYPE_CHECKING:
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
from datetime import datetime
from .._models import ( # pylint: disable=unused-import
ContentSettings,
Expand Down Expand Up @@ -115,14 +116,13 @@ class BlobClient(AsyncStorageAccountHostsMixin, BlobClientBase, StorageEncryptio
:caption: Creating the BlobClient from a SAS URL to a blob.
"""
def __init__(
self, account_url, # type: str
container_name, # type: str
blob_name, # type: str
snapshot=None, # type: Optional[Union[str, Dict[str, Any]]]
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
):
# type: (...) -> None
self, account_url: str,
container_name: str,
blob_name: str,
snapshot: Optional[Union[str, Dict[str, Any]]] = None,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> None:
kwargs['retry_policy'] = kwargs.get('retry_policy') or ExponentialRetry(**kwargs)
super(BlobClient, self).__init__(
account_url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import functools
import warnings
from typing import ( # pylint: disable=unused-import
from typing import (
Any, Dict, List, Optional, Union,
TYPE_CHECKING
)
Expand Down Expand Up @@ -40,6 +40,7 @@
from ._models import ContainerPropertiesPaged, FilteredBlobPaged

if TYPE_CHECKING:
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
from datetime import datetime
from .._shared.models import UserDelegationKey
from ._lease_async import BlobLeaseClient
Expand Down Expand Up @@ -115,11 +116,10 @@ class BlobServiceClient(AsyncStorageAccountHostsMixin, BlobServiceClientBase, St
"""

def __init__(
self, account_url, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
):
# type: (...) -> None
self, account_url: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> None:
kwargs['retry_policy'] = kwargs.get('retry_policy') or ExponentialRetry(**kwargs)
super(BlobServiceClient, self).__init__(
account_url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from ._models import FilteredBlobPaged

if TYPE_CHECKING:
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
from datetime import datetime
from .._models import ( # pylint: disable=unused-import
AccessPolicy,
Expand Down Expand Up @@ -110,12 +111,11 @@ class ContainerClient(AsyncStorageAccountHostsMixin, ContainerClientBase, Storag
:caption: Creating the container client directly.
"""
def __init__(
self, account_url, # type: str
container_name, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
):
# type: (...) -> None
self, account_url: str,
container_name: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> None:
kwargs['retry_policy'] = kwargs.get('retry_policy') or ExponentialRetry(**kwargs)
super(ContainerClient, self).__init__(
account_url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from typing import ( # pylint: disable=unused-import
Any, Dict, Optional, Type, TypeVar, Union,
TYPE_CHECKING)
from typing import (
Any, Dict, Optional, Union,
TYPE_CHECKING
)

try:
from urllib.parse import quote, unquote
Expand All @@ -19,10 +20,9 @@
from ._path_client import PathClient

if TYPE_CHECKING:
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
from datetime import datetime

ClassType = TypeVar("ClassType")


class DataLakeDirectoryClient(PathClient):
"""A client to interact with the DataLake directory, even if the directory may not yet exist.
Expand Down Expand Up @@ -67,25 +67,23 @@ class DataLakeDirectoryClient(PathClient):
:caption: Creating the DataLakeServiceClient from connection string.
"""
def __init__(
self, account_url, # type: str
file_system_name, # type: str
directory_name, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
):
# type: (...) -> None
self, account_url: str,
file_system_name: str,
directory_name: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> None:
super(DataLakeDirectoryClient, self).__init__(account_url, file_system_name, path_name=directory_name,
credential=credential, **kwargs)

@classmethod
def from_connection_string(
cls, # type: Type[ClassType]
conn_str, # type: str
file_system_name, # type: str
directory_name, # type: str
credential=None, # type: Optional[Union[str, Dict[str, str], AzureNamedKeyCredential, AzureSasCredential, "TokenCredential"]] # pylint: disable=line-too-long
**kwargs # type: Any
): # type: (...) -> ClassType
cls, conn_str: str,
file_system_name: str,
directory_name: str,
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
**kwargs: Any
) -> "DataLakeDirectoryClient":
"""
Create DataLakeDirectoryClient from a Connection String.

Expand Down
Loading