Skip to content

Commit 27c77b9

Browse files
authored
Add DisconncetedTask (#1767)
1 parent e20a774 commit 27c77b9

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

libs/labelbox/src/labelbox/client.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from labelbox.schema.send_to_annotate_params import SendToAnnotateFromCatalogParams, build_destination_task_queue_input, \
5252
build_predictions_input, build_annotations_input
5353
from labelbox.schema.slice import CatalogSlice, ModelSlice
54-
from labelbox.schema.task import Task
54+
from labelbox.schema.task import Task, DataUpsertTask
5555
from labelbox.schema.user import User
5656
from labelbox.schema.label_score import LabelScore
5757
from labelbox.schema.ontology_kind import (OntologyKind, EditorTaskTypeMapper,
@@ -2424,3 +2424,51 @@ def get_labeling_service_dashboards(
24242424
return LabelingServiceDashboard.get_all(self,
24252425
after,
24262426
search_query=search_query)
2427+
2428+
def get_task_by_id(self, task_id: str) -> Union[Task, DataUpsertTask]:
2429+
"""
2430+
Fetches a task by ID.
2431+
2432+
Args:
2433+
task_id (str): The ID of the task.
2434+
2435+
Returns:
2436+
Task or DataUpsertTask
2437+
2438+
Throws:
2439+
ResourceNotFoundError: If the task does not exist.
2440+
2441+
NOTE: Export task is not supported yet
2442+
"""
2443+
user = self.get_user()
2444+
query = """
2445+
query GetUserCreatedTasksPyApi($userId: ID!, $taskId: ID!) {
2446+
user(where: {id: $userId}) {
2447+
createdTasks(where: {id: $taskId} skip: 0 first: 1) {
2448+
completionPercentage
2449+
createdAt
2450+
errors
2451+
metadata
2452+
name
2453+
result
2454+
status
2455+
type
2456+
id
2457+
updatedAt
2458+
}
2459+
}
2460+
}
2461+
"""
2462+
result = self.execute(query, {"userId": user.uid, "taskId": task_id})
2463+
data = result.get("user", {}).get("createdTasks", [])
2464+
if not data:
2465+
raise labelbox.exceptions.ResourceNotFoundError(
2466+
message=f"The task {task_id} does not exist.")
2467+
task_data = data[0]
2468+
if task_data["type"].lower() == 'adv-upsert-data-rows':
2469+
task = DataUpsertTask(self, task_data)
2470+
else:
2471+
task = Task(self, task_data)
2472+
2473+
task._user = user
2474+
return task

libs/labelbox/src/labelbox/schema/task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import requests
44
import time
5-
from typing import TYPE_CHECKING, Callable, Optional, Dict, Any, List, Union, Final
5+
from typing import TYPE_CHECKING, Callable, Optional, Dict, Any, List, Union
66
from labelbox import parser
77

88
from labelbox.exceptions import ResourceNotFoundError

libs/labelbox/tests/integration/test_task.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from labelbox.schema.disconnected_task import DisconnectedTask
23
import pytest
34
import collections.abc
45
from labelbox import DataRow
@@ -31,6 +32,13 @@ def test_task_errors(dataset, image_url, snapshot):
3132
0]['message']
3233
assert len(task.failed_data_rows[0]['failedDataRows'][0]['metadata']) == 2
3334

35+
dt = client.get_task_by_id(task.uid)
36+
assert dt.status == "COMPLETE"
37+
assert len(dt.errors) == 1
38+
assert dt.errors[0]['message'].startswith(
39+
"A schemaId can only be specified once per DataRow")
40+
assert dt.result is None
41+
3442

3543
def test_task_success_json(dataset, image_url, snapshot):
3644
client = dataset.client
@@ -57,3 +65,8 @@ def test_task_success_json(dataset, image_url, snapshot):
5765
snapshot.assert_match(json.dumps(task_result),
5866
'test_task.test_task_success_json.json')
5967
assert len(task.result)
68+
69+
dt = client.get_task_by_id(task.uid)
70+
assert dt.status == "COMPLETE"
71+
assert len(dt.result) == 1
72+
assert dt.errors is None

0 commit comments

Comments
 (0)