Skip to content

Commit f811af2

Browse files
author
Val Brodsky
committed
Convert labeling dashboard models to pydantic2
1 parent 83fe543 commit f811af2

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

libs/labelbox/src/labelbox/schema/search_filters.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@
22
from enum import Enum
33
from typing import List, Literal, Union
44

5-
from pydantic import BaseModel, field_validator
5+
from pydantic import BaseModel, Field, field_validator
66
from labelbox.schema.labeling_service_status import LabelingServiceStatus
77
from labelbox.utils import format_iso_datetime
8+
from pydantic.config import ConfigDict
89

910

1011
class BaseSearchFilter(BaseModel):
1112
"""
1213
Shared code for all search filters
1314
"""
1415

15-
class Config:
16-
use_enum_values = True
16+
model_config = ConfigDict(use_enum_values=True)
1717

1818
def dict(self, *args, **kwargs):
1919
res = super().dict(*args, **kwargs)
20-
if 'operation' in res:
21-
res['type'] = res.pop('operation')
22-
2320
# go through all the keys and convert date to string
2421
for key in res:
2522
if isinstance(res[key], datetime.datetime):
@@ -216,16 +213,19 @@ class TaskCompletedCountFilter(BaseSearchFilter):
216213
A task maps to a data row. Task completed should map to a data row in a labeling queue DONE
217214
"""
218215
operation: Literal[
219-
OperationType.TaskCompletedCount] = OperationType.TaskCompletedCount
216+
OperationType.TaskCompletedCount] = Field(default=OperationType.TaskCompletedCount, serialization_alias='type')
220217
value: IntegerValue
221218

222219

220+
221+
222+
223223
class TaskRemainingCountFilter(BaseSearchFilter):
224224
"""
225225
Filter for remaining tasks count. Reverse of TaskCompletedCountFilter
226226
"""
227227
operation: Literal[
228-
OperationType.TaskRemainingCount] = OperationType.TaskRemainingCount
228+
OperationType.TaskRemainingCount] = Field(OperationType.TaskRemainingCount, serialization_alias='type')
229229
value: IntegerValue
230230

231231

@@ -253,5 +253,5 @@ def build_search_filter(filter: List[SearchFilter]):
253253
"""
254254
Converts a list of search filters to a graphql string
255255
"""
256-
filters = [_dict_to_graphql_string(f.dict()) for f in filter]
256+
filters = [_dict_to_graphql_string(f.model_dump(by_alias=True)) for f in filter]
257257
return "[" + ", ".join(filters) + "]"

libs/labelbox/tests/unit/test_unit_search_filters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ def test_date_range_filters():
7575

7676
def test_task_count_filters():
7777
filters = [
78-
TaskCompletedCountFilter(value=IntegerValue(
79-
operator=RangeOperatorWithSingleValue.GreaterThanOrEqual, value=1)),
80-
TaskRemainingCountFilter(value=IntegerValue(
81-
operator=RangeOperatorWithSingleValue.LessThanOrEqual, value=10)),
78+
TaskCompletedCountFilter(value=IntegerValue(operator=RangeOperatorWithSingleValue.GreaterThanOrEqual, value=1)),
79+
# TaskRemainingCountFilter(value=IntegerValue(
80+
# operator=RangeOperatorWithSingleValue.LessThanOrEqual, value=10)),
8281
]
8382

83+
# expected = '[{value: {operator: "GREATER_THAN_OR_EQUAL", value: 1}, type: "task_completed_count"}, {value: {operator: "LESS_THAN_OR_EQUAL", value: 10}, type: "task_remaining_count"}]'
8484
expected = '[{value: {operator: "GREATER_THAN_OR_EQUAL", value: 1}, type: "task_completed_count"}, {value: {operator: "LESS_THAN_OR_EQUAL", value: 10}, type: "task_remaining_count"}]'
8585
assert build_search_filter(filters) == expected

0 commit comments

Comments
 (0)