Skip to content

Commit c94e0cc

Browse files
rllinrllin
authored andcommitted
raise error for repeat uuid
1 parent e1a8715 commit c94e0cc

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

labelbox/schema/bulk_import_request.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import logging
33
import time
44
from pathlib import Path
5+
from typing import Any
56
from typing import BinaryIO
7+
from typing import Dict
68
from typing import Iterable
9+
from typing import Set
710
from typing import Tuple
811
from typing import Union
912

@@ -217,6 +220,7 @@ def create_from_objects(cls, client, project_id: str, name: str,
217220
Returns:
218221
BulkImportRequest object
219222
"""
223+
_validate_ndjson(predictions)
220224
data_str = ndjson.dumps(predictions)
221225
data = data_str.encode('utf-8')
222226
file_name = _make_file_name(project_id, name)
@@ -264,8 +268,7 @@ def create_from_local_file(cls,
264268
# by iterating through the file so we only store
265269
# each line in memory rather than the entire file
266270
try:
267-
for line in reader:
268-
pass
271+
_validate_ndjson(reader)
269272
except ValueError:
270273
raise ValueError(f"{file} is not a valid ndjson file")
271274
else:
@@ -274,3 +277,21 @@ def create_from_local_file(cls,
274277
response_data = _send_create_file_command(client, request_data,
275278
file_name, file_data)
276279
return cls(client, response_data["createBulkImportRequest"])
280+
281+
282+
class NdjsonError(Exception):
283+
pass
284+
285+
286+
class UuidError(NdjsonError):
287+
pass
288+
289+
290+
def _validate_ndjson(lines: Iterable[Dict[str, Any]]) -> None:
291+
uuids: Set[str] = set()
292+
for line in lines:
293+
uuid = line['uuid']
294+
if uuid in uuids:
295+
raise UuidError(f'{uuid} already used in this import job, '
296+
'must be unique for the project.')
297+
uuids.add(line['uuid'])

tests/integration/test_bulk_import_request.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
import requests
66

7-
from labelbox.schema.bulk_import_request import BulkImportRequest
7+
from labelbox.schema.bulk_import_request import BulkImportRequest, UuidError
88
from labelbox.schema.enums import BulkImportRequestState
99

1010
PREDICTIONS = [{
@@ -116,6 +116,20 @@ def test_validate_ndjson(tmp_path, project):
116116
project.upload_annotations(name="name", annotations=str(file_path))
117117

118118

119+
def test_validate_ndjson_uuid(tmp_path, project):
120+
file_name = f"repeat_uuid.ndjson"
121+
file_path = tmp_path / file_name
122+
repeat_uuid = PREDICTIONS.copy()
123+
repeat_uuid[0]['uuid'] = 'test_uuid'
124+
repeat_uuid[1]['uuid'] = 'test_uuid'
125+
126+
with file_path.open("w") as f:
127+
ndjson.dump(repeat_uuid, f)
128+
129+
with pytest.raises(UuidError):
130+
project.upload_annotations(name="name", annotations=str(file_path))
131+
132+
119133
@pytest.mark.slow
120134
def test_wait_till_done(project):
121135
name = str(uuid.uuid4())

0 commit comments

Comments
 (0)