Skip to content

Commit c050da2

Browse files
author
Val Brodsky
committed
Update to support identifiable as type in graphql and other identifiable improvements
1 parent f3d1dbd commit c050da2

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

labelbox/schema/identifiable.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
from abc import ABC
1+
from abc import ABC, abstractmethod
22
from enum import Enum
33
from typing import List, Union
4-
from typing_extensions import TypeAlias
54

65

76
class IdType(str, Enum):
8-
DataRowId = "DATA_ROW_ID"
7+
DataRowId = "UID"
98
GlobalKey = "GLOBAL_KEY"
109

1110

1211
class Identifiable(ABC):
1312

14-
def __init__(self, keys: Union[str, List[str]]):
13+
def __init__(self, keys: Union[str, List[str]], id_type: IdType):
1514
self._keys = keys
1615
if isinstance(keys, str):
17-
self.keys = [keys]
18-
self._id_type = IdType.DataRowId
16+
self._keys = [keys]
17+
self._id_type = id_type
1918

2019
@property
2120
def keys(self):
@@ -25,11 +24,12 @@ def keys(self):
2524
def keys(self, keys):
2625
self._keys = keys
2726
if isinstance(keys, str):
28-
self.keys = [keys]
27+
self._keys = [keys]
2928

30-
@property
31-
def id_type(self):
32-
return self._id_type
29+
@classmethod
30+
@abstractmethod
31+
def strings_to_identifiable(cls, keys: Union[str, List[str]]):
32+
pass
3333

3434
def __eq__(self, other):
3535
return other.keys == self.keys
@@ -43,21 +43,23 @@ def __str__(self):
4343

4444
class UniqueIds(Identifiable):
4545

46+
@classmethod
47+
def strings_to_identifiable(cls, keys: Union[str, List[str]]):
48+
return cls(keys)
49+
4650
def __init__(self, keys: Union[str, List[str]]):
47-
super().__init__(keys)
48-
self._id_type = IdType.DataRowId
51+
super().__init__(keys, IdType.DataRowId)
4952

5053

5154
class GlobalKeys(Identifiable):
5255

53-
def __init__(self, keys: Union[str, List[str]]):
54-
super().__init__(keys)
55-
self._id_type = IdType.GlobalKey
56-
56+
@classmethod
57+
def strings_to_identifiable(cls, keys: Union[str, List[str]]):
58+
return cls(keys)
5759

58-
DefaultIdentifiable: TypeAlias = UniqueIds
59-
DataRowIdentifiers: TypeAlias = Union[UniqueIds, GlobalKeys]
60+
def __init__(self, keys: Union[str, List[str]]):
61+
super().__init__(keys, IdType.GlobalKey)
6062

6163

62-
def strings_to_identifiable(keys: Union[str, List[str]]) -> DefaultIdentifiable:
63-
return DefaultIdentifiable(keys)
64+
DefaultIdentifiable = UniqueIds
65+
DataRowIdentifiers = Union[UniqueIds, GlobalKeys]

labelbox/schema/project.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from labelbox.schema.export_filters import ProjectExportFilters, validate_datetime, build_filters
2929
from labelbox.schema.export_params import ProjectExportParams
3030
from labelbox.schema.export_task import ExportTask
31-
from labelbox.schema.identifiable import DataRowIdentifiers, strings_to_identifiable
31+
from labelbox.schema.identifiable import DataRowIdentifiers, UniqueIds
3232
from labelbox.schema.media_type import MediaType
3333
from labelbox.schema.queue_mode import QueueMode
3434
from labelbox.schema.resource_tag import ResourceTag
@@ -1403,18 +1403,17 @@ def move_data_rows_to_task_queue(self, data_row_ids, task_queue_id: str):
14031403
14041404
"""
14051405
if isinstance(data_row_ids, list):
1406-
data_row_ids = strings_to_identifiable(data_row_ids)
1406+
data_row_ids = UniqueIds.strings_to_identifiable(data_row_ids)
14071407

14081408
method = "createBulkAddRowsToQueueTask"
14091409
query_str = """mutation AddDataRowsToTaskQueueAsyncPyApi(
14101410
$projectId: ID!
14111411
$queueId: ID
1412-
$dataRowIds: [ID!]!
1413-
$idType: IdType!
1412+
$dataRowIdentifiers: AddRowsToTaskQueueViaDataRowIdentifiersInput!
14141413
) {
14151414
project(where: { id: $projectId }) {
14161415
%s(
1417-
data: { queueId: $queueId, dataRowIds: $dataRowIds, idType: $idType }
1416+
data: { queueId: $queueId, dataRowIdentifiers: $dataRowIdentifiers }
14181417
) {
14191418
taskId
14201419
}
@@ -1426,8 +1425,10 @@ def move_data_rows_to_task_queue(self, data_row_ids, task_queue_id: str):
14261425
query_str, {
14271426
"projectId": self.uid,
14281427
"queueId": task_queue_id,
1429-
"dataRowIds": data_row_ids.keys,
1430-
"idType": data_row_ids.id_type,
1428+
"dataRowIdentifiers": {
1429+
"ids": data_row_ids.keys,
1430+
"idType": data_row_ids._id_type,
1431+
},
14311432
},
14321433
timeout=180.0,
14331434
experimental=True)["project"][method]["taskId"]

tests/unit/test_unit_identifiable.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
1-
from labelbox.schema.identifiable import GlobalKeys, UniqueIds, strings_to_identifiable
1+
from labelbox.schema.identifiable import GlobalKeys, UniqueIds
22

33

44
def test_unique_ids():
55
ids = ["a", "b", "c"]
66
identifiable = UniqueIds(ids)
77
assert identifiable.keys == ids
8-
assert identifiable.id_type == "DATA_ROW_ID"
8+
assert identifiable._id_type == "UID"
99

1010

1111
def test_global_keys():
1212
ids = ["a", "b", "c"]
1313
identifiable = GlobalKeys(ids)
1414
assert identifiable.keys == ids
15-
assert identifiable.id_type == "GLOBAL_KEY"
15+
assert identifiable._id_type == "GLOBAL_KEY"
1616

1717

18-
def test_strings_to_identifiable():
18+
def test_strings_to_identifiable_unique_id():
1919
ids = ["a", "b", "c"]
20-
identifiable = strings_to_identifiable(ids)
20+
identifiable = UniqueIds.strings_to_identifiable(ids)
21+
assert type(identifiable) == UniqueIds
2122
assert identifiable.keys == ids
22-
assert identifiable.id_type == "DATA_ROW_ID"
23+
assert identifiable._id_type == "UID"
2324

2425
ids = "a"
25-
identifiable = strings_to_identifiable(ids)
26+
identifiable = UniqueIds.strings_to_identifiable(ids)
27+
assert type(identifiable) == UniqueIds
2628
assert identifiable.keys == [ids]
27-
assert identifiable.id_type == "DATA_ROW_ID"
29+
assert identifiable._id_type == "UID"
30+
31+
32+
def test_strings_to_identifiable_global_key():
33+
ids = ["a", "b", "c"]
34+
identifiable = GlobalKeys.strings_to_identifiable(ids)
35+
assert type(identifiable) == GlobalKeys
36+
assert identifiable.keys == ids
37+
assert identifiable._id_type == "GLOBAL_KEY"
38+
39+
ids = "a"
40+
identifiable = GlobalKeys.strings_to_identifiable(ids)
41+
assert type(identifiable) == GlobalKeys
42+
assert identifiable.keys == [ids]
43+
assert identifiable._id_type == "GLOBAL_KEY"
2844

2945

3046
def test__str__():

0 commit comments

Comments
 (0)