@@ -199,6 +199,17 @@ def to_dict(self):
199199 return cls
200200
201201
202+ def cast_to_string (value : Any ) -> str :
203+ """Casts a value to a string."""
204+ if isinstance (value , str ):
205+ return value
206+
207+ if isinstance (value , (int , float )):
208+ return str (value )
209+
210+ raise TypeError (f"{ value } cannot be casted to string." )
211+
212+
202213def key_is_string (func : Callable [..., Any ]) -> Any :
203214 """Decorator to check if the key is a string.
204215 Will attempt to cast the key to a string if it is not.
@@ -208,12 +219,7 @@ def wrapper(self: Any, key: Any, *args: Any, **kwargs: Any) -> Any:
208219 if key is None :
209220 raise ValueError ("Key cannot be None." )
210221
211- if not isinstance (key , str ):
212- if not isinstance (key , (int , float )):
213- raise TypeError (f"{ key } cannot be casted to string." )
214-
215- key = str (key )
216-
222+ key = cast_to_string (key )
217223 return func (self , key , * args , ** kwargs )
218224
219225 return wrapper
@@ -270,12 +276,7 @@ def wrapper(self: Any, data: Any, *args: Any, **kwargs: Any) -> Any:
270276 raise TypeError (f"Decorator found unsupported type: { type (data )} ." )
271277
272278 for key , value in items :
273- if not isinstance (key , str ):
274- if not isinstance (key , (int , float )):
275- raise TypeError (f"{ key } cannot be casted to string." )
276-
277- key = str (key )
278-
279+ key = cast_to_string (key )
279280 data_dict [key ] = value
280281
281282 return func (self , data_dict , * args , ** kwargs )
@@ -655,7 +656,7 @@ def doc_insert(
655656 data : dict [str , Any ] = {},
656657 ** kwargs : Any ,
657658) -> dict [str , Any ]:
658- """Inserts a document into a collection."""
659+ """Inserts a document into a collection. Returns document metadata. """
659660 result : dict [str , Any ] = db .insert_document (
660661 collection , {** data , "_id" : id }, overwrite = True , ** kwargs
661662 )
0 commit comments