@@ -785,7 +785,7 @@ async def bulk_write(
785
785
786
786
write_concern = self ._write_concern_for (session )
787
787
788
- def process_for_bulk (request : _WriteOp ) -> bool :
788
+ def process_for_bulk (request : Union [ _DocumentType , RawBSONDocument , _WriteOp ] ) -> bool :
789
789
try :
790
790
return request ._add_to_bulk (blk )
791
791
except AttributeError :
@@ -810,27 +810,32 @@ async def _insert_one(
810
810
) -> Any :
811
811
"""Internal helper for inserting a single document."""
812
812
write_concern = write_concern or self .write_concern
813
+ acknowledged = write_concern .acknowledged
813
814
command = {"insert" : self .name , "ordered" : ordered , "documents" : [doc ]}
814
815
if comment is not None :
815
816
command ["comment" ] = comment
816
817
817
818
async def _insert_command (
818
- session : Optional [AsyncClientSession ], conn : AsyncConnection
819
+ session : Optional [AsyncClientSession ], conn : AsyncConnection , retryable_write : bool
819
820
) -> None :
820
821
if bypass_doc_val is not None :
821
822
command ["bypassDocumentValidation" ] = bypass_doc_val
823
+
822
824
result = await conn .command (
823
825
self ._database .name ,
824
826
command ,
825
827
write_concern = write_concern ,
826
828
codec_options = self ._write_response_codec_options ,
827
829
session = session ,
828
830
client = self ._database .client ,
831
+ retryable_write = retryable_write ,
829
832
)
830
833
831
834
_check_write_command_response (result )
832
835
833
- await self ._database .client ._retryable_write (_insert_command , session , operation = _Op .INSERT )
836
+ await self ._database .client ._retryable_write (
837
+ acknowledged , _insert_command , session , operation = _Op .INSERT
838
+ )
834
839
835
840
if not isinstance (doc , RawBSONDocument ):
836
841
return doc .get ("_id" )
@@ -959,7 +964,7 @@ async def insert_many(
959
964
raise TypeError ("documents must be a non-empty list" )
960
965
inserted_ids : list [ObjectId ] = []
961
966
962
- def process_for_bulk (document : Union [_DocumentType , RawBSONDocument ]) -> bool :
967
+ def process_for_bulk (document : Union [_DocumentType , RawBSONDocument , _WriteOp ]) -> bool :
963
968
"""A generator that validates documents and handles _ids."""
964
969
common .validate_is_document_type ("document" , document )
965
970
if not isinstance (document , RawBSONDocument ):
@@ -989,6 +994,7 @@ async def _update(
989
994
array_filters : Optional [Sequence [Mapping [str , Any ]]] = None ,
990
995
hint : Optional [_IndexKeyHint ] = None ,
991
996
session : Optional [AsyncClientSession ] = None ,
997
+ retryable_write : bool = False ,
992
998
let : Optional [Mapping [str , Any ]] = None ,
993
999
sort : Optional [Mapping [str , Any ]] = None ,
994
1000
comment : Optional [Any ] = None ,
@@ -1051,6 +1057,7 @@ async def _update(
1051
1057
codec_options = self ._write_response_codec_options ,
1052
1058
session = session ,
1053
1059
client = self ._database .client ,
1060
+ retryable_write = retryable_write ,
1054
1061
)
1055
1062
).copy ()
1056
1063
_check_write_command_response (result )
@@ -1090,7 +1097,7 @@ async def _update_retryable(
1090
1097
"""Internal update / replace helper."""
1091
1098
1092
1099
async def _update (
1093
- session : Optional [AsyncClientSession ], conn : AsyncConnection
1100
+ session : Optional [AsyncClientSession ], conn : AsyncConnection , retryable_write : bool
1094
1101
) -> Optional [Mapping [str , Any ]]:
1095
1102
return await self ._update (
1096
1103
conn ,
@@ -1106,12 +1113,14 @@ async def _update(
1106
1113
array_filters = array_filters ,
1107
1114
hint = hint ,
1108
1115
session = session ,
1116
+ retryable_write = retryable_write ,
1109
1117
let = let ,
1110
1118
sort = sort ,
1111
1119
comment = comment ,
1112
1120
)
1113
1121
1114
1122
return await self ._database .client ._retryable_write (
1123
+ (write_concern or self .write_concern ).acknowledged and not multi ,
1115
1124
_update ,
1116
1125
session ,
1117
1126
operation ,
@@ -1501,6 +1510,7 @@ async def _delete(
1501
1510
collation : Optional [_CollationIn ] = None ,
1502
1511
hint : Optional [_IndexKeyHint ] = None ,
1503
1512
session : Optional [AsyncClientSession ] = None ,
1513
+ retryable_write : bool = False ,
1504
1514
let : Optional [Mapping [str , Any ]] = None ,
1505
1515
comment : Optional [Any ] = None ,
1506
1516
) -> Mapping [str , Any ]:
@@ -1540,6 +1550,7 @@ async def _delete(
1540
1550
codec_options = self ._write_response_codec_options ,
1541
1551
session = session ,
1542
1552
client = self ._database .client ,
1553
+ retryable_write = retryable_write ,
1543
1554
)
1544
1555
_check_write_command_response (result )
1545
1556
return result
@@ -1560,7 +1571,7 @@ async def _delete_retryable(
1560
1571
"""Internal delete helper."""
1561
1572
1562
1573
async def _delete (
1563
- session : Optional [AsyncClientSession ], conn : AsyncConnection
1574
+ session : Optional [AsyncClientSession ], conn : AsyncConnection , retryable_write : bool
1564
1575
) -> Mapping [str , Any ]:
1565
1576
return await self ._delete (
1566
1577
conn ,
@@ -1572,11 +1583,13 @@ async def _delete(
1572
1583
collation = collation ,
1573
1584
hint = hint ,
1574
1585
session = session ,
1586
+ retryable_write = retryable_write ,
1575
1587
let = let ,
1576
1588
comment = comment ,
1577
1589
)
1578
1590
1579
1591
return await self ._database .client ._retryable_write (
1592
+ (write_concern or self .write_concern ).acknowledged and not multi ,
1580
1593
_delete ,
1581
1594
session ,
1582
1595
operation = _Op .DELETE ,
@@ -3221,7 +3234,7 @@ async def _find_and_modify(
3221
3234
write_concern = self ._write_concern_for_cmd (cmd , session )
3222
3235
3223
3236
async def _find_and_modify_helper (
3224
- session : Optional [AsyncClientSession ], conn : AsyncConnection
3237
+ session : Optional [AsyncClientSession ], conn : AsyncConnection , retryable_write : bool
3225
3238
) -> Any :
3226
3239
acknowledged = write_concern .acknowledged
3227
3240
if array_filters is not None :
@@ -3247,13 +3260,15 @@ async def _find_and_modify_helper(
3247
3260
write_concern = write_concern ,
3248
3261
collation = collation ,
3249
3262
session = session ,
3263
+ retryable_write = retryable_write ,
3250
3264
user_fields = _FIND_AND_MODIFY_DOC_FIELDS ,
3251
3265
)
3252
3266
_check_write_command_response (out )
3253
3267
3254
3268
return out .get ("value" )
3255
3269
3256
3270
return await self ._database .client ._retryable_write (
3271
+ write_concern .acknowledged ,
3257
3272
_find_and_modify_helper ,
3258
3273
session ,
3259
3274
operation = _Op .FIND_AND_MODIFY ,
0 commit comments