1616
1717from __future__ import annotations
1818
19+ import sys
1920import time
20- from typing import Literal , NotRequired , TypedDict , Union
21+
22+ if sys .version_info >= (3 , 11 ):
23+ import typing
24+ else :
25+ import typing_extensions as typing
26+
2127from urllib .parse import urlparse
2228
2329from typesense .exceptions import ConfigError
2430from typesense .logger import logger
2531
2632
27- class NodeConfigDict (TypedDict ):
33+ class NodeConfigDict (typing . TypedDict ):
2834 """
2935 A dictionary that represents the configuration for a node in the Typesense cluster.
3036
3137 Attributes:
3238 host (str): The host name of the node.
3339 port (int): The port number of the node.
3440 path (str, optional): The path of the node.
35- protocol (Literal['http', 'https'] | str): The protocol of the node.
41+ protocol (typing. Literal['http', 'https'] | str): The protocol of the node.
3642 """
3743
3844 host : str
3945 port : int
40- path : NotRequired [str ]
41- protocol : Literal [' http' , ' https' ] | str
46+ path : typing . NotRequired [str ]
47+ protocol : typing . Literal [" http" , " https" ] | str
4248
4349
44- class ConfigDict (TypedDict ):
50+ class ConfigDict (typing . TypedDict ):
4551 """
4652 A dictionary that represents the configuration for the Typesense client.
4753
4854 Attributes:
49- nodes (list[Union[str, NodeConfigDict]]): A list of dictionaries or URLs that
55+ nodes (list[typing. Union[str, NodeConfigDict]]): A list of dictionaries or URLs that
5056 represent the nodes in the cluster.
5157
52- nearest_node (Union[str, NodeConfigDict]): A dictionary or URL
58+ nearest_node (typing. Union[str, NodeConfigDict]): A dictionary or URL
5359 that represents the nearest node to the client.
5460
5561 api_key (str): The API key to use for authentication.
@@ -65,23 +71,25 @@ class ConfigDict(TypedDict):
6571
6672 timeout_seconds (int, deprecated): The connection timeout in seconds.
6773
68- master_node (Union[str, NodeConfigDict], deprecated): A dictionary or
74+ master_node (typing. Union[str, NodeConfigDict], deprecated): A dictionary or
6975 URL that represents the master node.
7076
71- read_replica_nodes (list[Union[str, NodeConfigDict]], deprecated): A list of
77+ read_replica_nodes (list[typing. Union[str, NodeConfigDict]], deprecated): A list of
7278 dictionaries or URLs that represent the read replica nodes.
7379 """
7480
75- nodes : list [Union [str , NodeConfigDict ]]
76- nearest_node : NotRequired [Union [str , NodeConfigDict ]]
81+ nodes : list [typing . Union [str , NodeConfigDict ]]
82+ nearest_node : typing . NotRequired [typing . Union [str , NodeConfigDict ]]
7783 api_key : str
78- num_retries : NotRequired [int ]
79- interval_seconds : NotRequired [int ]
80- healthcheck_interval_seconds : NotRequired [int ]
81- verify : NotRequired [bool ]
82- timeout_seconds : NotRequired [int ] # deprecated
83- master_node : NotRequired [Union [str , NodeConfigDict ]] # deprecated
84- read_replica_nodes : NotRequired [list [Union [str , NodeConfigDict ]]] # deprecated
84+ num_retries : typing .NotRequired [int ]
85+ interval_seconds : typing .NotRequired [int ]
86+ healthcheck_interval_seconds : typing .NotRequired [int ]
87+ verify : typing .NotRequired [bool ]
88+ timeout_seconds : typing .NotRequired [int ] # deprecated
89+ master_node : typing .NotRequired [typing .Union [str , NodeConfigDict ]] # deprecated
90+ read_replica_nodes : typing .NotRequired [
91+ list [typing .Union [str , NodeConfigDict ]]
92+ ] # deprecated
8593
8694
8795class Node :
@@ -92,7 +100,7 @@ class Node:
92100 host (str): The host name of the node.
93101 port (str | int): The port number of the node.
94102 path (str): The path of the node.
95- protocol (Literal['http', 'https'] | str): The protocol of the node.
103+ protocol (typing. Literal['http', 'https'] | str): The protocol of the node.
96104 healthy (bool): Whether the node is healthy or not.
97105 """
98106
@@ -101,7 +109,7 @@ def __init__(
101109 host : str ,
102110 port : str | int ,
103111 path : str ,
104- protocol : Literal [' http' , ' https' ] | str ,
112+ protocol : typing . Literal [" http" , " https" ] | str ,
105113 ) -> None :
106114 """
107115 Initialize a Node object with the specified host, port, path, and protocol.
@@ -110,7 +118,7 @@ def __init__(
110118 host (str): The host name of the node.
111119 port (str | int): The port number of the node.
112120 path (str): The path of the node.
113- protocol (Literal['http', 'https'] | str): The protocol of the node.
121+ protocol (typing. Literal['http', 'https'] | str): The protocol of the node.
114122 """
115123 self .host = host
116124 self .port = port
@@ -208,8 +216,8 @@ def __init__(
208216
209217 def _handle_nearest_node (
210218 self ,
211- nearest_node : Union [str , NodeConfigDict , None ],
212- ) -> Union [Node , None ]:
219+ nearest_node : typing . Union [str , NodeConfigDict , None ],
220+ ) -> typing . Union [Node , None ]:
213221 """
214222 Handle the nearest node configuration.
215223
@@ -225,7 +233,7 @@ def _handle_nearest_node(
225233
226234 def _initialize_nodes (
227235 self ,
228- node : Union [str , NodeConfigDict ],
236+ node : typing . Union [str , NodeConfigDict ],
229237 ) -> Node :
230238 """
231239 Handle the initialization of a node.
@@ -286,7 +294,7 @@ def validate_required_config_fields(config_dict: ConfigDict) -> None:
286294 raise ConfigError ('`api_key` is not defined.' )
287295
288296 @staticmethod
289- def validate_nodes (nodes : list [Union [str , NodeConfigDict ]]) -> None :
297+ def validate_nodes (nodes : list [typing . Union [str , NodeConfigDict ]]) -> None :
290298 """
291299 Validate the nodes in the configuration dictionary.
292300
@@ -309,7 +317,7 @@ def validate_nodes(nodes: list[Union[str, NodeConfigDict]]) -> None:
309317 )
310318
311319 @staticmethod
312- def validate_nearest_node (nearest_node : Union [str , NodeConfigDict ]) -> None :
320+ def validate_nearest_node (nearest_node : typing . Union [str , NodeConfigDict ]) -> None :
313321 """
314322 Validate the nearest node in the configuration dictionary.
315323
0 commit comments