17
17
)
18
18
from pymilvus .client .utils import get_params , is_vector_type
19
19
from pymilvus .exceptions import (
20
- DataTypeNotMatchException ,
21
20
ErrorCode ,
22
21
MilvusException ,
23
22
ParamError ,
@@ -235,16 +234,11 @@ def insert(
235
234
Returns:
236
235
Dict: Number of rows that were inserted and the inserted primary key list.
237
236
"""
238
- # If no data provided, we cannot input anything
239
237
if isinstance (data , Dict ):
240
238
data = [data ]
239
+ validate_params (insert_data = (data , List ))
241
240
242
- msg = "wrong type of argument 'data',"
243
- msg += f"expected 'Dict' or list of 'Dict', got '{ type (data ).__name__ } '"
244
-
245
- if not isinstance (data , List ):
246
- raise TypeError (msg )
247
-
241
+ # If no data provided, we cannot input anything
248
242
if len (data ) == 0 :
249
243
return {"insert_count" : 0 , "ids" : []}
250
244
@@ -287,21 +281,16 @@ def upsert(
287
281
Returns:
288
282
Dict: Number of rows that were upserted.
289
283
"""
290
- # If no data provided, we cannot input anything
291
284
if isinstance (data , Dict ):
292
285
data = [data ]
293
286
294
- msg = "wrong type of argument 'data',"
295
- msg += f"expected 'Dict' or list of 'Dict', got '{ type (data ).__name__ } '"
296
-
297
- if not isinstance (data , List ):
298
- raise TypeError (msg )
287
+ validate_params (upsert_data = (data , List ))
299
288
289
+ # If no data provided, we cannot input anything
300
290
if len (data ) == 0 :
301
291
return {"upsert_count" : 0 }
302
292
303
293
conn = self ._get_connection ()
304
- # Upsert into the collection.
305
294
try :
306
295
res = conn .upsert_rows (
307
296
collection_name , data , partition_name = partition_name , timeout = timeout , ** kwargs
@@ -468,17 +457,15 @@ def query(
468
457
Returns:
469
458
List[dict]: A list of result dicts, vectors are not included.
470
459
"""
471
- if filter and not isinstance (filter , str ):
472
- raise DataTypeNotMatchException (message = ExceptionsMessage .ExprType % type (filter ))
460
+ validate_params (filter = (filter , str , True ))
473
461
474
- if filter and ids is not None :
462
+ if filter and ids :
475
463
raise ParamError (message = ExceptionsMessage .AmbiguousQueryFilterParam )
476
464
477
465
if isinstance (ids , (int , str )):
478
466
ids = [ids ]
479
467
480
468
conn = self ._get_connection ()
481
-
482
469
if ids :
483
470
try :
484
471
schema_dict = conn .describe_collection (collection_name , timeout = timeout , ** kwargs )
@@ -517,8 +504,7 @@ def query_iterator(
517
504
timeout : Optional [float ] = None ,
518
505
** kwargs ,
519
506
):
520
- if filter is not None and not isinstance (filter , str ):
521
- raise DataTypeNotMatchException (message = ExceptionsMessage .ExprType % type (filter ))
507
+ validate_params (filter = (filter , str , True ))
522
508
523
509
conn = self ._get_connection ()
524
510
# set up schema for iterator
@@ -624,8 +610,7 @@ def search_iterator(
624
610
raise ex from ex
625
611
626
612
# following is the old code for search_iterator V1
627
- if filter is not None and not isinstance (filter , str ):
628
- raise DataTypeNotMatchException (message = ExceptionsMessage .ExprType % type (filter ))
613
+ validate_params (filter = (filter , str , True ))
629
614
630
615
# set up schema for iterator
631
616
try :
@@ -755,7 +740,7 @@ def get(
755
740
def delete (
756
741
self ,
757
742
collection_name : str ,
758
- ids : Optional [Union [list , str , int ]] = None ,
743
+ ids : Optional [Union [List , str , int ]] = None ,
759
744
timeout : Optional [float ] = None ,
760
745
filter : Optional [str ] = None ,
761
746
partition_name : Optional [str ] = None ,
@@ -785,28 +770,20 @@ def delete(
785
770
Dict: with key 'deleted_count' and value number of rows that were deleted.
786
771
"""
787
772
pks = kwargs .get ("pks" , [])
773
+ validate_params (pks = (pks , int , str , List [int | str ]))
788
774
if isinstance (pks , (int , str )):
789
775
pks = [pks ]
790
776
791
- for pk in pks :
792
- if not isinstance (pk , (int , str )):
793
- msg = f"wrong type of argument pks, expect list, int or str, got '{ type (pk ).__name__ } '"
794
- raise TypeError (msg )
795
-
796
- if ids is not None :
777
+ validate_params (ids = (ids , int , str , List [int | str ], True ))
778
+ if ids :
797
779
if isinstance (ids , (int , str )):
798
780
pks .append (ids )
799
- elif isinstance (ids , list ):
800
- for id in ids :
801
- if not isinstance (id , (int , str )):
802
- msg = f"wrong type of argument ids, expect list, int or str, got '{ type (id ).__name__ } '"
803
- raise TypeError (msg )
781
+ elif isinstance (ids , List ):
782
+ validate_params (ids = (ids , List [int | str ]))
804
783
pks .extend (ids )
805
- else :
806
- msg = f"wrong type of argument ids, expect list, int or str, got '{ type (ids ).__name__ } '"
807
- raise TypeError (msg )
808
784
809
785
# validate ambiguous delete filter param before describe collection rpc
786
+ validate_params (filter = (filter , str , True ))
810
787
if filter and len (pks ) > 0 :
811
788
raise ParamError (message = ExceptionsMessage .AmbiguousDeleteFilterParam )
812
789
@@ -822,8 +799,6 @@ def delete(
822
799
raise ex from ex
823
800
expr = self ._pack_pks_expr (schema_dict , pks )
824
801
else :
825
- if not isinstance (filter , str ):
826
- raise DataTypeNotMatchException (message = ExceptionsMessage .ExprType % type (filter ))
827
802
expr = filter
828
803
829
804
ret_pks = []
@@ -1240,9 +1215,6 @@ def get_partition_stats(
1240
1215
self , collection_name : str , partition_name : str , timeout : Optional [float ] = None , ** kwargs
1241
1216
) -> Dict :
1242
1217
conn = self ._get_connection ()
1243
- if not isinstance (partition_name , str ):
1244
- msg = f"wrong type of argument 'partition_name', str expected, got '{ type (partition_name ).__name__ } '"
1245
- raise TypeError (msg )
1246
1218
ret = conn .get_partition_stats (collection_name , partition_name , timeout = timeout , ** kwargs )
1247
1219
result = {stat .key : stat .value for stat in ret }
1248
1220
if "row_count" in result :
0 commit comments