From 2fa0d7c2ebaead7244ddc1ccd87e5050289a0d17 Mon Sep 17 00:00:00 2001 From: Attila Papai Date: Fri, 15 Mar 2024 19:04:41 +0100 Subject: [PATCH 1/4] add update method to attachments --- labelbox/schema/asset_attachment.py | 26 ++++++++++++++++++++++++-- tests/integration/test_data_rows.py | 20 +++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/labelbox/schema/asset_attachment.py b/labelbox/schema/asset_attachment.py index 12a658ab3..aab7e4de8 100644 --- a/labelbox/schema/asset_attachment.py +++ b/labelbox/schema/asset_attachment.py @@ -27,7 +27,6 @@ def __missing__(cls, value: object): VIDEO = "VIDEO" IMAGE = "IMAGE" - # TEXT = "TEXT" # Deprecated IMAGE_OVERLAY = "IMAGE_OVERLAY" HTML = "HTML" RAW_TEXT = "RAW_TEXT" @@ -40,6 +39,7 @@ def __missing__(cls, value: object): attachment_type = Field.String("attachment_type", "type") attachment_value = Field.String("attachment_value", "value") + attachment_name = Field.String("attachment_name", "name") @classmethod def validate_attachment_json(cls, attachment_json: Dict[str, str]) -> None: @@ -55,7 +55,7 @@ def validate_attachment_type(cls, attachment_type: str) -> None: valid_types = set(cls.AttachmentType.__members__) if attachment_type not in valid_types: raise ValueError( - f"meta_type must be one of {valid_types}. Found {attachment_type}" + f"attachment_type must be one of {valid_types}. Found {attachment_type}" ) def delete(self) -> None: @@ -65,3 +65,25 @@ def delete(self) -> None: id} }""" self.client.execute(query_str, {"attachment_id": self.uid}) + + def update(self, name: str = None, type: str = None, value: str = None): + """Updates an attachment on the data row.""" + self.validate_attachment_type(type) + + query_str = """mutation updateDataRowAttachmentPyApi($attachment_id: ID!, $name: String, $type: AttachmentType, $value: String) { + updateDataRowAttachment( + where: {id: $attachment_id}, + data: {name: $name, type: $type, value: $value} + ) { id name type value } + }""" + res = (self.client.execute( + query_str, { + "attachment_id": self.uid, + "name": name, + "type": type, + "value": value + }))['updateDataRowAttachment'] + + self.attachment_name = res['name'] + self.attachment_value = res['value'] + self.attachment_type = res['type'] diff --git a/tests/integration/test_data_rows.py b/tests/integration/test_data_rows.py index deea19e6b..b9cc4d118 100644 --- a/tests/integration/test_data_rows.py +++ b/tests/integration/test_data_rows.py @@ -7,7 +7,7 @@ import pytest import requests -from labelbox import DataRow +from labelbox import DataRow, AssetAttachment from labelbox.exceptions import MalformedQueryException from labelbox.schema.task import Task from labelbox.schema.data_row_metadata import DataRowMetadataField, DataRowMetadataKind @@ -773,6 +773,24 @@ def test_delete_data_row_attachment(data_row, image_url): assert len(list(data_row.attachments())) == 0 +def test_update_data_row_attachment(data_row, image_url): + attachment: AssetAttachment = data_row.create_attachment( + "RAW_TEXT", "value", "name") + assert attachment is not None + attachment.update(name="updated name", type="IMAGE", value=image_url) + assert attachment.attachment_name == "updated name" + assert attachment.attachment_type == "IMAGE" + assert attachment.attachment_value == image_url + + +def test_update_data_row_attachment_invalid_type(data_row): + attachment: AssetAttachment = data_row.create_attachment( + "RAW_TEXT", "value", "name") + assert attachment is not None + with pytest.raises(ValueError): + attachment.update(name="updated name", type="INVALID", value="value") + + def test_create_data_rows_result(client, dataset, image_url): task = dataset.create_data_rows([ { From 18a6f60ebbf6947ad1d858ed841dc10e2f305109 Mon Sep 17 00:00:00 2001 From: Attila Papai Date: Fri, 15 Mar 2024 19:05:54 +0100 Subject: [PATCH 2/4] add comment of new attribute --- labelbox/schema/asset_attachment.py | 1 + 1 file changed, 1 insertion(+) diff --git a/labelbox/schema/asset_attachment.py b/labelbox/schema/asset_attachment.py index aab7e4de8..af2c05d33 100644 --- a/labelbox/schema/asset_attachment.py +++ b/labelbox/schema/asset_attachment.py @@ -12,6 +12,7 @@ class AssetAttachment(DbObject): Attributes: attachment_type (str): IMAGE, VIDEO, IMAGE_OVERLAY, HTML, RAW_TEXT, TEXT_URL, or PDF_URL. TEXT attachment type is deprecated. attachment_value (str): URL to an external file or a string of text + attachment_name (str): The name of the attachment """ class AttachmentType(Enum): From b67852433bf9ec8540cc9f8a24d83592c11e999e Mon Sep 17 00:00:00 2001 From: Attila Papai Date: Fri, 15 Mar 2024 19:21:03 +0100 Subject: [PATCH 3/4] fix mypy --- labelbox/schema/asset_attachment.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/labelbox/schema/asset_attachment.py b/labelbox/schema/asset_attachment.py index af2c05d33..b72b44743 100644 --- a/labelbox/schema/asset_attachment.py +++ b/labelbox/schema/asset_attachment.py @@ -1,6 +1,6 @@ import warnings from enum import Enum -from typing import Dict +from typing import Dict, Optional from labelbox.orm.db_object import DbObject from labelbox.orm.model import Field @@ -67,7 +67,10 @@ def delete(self) -> None: }""" self.client.execute(query_str, {"attachment_id": self.uid}) - def update(self, name: str = None, type: str = None, value: str = None): + def update(self, + name: Optional[str] = None, + type: Optional[str] = None, + value: Optional[str] = None): """Updates an attachment on the data row.""" self.validate_attachment_type(type) From ea6add65f51d4230f2a1c6eb53705172784b06ca Mon Sep 17 00:00:00 2001 From: Attila Papai Date: Sat, 16 Mar 2024 09:12:23 +0100 Subject: [PATCH 4/4] fix mypy --- labelbox/schema/asset_attachment.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/labelbox/schema/asset_attachment.py b/labelbox/schema/asset_attachment.py index b72b44743..219ebbab8 100644 --- a/labelbox/schema/asset_attachment.py +++ b/labelbox/schema/asset_attachment.py @@ -72,7 +72,8 @@ def update(self, type: Optional[str] = None, value: Optional[str] = None): """Updates an attachment on the data row.""" - self.validate_attachment_type(type) + if type: + self.validate_attachment_type(type) query_str = """mutation updateDataRowAttachmentPyApi($attachment_id: ID!, $name: String, $type: AttachmentType, $value: String) { updateDataRowAttachment(