Skip to content
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

[Storage]fix type annotation #20096

Merged
merged 2 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from io import BytesIO
from typing import ( # pylint: disable=unused-import
Union, Optional, Any, IO, Iterable, AnyStr, Dict, List, Tuple,
TYPE_CHECKING
)
TYPE_CHECKING,
TypeVar, Type)

try:
from urllib.parse import urlparse, quote, unquote
Expand Down Expand Up @@ -74,6 +74,8 @@
'The require_encryption flag is set, but encryption is not supported'
' for this method.')

ClassType = TypeVar("ClassType")


class BlobClient(StorageAccountHostsMixin): # 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 @@ -203,7 +205,7 @@ def _encode_source_url(self, source_url):

@classmethod
def from_blob_url(cls, blob_url, credential=None, snapshot=None, **kwargs):
# type: (str, Optional[Any], Optional[Union[str, Dict[str, Any]]], Any) -> BlobClient
# type: (Type[ClassType], str, Optional[Any], Optional[Union[str, Dict[str, Any]]], Any) -> ClassType
"""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 @@ -273,13 +275,14 @@ def from_blob_url(cls, blob_url, credential=None, snapshot=None, **kwargs):

@classmethod
def from_connection_string(
cls, conn_str, # type: str
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[Any]
**kwargs # type: Any
): # type: (...) -> BlobClient
): # type: (...) -> ClassType
"""Create BlobClient from a Connection String.

:param str conn_str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import warnings
from typing import ( # pylint: disable=unused-import
Union, Optional, Any, Iterable, Dict, List,
TYPE_CHECKING
)
TYPE_CHECKING,
TypeVar)


try:
Expand Down Expand Up @@ -52,6 +52,8 @@
FilteredBlob
)

ClassType = TypeVar("ClassType")


class BlobServiceClient(StorageAccountHostsMixin):
"""A client to interact with the Blob Service at the account level.
Expand Down Expand Up @@ -145,10 +147,11 @@ def _format_url(self, hostname):

@classmethod
def from_connection_string(
cls, conn_str, # type: str
cls, # type: Type[ClassType]
conn_str, # type: str
credential=None, # type: Optional[Any]
**kwargs # type: Any
): # type: (...) -> BlobServiceClient
): # type: (...) -> ClassType
"""Create BlobServiceClient from a Connection String.

:param str conn_str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import functools
from typing import ( # pylint: disable=unused-import
Union, Optional, Any, Iterable, AnyStr, Dict, List, Tuple, IO, Iterator,
TYPE_CHECKING
)
TYPE_CHECKING,
TypeVar)


try:
Expand Down Expand Up @@ -69,6 +69,9 @@ def _get_blob_name(blob):
return blob


ClassType = TypeVar("ClassType")
Copy link
Contributor

@tasherif-msft tasherif-msft Oct 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be something like ClassType = TypeVar("ContainerClient")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on the doc in typing
class TypeVar(_Final, _Immutable, _root=True):

    """Type variable.

    Usage::

      T = TypeVar('T')  # Can be anything
      A = TypeVar('A', str, bytes)  # Must be str or bytes
      ....................................................

I think T should have the same name as the 'T' in TypeVar?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why not ContainerClientClass = TypeVar("ContainerClientClass")
Anyway this is just a generic I just thought the naming could be better. I will approve - feel free to change it to the name above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeVar is just to make a type of a parameter generic, so the code will automatically know if the return type should be aio.ContainerClient or ContainerClient. I think we don't have to make it that specific, ClassType make it easier for batch search.



class ContainerClient(StorageAccountHostsMixin): # 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 @@ -171,7 +174,7 @@ def _format_url(self, hostname):

@classmethod
def from_container_url(cls, container_url, credential=None, **kwargs):
# type: (str, Optional[Any], Any) -> ContainerClient
# type: (Type[ClassType], str, Optional[Any], Any) -> ClassType
"""Create ContainerClient from a container url.

