From 18dc104a79aea810da64e549cadb0d1a828d940c Mon Sep 17 00:00:00 2001 From: Ryan Koo Date: Thu, 15 Aug 2024 18:31:13 -0400 Subject: [PATCH] sdk/python: fix type hint for python 3.8 compatibility - Fixes type hints for Python 3.8 compatibility (replace tuple, list, dict with Tuple, List, Dict from typing) Signed-off-by: Ryan Koo changes Signed-off-by: Ryan Koo --- python/aistore/sdk/authn/authn_client.py | 6 +++--- python/aistore/sdk/bucket.py | 6 +++--- python/aistore/sdk/client.py | 6 ++++-- python/aistore/sdk/multiobj/object_group.py | 2 +- python/aistore/sdk/request_client.py | 8 ++++---- python/aistore/sdk/types.py | 10 +++++----- python/tests/utils.py | 8 +++++--- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/python/aistore/sdk/authn/authn_client.py b/python/aistore/sdk/authn/authn_client.py index bcef5a9c3e0..96b8013219a 100644 --- a/python/aistore/sdk/authn/authn_client.py +++ b/python/aistore/sdk/authn/authn_client.py @@ -3,7 +3,7 @@ # import logging -from typing import Optional, Union +from typing import Optional, Tuple, Union from aistore.sdk.request_client import RequestClient from aistore.sdk.const import ( HTTP_METHOD_POST, @@ -30,7 +30,7 @@ class AuthNClient: endpoint (str): AuthN service endpoint URL. skip_verify (bool, optional): If True, skip SSL certificate verification. Defaults to False. ca_cert (str, optional): Path to a CA certificate file for SSL verification. - timeout (Union[float, tuple[float, float], None], optional): Request timeout in seconds; a single float + timeout (Union[float, Tuple[float, float], None], optional): Request timeout in seconds; a single float for both connect/read timeouts (e.g., 5.0), a tuple for separate connect/read timeouts (e.g., (3.0, 10.0)), or None to disable timeout. token (str, optional): Authorization token. @@ -41,7 +41,7 @@ def __init__( endpoint: str, skip_verify: bool = False, ca_cert: Optional[str] = None, - timeout: Optional[Union[float, tuple[float, float]]] = None, + timeout: Optional[Union[float, Tuple[float, float]]] = None, token: Optional[str] = None, ): logger.info("Initializing AuthNClient") diff --git a/python/aistore/sdk/bucket.py b/python/aistore/sdk/bucket.py index 1355a50cd5c..1d1d8030245 100644 --- a/python/aistore/sdk/bucket.py +++ b/python/aistore/sdk/bucket.py @@ -835,7 +835,7 @@ def object(self, obj_name: str, props: ObjectProps = None) -> Object: def objects( self, - obj_names: list = None, + obj_names: List = None, obj_range: ObjectRange = None, obj_template: str = None, ) -> ObjectGroup: @@ -861,8 +861,8 @@ def make_request( self, method: str, action: str, - value: dict = None, - params: dict = None, + value: Dict = None, + params: Dict = None, ) -> requests.Response: """ Use the bucket's client to make a request to the bucket endpoint on the AIS server diff --git a/python/aistore/sdk/client.py b/python/aistore/sdk/client.py index 03e3b2f2f16..cacc5317dd8 100644 --- a/python/aistore/sdk/client.py +++ b/python/aistore/sdk/client.py @@ -4,6 +4,8 @@ from __future__ import annotations # pylint: disable=unused-variable +from typing import Optional, Tuple, Union + from aistore.sdk.bucket import Bucket from aistore.sdk.const import ( PROVIDER_AIS, @@ -28,7 +30,7 @@ class Client: endpoint (str): AIStore endpoint skip_verify (bool, optional): If True, skip SSL certificate verification. Defaults to False. ca_cert (str, optional): Path to a CA certificate file for SSL verification. - timeout (Union[float, tuple[float, float], None], optional): Request timeout in seconds; a single float + timeout (Union[float, Tuple[float, float], None], optional): Request timeout in seconds; a single float for both connect/read timeouts (e.g., 5.0), a tuple for separate connect/read timeouts (e.g., (3.0, 10.0)), or None to disable timeout. token (str, optional): Authorization token. @@ -39,7 +41,7 @@ def __init__( endpoint: str, skip_verify: bool = False, ca_cert: str = None, - timeout: float | tuple[float, float] | None = None, + timeout: Optional[Union[float, Tuple[float, float]]] = None, token: str = None, ): self._request_client = RequestClient( diff --git a/python/aistore/sdk/multiobj/object_group.py b/python/aistore/sdk/multiobj/object_group.py index 031cb8842c2..a2ffe71ada6 100644 --- a/python/aistore/sdk/multiobj/object_group.py +++ b/python/aistore/sdk/multiobj/object_group.py @@ -48,7 +48,7 @@ class ObjectGroup(AISSource): def __init__( self, bck: "Bucket", - obj_names: list = None, + obj_names: List = None, obj_range: ObjectRange = None, obj_template: str = None, ): diff --git a/python/aistore/sdk/request_client.py b/python/aistore/sdk/request_client.py index dc0021f08cb..56365e1ef6a 100644 --- a/python/aistore/sdk/request_client.py +++ b/python/aistore/sdk/request_client.py @@ -3,7 +3,7 @@ # import os from urllib.parse import urljoin, urlencode -from typing import TypeVar, Type, Any, Dict +from typing import Optional, TypeVar, Tuple, Type, Union, Any, Dict from requests import session, Session, Response from aistore.sdk.const import ( @@ -29,7 +29,7 @@ class RequestClient: endpoint (str): AIStore endpoint skip_verify (bool, optional): If True, skip SSL certificate verification. Defaults to False. ca_cert (str, optional): Path to a CA certificate file for SSL verification. - timeout (Union[float, tuple[float, float], None], optional): Request timeout in seconds; a single float + timeout (Union[float, Tuple[float, float], None], optional): Request timeout in seconds; a single float for both connect/read timeouts (e.g., 5.0), a tuple for separate connect/read timeouts (e.g., (3.0, 10.0)), or None to disable timeout. token (str, optional): Authorization token. @@ -40,7 +40,7 @@ def __init__( endpoint: str, skip_verify: bool = False, ca_cert: str = None, - timeout=None, + timeout: Optional[Union[float, Tuple[float, float]]] = None, token: str = None, ): self._endpoint = endpoint @@ -146,7 +146,7 @@ def request( method: str, path: str, endpoint: str = None, - headers: dict = None, + headers: Dict = None, **kwargs, ) -> Response: """ diff --git a/python/aistore/sdk/types.py b/python/aistore/sdk/types.py index c3e234aaa81..08a53184b45 100644 --- a/python/aistore/sdk/types.py +++ b/python/aistore/sdk/types.py @@ -512,7 +512,7 @@ class PrefetchMsg(BaseModel): API message structure for prefetching objects from remote buckets. """ - object_selection: dict + object_selection: Dict continue_on_err: bool latest: bool blob_threshold: int = None @@ -534,7 +534,7 @@ class TCMultiObj(BaseModel): to_bck: BucketModel tc_msg: TCBckMsg = None continue_on_err: bool - object_selection: dict + object_selection: Dict def as_dict(self): dict_rep = self.object_selection @@ -557,7 +557,7 @@ class ArchiveMultiObj(BaseModel): include_source_name = False allow_append = False continue_on_err = False - object_selection: dict + object_selection: Dict def as_dict(self): dict_rep = self.object_selection @@ -729,7 +729,7 @@ class NodeStatsV322(BaseModel): ais_version: str build_time: str k8s_pod_name: str - sys_info: dict + sys_info: Dict smap_version: str @@ -747,7 +747,7 @@ class NodeStats(BaseModel): ais_version: str build_time: str k8s_pod_name: str - sys_info: dict + sys_info: Dict smap_version: str reserved1: Optional[str] = "" reserved2: Optional[str] = "" diff --git a/python/tests/utils.py b/python/tests/utils.py index 4f88a1beebf..3db21275b74 100644 --- a/python/tests/utils.py +++ b/python/tests/utils.py @@ -6,6 +6,8 @@ import io from pathlib import Path +from typing import Dict, List + from aistore.sdk import Client from aistore.sdk.const import UTF_ENCODING from aistore.sdk.errors import ErrBckNotFound @@ -16,7 +18,7 @@ def random_string(length: int = 10): return "".join(random.choices(string.ascii_lowercase, k=length)) -def string_to_dict(input_string: str) -> dict: +def string_to_dict(input_string: str) -> Dict: pairs = input_string.split(", ") result_dict = { key_value.split("=")[0]: key_value.split("=")[1] for key_value in pairs @@ -104,7 +106,7 @@ def generate_random_content(min_size: int = 1024, max_size: int = 10240) -> byte size = random.randint(min_size, max_size) return os.urandom(size) - def generate_files(num_files: int, num_extensions: int, dest_dir: str) -> list: + def generate_files(num_files: int, num_extensions: int, dest_dir: str) -> List: files_list = [] filenames_list = [] @@ -124,7 +126,7 @@ def generate_files(num_files: int, num_extensions: int, dest_dir: str) -> list: return files_list, extension_list - def create_tarballs(min_shard_size: int, dest_dir: str, files_list: list) -> None: + def create_tarballs(min_shard_size: int, dest_dir: str, files_list: List) -> None: num_input_shards = 0 current_size = 0 dest_dir_path = Path(dest_dir).resolve()