1+ """
2+ Functionality for preprocessing parameters in the Typesense Python client library.
3+
4+ This module contains utility functions for converting various data types to strings and
5+ processing parameter lists and dictionaries. These functions are used to prepare
6+ data for API requests to Typesense.
7+
8+ Key features:
9+ - Convert individual values (int, str, bool) to strings
10+ - Process lists of parameters into comma-separated strings
11+ - Stringify search parameter dictionaries
12+
13+ Functions:
14+ stringify: Convert a single value to a string.
15+ process_param_list: Convert a list of parameters to a comma-separated string.
16+ stringify_search_params: Convert a dictionary of search parameters to strings.
17+
18+ Types:
19+ _ListTypes: Type alias for a list of strings, integers, or booleans.
20+ _Types: Type alias for a single string, integer, or boolean.
21+ ParamSchema: Type alias for a dictionary of search parameters.
22+ StringifiedParamSchema: Type alias for a dictionary of stringified search parameters.
23+
24+ Dependencies:
25+ - typesense.exceptions: Provides InvalidParameter exception
26+ - typing or typing_extensions: For type hinting
27+
28+ Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
29+ """
30+
131import sys
232
333from typesense .exceptions import InvalidParameter
737else :
838 import typing_extensions as typing
939
10-
1140_ListTypes = typing .List [typing .Union [str , int , bool ]]
12-
1341_Types = typing .Union [int , str , bool ]
14-
1542ParamSchema : typing .TypeAlias = typing .Dict [
1643 str ,
1744 typing .Union [
1845 _Types ,
1946 _ListTypes ,
2047 ],
2148]
22-
23-
2449StringifiedParamSchema : typing .TypeAlias = typing .Dict [str , str ]
2550
2651
2752def stringify (argument : _Types ) -> str :
53+ """
54+ Convert a single value to a string.
55+
56+ Args:
57+ argument (_Types): The value to be converted to a string.
58+
59+ Returns:
60+ str: The stringified version of the input.
61+
62+ Raises:
63+ InvalidParameter: If the input is not a string, integer, or boolean.
64+
65+ Examples:
66+ >>> stringify(True)
67+ 'true'
68+ >>> stringify(42)
69+ '42'
70+ >>> stringify("Hello")
71+ 'Hello'
72+ """
2873 if not isinstance (argument , (str , int , bool )):
2974 raise InvalidParameter (
3075 f"Value { argument } is not a string, integer, or boolean." ,
@@ -69,6 +114,9 @@ def stringify_search_params(parameter_dict: ParamSchema) -> StringifiedParamSche
69114 """
70115 Convert the search parameters to strings.
71116
117+ This function takes a dictionary of search parameters and converts all values
118+ to their string representations. List values are converted to comma-separated strings.
119+
72120 Args:
73121 parameter_dict (ParamSchema): The search parameters.
74122
@@ -86,8 +134,7 @@ def stringify_search_params(parameter_dict: ParamSchema) -> StringifiedParamSche
86134 >>> stringify_search_params({"a": [True, False, True], "b": [1, 2, 3]})
87135 {"a": "true,false,true", "b": "1,2,3"}
88136 """
89- stringified_params = {}
90-
137+ stringified_params : StringifiedParamSchema = {}
91138 for key , param_value in parameter_dict .items ():
92139 if isinstance (param_value , list ):
93140 stringified_params [key ] = process_param_list (param_value )
@@ -97,5 +144,4 @@ def stringify_search_params(parameter_dict: ParamSchema) -> StringifiedParamSche
97144 raise InvalidParameter (
98145 f"Value { param_value } is not a string, integer, or boolean" ,
99146 )
100-
101147 return stringified_params
0 commit comments