Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 76 additions & 8 deletions labelbox/schema/data_row_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

from pydantic import BaseModel, conlist, constr
from labelbox.schema.identifiables import DataRowIdentifiers, UniqueIds
from labelbox.schema.identifiable import UniqueId, GlobalKey

from labelbox.schema.ontology import SchemaId
from labelbox.utils import _CamelCaseMixin, format_iso_datetime, format_iso_from_string
from labelbox.utils import _CamelCaseMixin, camel_case, format_iso_datetime, format_iso_from_string


class DataRowMetadataKind(Enum):
Expand Down Expand Up @@ -57,9 +58,12 @@ class DataRowMetadata(_CamelCaseMixin):


class DeleteDataRowMetadata(_CamelCaseMixin):
data_row_id: str
data_row_id: Union[str, UniqueId, GlobalKey]
fields: List[SchemaId]

class Config:
arbitrary_types_allowed = True


class DataRowMetadataBatchResponse(_CamelCaseMixin):
global_key: Optional[str]
Expand All @@ -86,9 +90,28 @@ class _UpsertBatchDataRowMetadata(_CamelCaseMixin):


class _DeleteBatchDataRowMetadata(_CamelCaseMixin):
data_row_id: str
data_row_identifier: Union[UniqueId, GlobalKey]
schema_ids: List[SchemaId]

class Config:
arbitrary_types_allowed = True
alias_generator = camel_case

def dict(self, *args, **kwargs):
res = super().dict(*args, **kwargs)
if 'data_row_identifier' in res.keys():
key = 'data_row_identifier'
id_type_key = 'id_type'
else:
key = 'dataRowIdentifier'
id_type_key = 'idType'
data_row_identifier = res.pop(key)
res[key] = {
"id": data_row_identifier.key,
id_type_key: data_row_identifier.id_type
}
return res


_BatchInputs = Union[List[_UpsertBatchDataRowMetadata],
List[_DeleteBatchDataRowMetadata]]
Expand Down Expand Up @@ -556,7 +579,17 @@ def bulk_delete(
""" Delete metadata from a datarow by specifiying the fields you want to remove

>>> delete = DeleteDataRowMetadata(
>>> data_row_id="datarow-id",
>>> data_row_id=UniqueId("datarow-id"),
>>> fields=[
>>> "schema-id-1",
>>> "schema-id-2"
>>> ...
>>> ]
>>> )
>>> mdo.batch_delete([metadata])

>>> delete = DeleteDataRowMetadata(
>>> data_row_id=GlobalKey("global-key"),
>>> fields=[
>>> "schema-id-1",
>>> "schema-id-2"
Expand All @@ -565,8 +598,22 @@ def bulk_delete(
>>> )
>>> mdo.batch_delete([metadata])

>>> delete = DeleteDataRowMetadata(
>>> data_row_id="global-key",
>>> fields=[
>>> "schema-id-1",
>>> "schema-id-2"
>>> ...
>>> ]
>>> )
>>> mdo.batch_delete([metadata])


Args:
deletes: Data row and schema ids to delete
For data row, we support UniqueId, str, and GlobalKey.
If you pass a str, we will assume it is a UniqueId
Do not pass a mix of data row ids and global keys in the same list

Returns:
list of unsuccessful deletions.
Expand All @@ -575,13 +622,34 @@ def bulk_delete(
"""

if not len(deletes):
raise ValueError("Empty list passed")
raise ValueError("The 'deletes' list cannot be empty.")

passed_strings = False
for i, delete in enumerate(deletes):
if isinstance(delete.data_row_id, str):
passed_strings = True
deletes[i] = DeleteDataRowMetadata(data_row_id=UniqueId(
delete.data_row_id),
fields=delete.fields)
elif isinstance(delete.data_row_id, UniqueId):
continue
elif isinstance(delete.data_row_id, GlobalKey):
continue
else:
raise ValueError(
f"Invalid data row identifier type '{type(delete.data_row_id)}' for '{delete.data_row_id}'"
)

if passed_strings:
warnings.warn(
"Using string for data row id will be deprecated. Please use "
"UniqueId instead.")

def _batch_delete(
deletes: List[_DeleteBatchDataRowMetadata]
) -> List[DataRowMetadataBatchResponse]:
query = """mutation DeleteDataRowMetadataBetaPyApi($deletes: [DataRowCustomMetadataBatchDeleteInput!]!) {
deleteDataRowCustomMetadata(data: $deletes) {
query = """mutation DeleteDataRowMetadataBetaPyApi($deletes: [DataRowIdentifierCustomMetadataBatchDeleteInput!]) {
deleteDataRowCustomMetadata(dataRowIdentifiers: $deletes) {
dataRowId
error
fields {
Expand Down Expand Up @@ -810,7 +878,7 @@ def _validate_delete(self, delete: DeleteDataRowMetadata):
deletes.add(schema.uid)

return _DeleteBatchDataRowMetadata(
data_row_id=delete.data_row_id,
data_row_identifier=delete.data_row_id,
schema_ids=list(delete.fields)).dict(by_alias=True)

def _validate_custom_schema_by_name(self,
Expand Down
Loading