6161 None ,
6262]
6363
64+ # One line of an import response. ``ImportResponse`` is a union of lists, one per
65+ # return mode, so the helpers below build a list of these and ``import_`` casts it
66+ # to the list type its overloads promise.
67+ _ImportResponseItem = typing .Union [
68+ ImportResponseWithDoc [TDoc ],
69+ ImportResponseWithId ,
70+ ImportResponseWithDocAndId [TDoc ],
71+ ImportResponseSuccess ,
72+ ImportResponseFail [TDoc ],
73+ ]
74+
6475
6576class AsyncDocuments (typing .Generic [TDoc ]):
6677 """
@@ -125,12 +136,14 @@ async def create(
125136 Returns:
126137 TDoc: The created document.
127138 """
128- dirty_values_parameters = dirty_values_parameters or {}
129- dirty_values_parameters ["action" ] = "create"
139+ write_parameters : typing .Dict [str , object ] = {
140+ ** (dirty_values_parameters or {}),
141+ "action" : "create" ,
142+ }
130143 response = await self .api_call .post (
131144 self ._endpoint_path (),
132145 body = document ,
133- params = dirty_values_parameters ,
146+ params = write_parameters ,
134147 as_json = True ,
135148 entity_type = typing .Dict [str , str ],
136149 )
@@ -154,7 +167,14 @@ async def create_many(
154167 The list of import responses.
155168 """
156169 logger .warn ("`create_many` is deprecated: please use `import_`." )
157- return await self .import_ (documents , dirty_values_parameters )
170+ # Dirty values parameters are a subset of the write parameters.
171+ return await self .import_ (
172+ documents ,
173+ typing .cast (
174+ typing .Optional [DocumentWriteParameters ],
175+ dirty_values_parameters ,
176+ ),
177+ )
158178
159179 async def upsert (
160180 self ,
@@ -172,12 +192,14 @@ async def upsert(
172192 Returns:
173193 TDoc: The upserted document.
174194 """
175- dirty_values_parameters = dirty_values_parameters or {}
176- dirty_values_parameters ["action" ] = "upsert"
195+ write_parameters : typing .Dict [str , object ] = {
196+ ** (dirty_values_parameters or {}),
197+ "action" : "upsert" ,
198+ }
177199 response = await self .api_call .post (
178200 self ._endpoint_path (),
179201 body = document ,
180- params = dirty_values_parameters ,
202+ params = write_parameters ,
181203 as_json = True ,
182204 entity_type = typing .Dict [str , str ],
183205 )
@@ -199,12 +221,14 @@ async def update(
199221 Returns:
200222 UpdateByFilterResponse: The response containing information about the update.
201223 """
202- dirty_values_parameters = dirty_values_parameters or {}
203- dirty_values_parameters ["action" ] = "update"
224+ update_parameters : typing .Dict [str , object ] = {
225+ ** (dirty_values_parameters or {}),
226+ "action" : "update" ,
227+ }
204228 response : UpdateByFilterResponse = await self .api_call .patch (
205229 self ._endpoint_path (),
206230 body = document ,
207- params = dirty_values_parameters ,
231+ params = update_parameters ,
208232 entity_type = UpdateByFilterResponse ,
209233 )
210234 return response
@@ -301,9 +325,14 @@ async def import_(
301325 return await self ._import_raw (documents , import_parameters )
302326
303327 if batch_size :
304- return await self ._batch_import (documents , import_parameters , batch_size )
305-
306- return await self ._bulk_import (documents , import_parameters )
328+ response_objs = await self ._batch_import (
329+ documents ,
330+ import_parameters ,
331+ batch_size ,
332+ )
333+ else :
334+ response_objs = await self ._bulk_import (documents , import_parameters )
335+ return typing .cast (ImportResponse [TDoc ], response_objs )
307336
308337 async def export (
309338 self ,
@@ -410,9 +439,9 @@ async def _batch_import(
410439 documents : typing .List [TDoc ],
411440 import_parameters : _ImportParameters ,
412441 batch_size : int ,
413- ) -> ImportResponse [ TDoc ]:
442+ ) -> typing . List [ _ImportResponseItem [ TDoc ] ]:
414443 """Import documents in batches."""
415- response_objs : ImportResponse [ TDoc ] = []
444+ response_objs : typing . List [ _ImportResponseItem [ TDoc ] ] = []
416445 for batch_index in range (0 , len (documents ), batch_size ):
417446 batch = documents [batch_index : batch_index + batch_size ]
418447 api_response = await self ._bulk_import (batch , import_parameters )
@@ -423,7 +452,7 @@ async def _bulk_import(
423452 self ,
424453 documents : typing .List [TDoc ],
425454 import_parameters : _ImportParameters ,
426- ) -> ImportResponse [ TDoc ]:
455+ ) -> typing . List [ _ImportResponseItem [ TDoc ] ]:
427456 """Import a list of documents in bulk."""
428457 document_strs = [json .dumps (doc ) for doc in documents ]
429458 if not document_strs :
@@ -439,9 +468,12 @@ async def _bulk_import(
439468 )
440469 return self ._parse_import_response (res )
441470
442- def _parse_import_response (self , response : str ) -> ImportResponse [TDoc ]:
471+ def _parse_import_response (
472+ self ,
473+ response : str ,
474+ ) -> typing .List [_ImportResponseItem [TDoc ]]:
443475 """Parse the import response string into a list of response objects."""
444- response_objs : typing .List [ImportResponse ] = []
476+ response_objs : typing .List [_ImportResponseItem [ TDoc ] ] = []
445477 for res_obj_str in response .split ("\n " ):
446478 try :
447479 res_obj_json = json .loads (res_obj_str )
0 commit comments