Skip to content

Commit 87fb1c1

Browse files
committed
attempt fix: tests
1 parent 247ed1c commit 87fb1c1

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

arango/collection.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,18 @@ def _prep_from_doc(
169169
else:
170170
return doc_id, doc_id, {"If-Match": rev}
171171

172-
def _ensure_key_in_body(self, body: Json) -> Json:
173-
"""Return the document body with "_key" field populated.
172+
def _ensure_key_in_body(self, body: Json, index: Optional[int] = None) -> Json:
173+
"""Return the document body with "_key" field populated. If
174+
neither "_key" or "_id" exist, set "_key" value to **index**,
175+
where **index** is the document's position in the sequence.
174176
175177
:param body: Document body.
176178
:type body: dict
179+
:param index: Document index value in the original list of documents.
180+
:param index: int | None
177181
:return: Document body with "_key" field.
178182
:rtype: dict
179-
:raise arango.exceptions.DocumentParseError: On missing ID and key.
183+
:raise arango.exceptions.DocumentParseError: On missing _key, _id, & index.
180184
"""
181185
if "_key" in body:
182186
return body
@@ -185,29 +189,24 @@ def _ensure_key_in_body(self, body: Json) -> Json:
185189
body = body.copy()
186190
body["_key"] = doc_id[len(self._id_prefix) :]
187191
return body
192+
elif index:
193+
body = body.copy()
194+
body["_key"] = str(index)
195+
return body
196+
188197
raise DocumentParseError('field "_key" or "_id" required')
189198

190-
def _ensure_key_from_id(self, body: Json, index: Optional[int] = None) -> Json:
199+
def _ensure_key_from_id(self, body: Json) -> Json:
191200
"""Return the body with "_key" field if it has "_id" field.
192-
If it has neither, set the "_key" value to i, where i
193-
is the document's index position in the sequence.
194-
195201
:param body: Document body.
196202
:type body: dict
197-
:param index: Document index value in the original list of documents.
198-
:param index: int | None
199203
:return: Document body with "_key" field if it has "_id" field.
200204
:rtype: dict
201205
"""
202206
if "_id" in body and "_key" not in body:
203207
doc_id = self._validate_id(body["_id"])
204208
body = body.copy()
205209
body["_key"] = doc_id[len(self._id_prefix) :]
206-
207-
if "_id" not in body and "_key" not in body:
208-
body = body.copy()
209-
body["_key"] = str(index)
210-
211210
return body
212211

213212
@property
@@ -2002,7 +2001,7 @@ def import_bulk(
20022001
:raise arango.exceptions.DocumentInsertError: If import fails.
20032002
"""
20042003
documents = [
2005-
self._ensure_key_from_id(doc, i) for i, doc in enumerate(documents, 1)
2004+
self._ensure_key_in_body(doc, i) for i, doc in enumerate(documents, 1)
20062005
]
20072006

20082007
params: Params = {"type": "array", "collection": self.name}

arango/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import logging
1010
from contextlib import contextmanager
11-
from typing import Any, Iterator, List, Optional, Union
11+
from typing import Any, Iterator, List, Optional, Sequence, Union
1212

1313
from arango.exceptions import DocumentParseError
1414
from arango.typings import Json
@@ -85,8 +85,8 @@ def is_none_or_str(obj: Any) -> bool:
8585

8686

8787
def get_batches(
88-
l: List[Any], batch_size: Optional[int] = None
89-
) -> Union[List[List[Any]], Iterator[List[Any]]]:
88+
l: Sequence[Json], batch_size: Optional[int] = None
89+
) -> Union[List[Sequence[Json]], Iterator[Sequence[Json]]]:
9090
"""Generator to split a list in batches
9191
of (maximum) **batch_size** elements each.
9292
If **batch_size** is invalid, return entire
@@ -100,7 +100,7 @@ def get_batches(
100100
if batch_size is None or batch_size <= 0 or batch_size >= len(l):
101101
return [l]
102102

103-
def generator() -> Iterator[List[Any]]:
103+
def generator() -> Iterator[Sequence[Json]]:
104104
n = int(batch_size) # type: ignore # (false positive)
105105
for i in range(0, len(l), n):
106106
yield l[i : i + n]

0 commit comments

Comments
 (0)