From b5b60f0db603228122efa0419ef2ec7157814d27 Mon Sep 17 00:00:00 2001 From: Gabefire <33893811+Gabefire@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:33:45 -0600 Subject: [PATCH 1/2] Let Batch.remove_queued_data_rows remove specific data rows remove_queued_data_rows() takes an optional data_row_ids list. With it, only those unlabeled rows leave the batch and the project; they stay in Catalog and other projects. If any of them is labeled or not in the batch, nothing is removed. Without it, every unlabeled row in the batch is removed, as before. Co-Authored-By: Claude Opus 5.5 --- libs/labelbox/src/labelbox/schema/batch.py | 47 +++++++++++-- libs/labelbox/tests/integration/test_batch.py | 21 ++++++ ...test_unit_batch_remove_queued_data_rows.py | 69 +++++++++++++++++++ 3 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 libs/labelbox/tests/unit/test_unit_batch_remove_queued_data_rows.py diff --git a/libs/labelbox/src/labelbox/schema/batch.py b/libs/labelbox/src/labelbox/schema/batch.py index 99e4c4908..9cbe0930c 100644 --- a/libs/labelbox/src/labelbox/schema/batch.py +++ b/libs/labelbox/src/labelbox/schema/batch.py @@ -1,5 +1,5 @@ import logging -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, List, Optional from lbox.exceptions import ResourceNotFoundError @@ -62,12 +62,51 @@ def project(self) -> "Project": # type: ignore return Entity.Project(self.client, response["project"]) - def remove_queued_data_rows(self) -> None: - """Removes remaining queued data rows from the batch and labeling queue. + def remove_queued_data_rows( + self, data_row_ids: Optional[List[str]] = None + ) -> None: + """Removes unlabeled data rows from the batch and the project's labeling queue. + + The data rows stay in Catalog and in any other projects. Labeled data + rows are never removed; delete their labels first if you want them out. Args: - batch (Batch): Batch to remove queued data rows from + data_row_ids: Ids of the data rows to remove, at most 10,000 per + call. If omitted, every unlabeled data row in the batch is + removed. If given, only these rows are removed, and if any of + them is labeled or not in this batch, nothing is removed. + + Raises: + ValueError: if `data_row_ids` is empty or not a list of ids. + OperationNotAllowedException: if a row in `data_row_ids` is labeled + or not in this batch (the message names them), or the batch + has no unlabeled data rows left. + + >>> batch.remove_queued_data_rows(data_row_ids=[data_row.uid]) """ + if data_row_ids is not None: + if isinstance(data_row_ids, str) or not all( + isinstance(data_row_id, str) for data_row_id in data_row_ids + ): + raise ValueError("data_row_ids must be a list of data row ids") + data_row_ids = list(data_row_ids) + if not data_row_ids: + raise ValueError( + "data_row_ids must not be empty. Omit it to remove every " + "unlabeled data row in the batch." + ) + self.client.execute( + """mutation RemoveQueuedDataRowsFromBatchPyApi($projectId: ID!, $batchId: ID!, $dataRowIds: [ID!]) { + project(where: {id: $projectId}) { removeQueuedDataRowsFromBatch(batchId: $batchId, dataRowIds: $dataRowIds) { id } } + }""", + { + "projectId": self.project_id, + "batchId": self.uid, + "dataRowIds": data_row_ids, + }, + experimental=True, + ) + return project_id_param = "projectId" batch_id_param = "batchId" diff --git a/libs/labelbox/tests/integration/test_batch.py b/libs/labelbox/tests/integration/test_batch.py index b58a4609b..f8ba25252 100644 --- a/libs/labelbox/tests/integration/test_batch.py +++ b/libs/labelbox/tests/integration/test_batch.py @@ -6,6 +6,7 @@ from lbox.exceptions import ( LabelboxError, MalformedQueryException, + OperationNotAllowedException, ProcessingWaitTimeout, ResourceConflict, ) @@ -187,6 +188,26 @@ def test_archive_batch(project: Project, small_dataset: Dataset): assert overview.to_label == 0 +def test_remove_specific_queued_data_rows( + project: Project, small_dataset: Dataset +): + data_rows = get_data_row_ids(small_dataset) + assert len(data_rows) >= 2 + removed, kept = data_rows[0], data_rows[1:] + + batch = project.create_batch("batch to remove one row from", data_rows) + batch.remove_queued_data_rows(data_row_ids=[removed]) + + assert project.get_overview().to_label == len(kept) + # removed from the project only, not from Catalog + assert project.client.get_data_row(removed).uid == removed + + # the row is no longer in the batch, so nothing is removed + with pytest.raises(OperationNotAllowedException, match=removed): + batch.remove_queued_data_rows(data_row_ids=[removed, kept[0]]) + assert project.get_overview().to_label == len(kept) + + def test_delete(project: Project, small_dataset: Dataset): export_task = small_dataset.export() export_task.wait_till_done() diff --git a/libs/labelbox/tests/unit/test_unit_batch_remove_queued_data_rows.py b/libs/labelbox/tests/unit/test_unit_batch_remove_queued_data_rows.py new file mode 100644 index 000000000..bdd7bc99e --- /dev/null +++ b/libs/labelbox/tests/unit/test_unit_batch_remove_queued_data_rows.py @@ -0,0 +1,69 @@ +from unittest.mock import MagicMock + +import pytest + +from labelbox.schema.batch import Batch + + +@pytest.fixture +def mock_client(): + return MagicMock() + + +@pytest.fixture +def batch(mock_client): + return Batch( + mock_client, + "test_project_id", + { + "id": "test_batch_id", + "name": "test", + "createdAt": "2021-06-01T00:00:00.000Z", + "updatedAt": "2021-06-01T00:00:00.000Z", + "size": 3, + "consensusSettingsJson": None, + }, + ) + + +def test_without_ids_removes_every_queued_row(batch, mock_client): + batch.remove_queued_data_rows() + + query, params = mock_client.execute.call_args.args + assert "dataRowIds" not in query + assert params == { + "projectId": "test_project_id", + "batchId": "test_batch_id", + } + assert mock_client.execute.call_args.kwargs == {"experimental": True} + + +def test_with_ids_sends_only_those_ids(batch, mock_client): + batch.remove_queued_data_rows(data_row_ids=("dr_1", "dr_2")) + + query, params = mock_client.execute.call_args.args + assert ( + "removeQueuedDataRowsFromBatch(batchId: $batchId, dataRowIds: $dataRowIds)" + in query + ) + assert params == { + "projectId": "test_project_id", + "batchId": "test_batch_id", + "dataRowIds": ["dr_1", "dr_2"], + } + assert mock_client.execute.call_args.kwargs == {"experimental": True} + + +def test_empty_ids_are_rejected_before_any_request(batch, mock_client): + with pytest.raises(ValueError, match="must not be empty"): + batch.remove_queued_data_rows(data_row_ids=[]) + + mock_client.execute.assert_not_called() + + +@pytest.mark.parametrize("bad_ids", ["dr_1", ["dr_1", 2]]) +def test_non_list_of_ids_is_rejected(batch, mock_client, bad_ids): + with pytest.raises(ValueError, match="list of data row ids"): + batch.remove_queued_data_rows(data_row_ids=bad_ids) + + mock_client.execute.assert_not_called() From bacb55953d4f434b892ebcbc3c0ab24f60ce4f5f Mon Sep 17 00:00:00 2001 From: Gabefire <33893811+Gabefire@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:39:31 -0600 Subject: [PATCH 2/2] Copy data_row_ids before validating so generators aren't consumed Co-Authored-By: Claude Opus 5.5 --- libs/labelbox/src/labelbox/schema/batch.py | 7 +++++-- .../unit/test_unit_batch_remove_queued_data_rows.py | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/libs/labelbox/src/labelbox/schema/batch.py b/libs/labelbox/src/labelbox/schema/batch.py index 9cbe0930c..796742768 100644 --- a/libs/labelbox/src/labelbox/schema/batch.py +++ b/libs/labelbox/src/labelbox/schema/batch.py @@ -85,11 +85,14 @@ def remove_queued_data_rows( >>> batch.remove_queued_data_rows(data_row_ids=[data_row.uid]) """ if data_row_ids is not None: - if isinstance(data_row_ids, str) or not all( + if isinstance(data_row_ids, str): + raise ValueError("data_row_ids must be a list of data row ids") + # Copy first so a one-shot iterable isn't consumed by the check. + data_row_ids = list(data_row_ids) + if not all( isinstance(data_row_id, str) for data_row_id in data_row_ids ): raise ValueError("data_row_ids must be a list of data row ids") - data_row_ids = list(data_row_ids) if not data_row_ids: raise ValueError( "data_row_ids must not be empty. Omit it to remove every " diff --git a/libs/labelbox/tests/unit/test_unit_batch_remove_queued_data_rows.py b/libs/labelbox/tests/unit/test_unit_batch_remove_queued_data_rows.py index bdd7bc99e..f1637837a 100644 --- a/libs/labelbox/tests/unit/test_unit_batch_remove_queued_data_rows.py +++ b/libs/labelbox/tests/unit/test_unit_batch_remove_queued_data_rows.py @@ -54,6 +54,15 @@ def test_with_ids_sends_only_those_ids(batch, mock_client): assert mock_client.execute.call_args.kwargs == {"experimental": True} +def test_one_shot_iterable_of_ids_is_sent_in_full(batch, mock_client): + batch.remove_queued_data_rows( + data_row_ids=(data_row_id for data_row_id in ["dr_1", "dr_2"]) + ) + + _, params = mock_client.execute.call_args.args + assert params["dataRowIds"] == ["dr_1", "dr_2"] + + def test_empty_ids_are_rejected_before_any_request(batch, mock_client): with pytest.raises(ValueError, match="must not be empty"): batch.remove_queued_data_rows(data_row_ids=[])