:param str container_url:
Expand Down Expand Up @@ -214,11 +217,12 @@ def from_container_url(cls, container_url, credential=None, **kwargs):

@classmethod
def from_connection_string(
cls, conn_str, # type: str
cls, # type: Type[ClassType]
conn_str, # type: str
container_name, # type: str
credential=None, # type: Optional[Any]
**kwargs # type: Any
): # type: (...) -> ContainerClient
): # type: (...) -> ClassType
"""Create ContainerClient from a Connection String.

:param str conn_str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from typing import Any
from typing import Any, TypeVar

try:
from urllib.parse import quote, unquote
Expand All @@ -16,6 +16,8 @@
from ._models import DirectoryProperties, FileProperties
from ._path_client import PathClient

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,12 +69,13 @@ def __init__(

@classmethod
def from_connection_string(
cls, conn_str, # type: str
cls, # type: Type[ClassType]
conn_str, # type: str
file_system_name, # type: str
directory_name, # type: str
credential=None, # type: Optional[Any]
**kwargs # type: Any
): # type: (...) -> DataLakeDirectoryClient
): # type: (...) -> ClassType
"""
Create DataLakeDirectoryClient from a Connection String.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# license information.
# --------------------------------------------------------------------------
from io import BytesIO
from typing import Any
from typing import Any, TypeVar

try:
from urllib.parse import quote, unquote
Expand All @@ -27,6 +27,8 @@
from ._deserialize import process_storage_error, deserialize_file_properties
from ._models import FileProperties, DataLakeFileQueryError

ClassType = TypeVar("ClassType")


class DataLakeFileClient(PathClient):
"""A client to interact with the DataLake file, even if the file may not yet exist.
Expand Down Expand Up @@ -76,12 +78,13 @@ def __init__(

@classmethod
def from_connection_string(
cls, conn_str, # type: str
cls, # type: Type[ClassType]
conn_str, # type: str
file_system_name, # type: str
file_path, # type: str
credential=None, # type: Optional[Any]
**kwargs # type: Any
): # type: (...) -> DataLakeFileClient
): # type: (...) -> ClassType
"""
Create DataLakeFileClient from a Connection String.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from typing import Optional, Dict, Any
from typing import Optional, Dict, Any, TypeVar

try:
from urllib.parse import urlparse
Expand All @@ -22,6 +22,8 @@
from ._models import UserDelegationKey, FileSystemPropertiesPaged, LocationMode
from ._serialize import convert_dfs_url_to_blob_url

ClassType = TypeVar("ClassType")


class DataLakeServiceClient(StorageAccountHostsMixin):
"""A client to interact with the DataLake Service at the account level.
Expand Down Expand Up @@ -116,10 +118,11 @@ def _format_url(self, hostname):

@classmethod
def from_connection_string(
cls, conn_str, # type: str
cls, # type: Type[ClassType]
conn_str, # type: str
credential=None, # type: Optional[Any]
**kwargs # type: Any
): # type: (...) -> DataLakeServiceClient
): # type: (...) -> ClassType
"""
Create DataLakeServiceClient from a Connection String.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# license information.
# --------------------------------------------------------------------------
import functools
from typing import Optional, Any, Union

from typing import Optional, Any, Union, TypeVar

try:
from urllib.parse import urlparse, quote, unquote
Expand All @@ -31,6 +30,9 @@
from ._deserialize import deserialize_path_properties, process_storage_error, is_file_path


ClassType = TypeVar("ClassType")


class FileSystemClient(StorageAccountHostsMixin):
"""A client to interact with a specific file system, even if that file system
may not yet exist.
Expand Down Expand Up @@ -133,11 +135,12 @@ def close(self):

@classmethod
def from_connection_string(
cls, conn_str, # type: str
cls, # type: Type[ClassType]
conn_str, # type: str
file_system_name, # type: str
credential=None, # type: Optional[Any]
**kwargs # type: Any
): # type: (...) -> FileSystemClient
): # type: (...) -> ClassType
"""
Create FileSystemClient from a Connection String.

Expand Down