Skip to content

Commit 8f2c3d9

Browse files
weirongw23-msftRena Chen
authored and
Rena Chen
committed
[Storage] [Typing] [File Datalake] azure-storage-file-datalake (#39691)
1 parent ccec3ed commit 8f2c3d9

36 files changed

+3144
-2339
lines changed

sdk/storage/azure-storage-blob/azure/storage/blob/_shared/base_client.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@
6565

6666

6767
class StorageAccountHostsMixin(object):
68+
6869
_client: Any
70+
_hosts: Dict[str, str]
71+
6972
def __init__(
7073
self,
7174
parsed_url: Any,
@@ -74,7 +77,7 @@ def __init__(
7477
**kwargs: Any
7578
) -> None:
7679
self._location_mode = kwargs.get("_location_mode", LocationMode.PRIMARY)
77-
self._hosts = kwargs.get("_hosts")
80+
self._hosts = kwargs.get("_hosts", {})
7881
self.scheme = parsed_url.scheme
7982
self._is_localhost = False
8083

@@ -93,7 +96,7 @@ def __init__(
9396
if self.scheme.lower() != "https" and hasattr(self.credential, "get_token"):
9497
raise ValueError("Token credential is only supported with HTTPS.")
9598

96-
secondary_hostname = None
99+
secondary_hostname = ""
97100
if hasattr(self.credential, "account_name"):
98101
self.account_name = self.credential.account_name
99102
secondary_hostname = f"{self.credential.account_name}-secondary.{service_name}.{SERVICE_HOST_BASE}"
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
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-file-datalake/azure/storage/filedatalake/_data_lake_directory_client.py

+54-59
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77

88
import functools
99
from typing import (
10-
Any, Dict, Optional, Union,
10+
Any, cast, Dict, Optional, Union,
1111
TYPE_CHECKING
1212
)
1313
from urllib.parse import quote, unquote
14-
1514
from typing_extensions import Self
1615

1716
from azure.core.paging import ItemPaged
@@ -23,7 +22,8 @@
2322
from ._list_paths_helper import PathPropertiesPaged
2423
from ._models import DirectoryProperties, FileProperties
2524
from ._path_client import PathClient
26-
from ._shared.base_client import TransportWrapper, parse_connection_str
25+
from ._path_client_helpers import _parse_rename_path
26+
from ._shared.base_client import parse_connection_str, TransportWrapper
2727

2828
if TYPE_CHECKING:
2929
from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential, TokenCredential
@@ -37,12 +37,6 @@ class DataLakeDirectoryClient(PathClient):
3737
For operations relating to a specific subdirectory or file under the directory, a directory client or file client
3838
can be retrieved using the :func:`~get_sub_directory_client` or :func:`~get_file_client` functions.
3939
40-
:ivar str url:
41-
The full endpoint URL to the file system, including SAS token if used.
42-
:ivar str primary_endpoint:
43-
The full primary endpoint URL.
44-
:ivar str primary_hostname:
45-
The hostname of the primary endpoint.
4640
:param str account_url:
4741
The URI to the storage account.
4842
:param file_system_name:
@@ -64,7 +58,7 @@ class DataLakeDirectoryClient(PathClient):
6458
~azure.core.credentials.AzureNamedKeyCredential or
6559
~azure.core.credentials.AzureSasCredential or
6660
~azure.core.credentials.TokenCredential or
67-
str or dict[str, str] or None
61+
str or Dict[str, str] or None
6862
:keyword str api_version:
6963
The Storage API version to use for requests. Default value is the most recent service version that is
7064
compatible with the current SDK. Setting to an older version may result in reduced feature compatibility.
@@ -81,6 +75,14 @@ class DataLakeDirectoryClient(PathClient):
8175
:dedent: 4
8276
:caption: Creating the DataLakeServiceClient from connection string.
8377
"""
78+
79+
url: str
80+
"""The full endpoint URL to the file system, including SAS token if used."""
81+
primary_endpoint: str
82+
"""The full primary endpoint URL."""
83+
primary_hostname: str
84+
"""The hostname of the primary endpoint."""
85+
8486
def __init__(
8587
self, account_url: str,
8688
file_system_name: str,
@@ -93,12 +95,12 @@ def __init__(
9395

9496
@classmethod
9597
def from_connection_string(
96-
cls, conn_str: str,
97-
file_system_name: str,
98-
directory_name: str,
99-
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
100-
**kwargs: Any
101-
) -> Self:
98+
cls, conn_str: str,
99+
file_system_name: str,
100+
directory_name: str,
101+
credential: Optional[Union[str, Dict[str, str], "AzureNamedKeyCredential", "AzureSasCredential", "TokenCredential"]] = None, # pylint: disable=line-too-long
102+
**kwargs: Any
103+
) -> Self:
102104
"""
103105
Create DataLakeDirectoryClient from a Connection String.
104106
@@ -120,7 +122,7 @@ def from_connection_string(
120122
~azure.core.credentials.AzureNamedKeyCredential or
121123
~azure.core.credentials.AzureSasCredential or
122124
~azure.core.credentials.TokenCredential or
123-
str or dict[str, str] or None
125+
str or Dict[str, str] or None
124126
:param directory_name:
125127
The name of directory to interact with. The directory is under file system.
126128
:type directory_name: str
@@ -136,15 +138,16 @@ def from_connection_string(
136138
credential=credential, **kwargs)
137139

138140
@distributed_trace
139-
def create_directory(self, metadata=None, # type: Optional[Dict[str, str]]
140-
**kwargs):
141-
# type: (...) -> Dict[str, Union[str, datetime]]
141+
def create_directory(
142+
self, metadata: Optional[Dict[str, str]] = None,
143+
**kwargs: Any
144+
) -> Dict[str, Union[str, "datetime"]]:
142145
"""
143146
Create a new directory.
144147
145148
:param metadata:
146149
Name-value pairs associated with the file as metadata.
147-
:type metadata: dict(str, str)
150+
:type metadata: Dict[str, str]
148151
:keyword ~azure.storage.filedatalake.ContentSettings content_settings:
149152
ContentSettings object used to set path properties.
150153
:keyword lease:
@@ -210,7 +213,7 @@ def create_directory(self, metadata=None, # type: Optional[Dict[str, str]]
210213
see `here <https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/storage/azure-storage-file-datalake
211214
#other-client--per-operation-configuration>`_.
212215
:return: A dictionary of response headers.
213-
:rtype: dict[str, Union[str, datetime]]
216+
:rtype: Dict[str, Union[str, ~datetime.datetime]]
214217
215218
.. admonition:: Example:
216219
@@ -224,8 +227,7 @@ def create_directory(self, metadata=None, # type: Optional[Dict[str, str]]
224227
return self._create('directory', metadata=metadata, **kwargs)
225228

226229
@distributed_trace
227-
def delete_directory(self, **kwargs):
228-
# type: (...) -> None
230+
def delete_directory(self, **kwargs: Any) -> Dict[str, Any]: # pylint: disable=delete-operation-wrong-return-type
229231
"""
230232
Marks the specified directory for deletion.
231233
@@ -256,8 +258,8 @@ def delete_directory(self, **kwargs):
256258
This value is not tracked or validated on the client. To configure client-side network timesouts
257259
see `here <https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/storage/azure-storage-file-datalake
258260
#other-client--per-operation-configuration>`_.
259-
:returns: None.
260-
:rtype: None
261+
:returns: A dictionary of response headers.
262+
:rtype: Dict[str, Any]
261263
262264
.. admonition:: Example:
263265
@@ -271,8 +273,7 @@ def delete_directory(self, **kwargs):
271273
return self._delete(recursive=True, **kwargs)
272274

273275
@distributed_trace
274-
def get_directory_properties(self, **kwargs):
275-
# type: (**Any) -> DirectoryProperties
276+
def get_directory_properties(self, **kwargs: Any) -> DirectoryProperties:
276277
"""Returns all user-defined metadata, standard HTTP properties, and
277278
system properties for the directory. It does not return the content of the directory.
278279
@@ -333,11 +334,10 @@ def get_directory_properties(self, **kwargs):
333334
headers = kwargs.pop('headers', {})
334335
headers['x-ms-upn'] = str(upn)
335336
kwargs['headers'] = headers
336-
return self._get_path_properties(cls=deserialize_dir_properties, **kwargs)
337+
return cast(DirectoryProperties, self._get_path_properties(cls=deserialize_dir_properties, **kwargs))
337338

338339
@distributed_trace
339-
def exists(self, **kwargs):
340-
# type: (**Any) -> bool
340+
def exists(self, **kwargs: Any) -> bool:
341341
"""
342342
Returns True if a directory exists and returns False otherwise.
343343
@@ -353,8 +353,7 @@ def exists(self, **kwargs):
353353
return self._exists(**kwargs)
354354

355355
@distributed_trace
356-
def rename_directory(self, new_name, **kwargs):
357-
# type: (str, **Any) -> DataLakeDirectoryClient
356+
def rename_directory(self, new_name: str, **kwargs: Any) -> "DataLakeDirectoryClient":
358357
"""
359358
Rename the source directory.
360359
@@ -422,7 +421,8 @@ def rename_directory(self, new_name, **kwargs):
422421
:dedent: 4
423422
:caption: Rename the source directory.
424423
"""
425-
new_file_system, new_path, new_dir_sas = self._parse_rename_path(new_name)
424+
new_file_system, new_path, new_dir_sas = _parse_rename_path(
425+
new_name, self.file_system_name, self._query_str, self._raw_credential)
426426

427427
new_directory_client = DataLakeDirectoryClient(
428428
f"{self.scheme}://{self.primary_hostname}", new_file_system, directory_name=new_path,
@@ -433,10 +433,11 @@ def rename_directory(self, new_name, **kwargs):
433433
return new_directory_client
434434

435435
@distributed_trace
436-
def create_sub_directory(self, sub_directory, # type: Union[DirectoryProperties, str]
437-
metadata=None, # type: Optional[Dict[str, str]]
438-
**kwargs):
439-
# type: (...) -> DataLakeDirectoryClient
436+
def create_sub_directory(
437+
self, sub_directory: Union[DirectoryProperties, str],
438+
metadata: Optional[Dict[str, str]] = None,
439+
**kwargs: Any
440+
) -> "DataLakeDirectoryClient":
440441
"""
441442
Create a subdirectory and return the subdirectory client to be interacted with.
442443
@@ -446,7 +447,7 @@ def create_sub_directory(self, sub_directory, # type: Union[DirectoryProperties
446447
:type sub_directory: str or ~azure.storage.filedatalake.DirectoryProperties
447448
:param metadata:
448449
Name-value pairs associated with the file as metadata.
449-
:type metadata: dict(str, str)
450+
:type metadata: Dict[str, str]
450451
:keyword ~azure.storage.filedatalake.ContentSettings content_settings:
451452
ContentSettings object used to set path properties.
452453
:keyword lease:
@@ -519,9 +520,10 @@ def create_sub_directory(self, sub_directory, # type: Union[DirectoryProperties
519520
return subdir
520521

521522
@distributed_trace
522-
def delete_sub_directory(self, sub_directory, # type: Union[DirectoryProperties, str]
523-
**kwargs):
524-
# type: (...) -> DataLakeDirectoryClient
523+
def delete_sub_directory( # pylint: disable=delete-operation-wrong-return-type
524+
self, sub_directory: Union[DirectoryProperties, str],
525+
**kwargs: Any
526+
) -> "DataLakeDirectoryClient":
525527
"""
526528
Marks the specified subdirectory for deletion.
527529
@@ -564,9 +566,7 @@ def delete_sub_directory(self, sub_directory, # type: Union[DirectoryProperties
564566
return subdir
565567

566568
@distributed_trace
567-
def create_file(self, file, # type: Union[FileProperties, str]
568-
**kwargs):
569-
# type: (...) -> DataLakeFileClient
569+
def create_file(self, file: Union[FileProperties, str], **kwargs: Any) -> DataLakeFileClient:
570570
"""
571571
Create a new file and return the file client to be interacted with.
572572
@@ -578,7 +578,7 @@ def create_file(self, file, # type: Union[FileProperties, str]
578578
ContentSettings object used to set path properties.
579579
:keyword metadata:
580580
Name-value pairs associated with the file as metadata.
581-
:type metadata: dict(str, str)
581+
:type metadata: Dict[str, str]
582582
:keyword lease:
583583
Required if the file has an active lease. Value can be a DataLakeLeaseClient object
584584
or the lease ID as a string.
@@ -688,7 +688,6 @@ def get_paths(
688688
:returns: An iterable (auto-paging) response of PathProperties.
689689
:rtype: ~azure.core.paging.ItemPaged[~azure.storage.filedatalake.PathProperties]
690690
"""
691-
timeout = kwargs.pop('timeout', None)
692691
hostname = self._hosts[self._location_mode]
693692
url = f"{self.scheme}://{hostname}/{quote(self.file_system_name)}"
694693
client = self._build_generated_client(url)
@@ -702,9 +701,7 @@ def get_paths(
702701
command, recursive, path=self.path_name, max_results=max_results,
703702
upn=upn, page_iterator_class=PathPropertiesPaged, **kwargs)
704703

705-
def get_file_client(self, file # type: Union[FileProperties, str]
706-
):
707-
# type: (...) -> DataLakeFileClient
704+
def get_file_client(self, file: Union[FileProperties, str]) -> DataLakeFileClient:
708705
"""Get a client to interact with the specified file.
709706
710707
The file need not already exist.
@@ -716,9 +713,9 @@ def get_file_client(self, file # type: Union[FileProperties, str]
716713
:returns: A DataLakeFileClient.
717714
:rtype: ~azure.storage.filedatalake.DataLakeFileClient
718715
"""
719-
try:
716+
if isinstance(file, FileProperties):
720717
file_path = file.get('name')
721-
except AttributeError:
718+
else:
722719
file_path = self.path_name + '/' + str(file)
723720

724721
_pipeline = Pipeline(
@@ -730,9 +727,7 @@ def get_file_client(self, file # type: Union[FileProperties, str]
730727
api_version=self.api_version,
731728
_hosts=self._hosts, _configuration=self._config, _pipeline=_pipeline)
732729

733-
def get_sub_directory_client(self, sub_directory # type: Union[DirectoryProperties, str]
734-
):
735-
# type: (...) -> DataLakeDirectoryClient
730+
def get_sub_directory_client(self, sub_directory: Union[DirectoryProperties, str]) -> "DataLakeDirectoryClient":
736731
"""Get a client to interact with the specified subdirectory of the current directory.
737732
738733
The sub subdirectory need not already exist.
@@ -744,14 +739,14 @@ def get_sub_directory_client(self, sub_directory # type: Union[DirectoryPropert
744739
:returns: A DataLakeDirectoryClient.
745740
:rtype: ~azure.storage.filedatalake.DataLakeDirectoryClient
746741
"""
747-
try:
742+
if isinstance(sub_directory, DirectoryProperties):
748743
subdir_path = sub_directory.get('name')
749-
except AttributeError:
744+
else:
750745
subdir_path = self.path_name + '/' + str(sub_directory)
751746

752747
_pipeline = Pipeline(
753-
transport=TransportWrapper(self._pipeline._transport), # pylint: disable = protected-access
754-
policies=self._pipeline._impl_policies # pylint: disable = protected-access
748+
transport=TransportWrapper(self._pipeline._transport), # pylint: disable=protected-access
749+
policies=self._pipeline._impl_policies # pylint: disable=protected-access
755750
)
756751
return DataLakeDirectoryClient(
757752
self.url, self.file_system_name, directory_name=subdir_path, credential=self._raw_credential,

0 commit comments

Comments
 (0)