1
1
import datetime
2
2
from enum import Enum
3
- from typing import List , Literal , Union
3
+ from typing import List , Union
4
+ from pydantic import PlainSerializer , BaseModel , Field
5
+
6
+ from typing_extensions import Annotated
4
7
5
8
from pydantic import BaseModel , Field , field_validator
6
9
from labelbox .schema .labeling_service_status import LabelingServiceStatus
7
10
from labelbox .utils import format_iso_datetime
8
- from pydantic .config import ConfigDict
9
11
10
12
11
13
class BaseSearchFilter (BaseModel ):
12
14
"""
13
15
Shared code for all search filters
14
16
"""
15
17
16
- model_config = ConfigDict (use_enum_values = True )
17
-
18
- def dict (self , * args , ** kwargs ):
19
- res = super ().dict (* args , ** kwargs )
20
- # go through all the keys and convert date to string
21
- for key in res :
22
- if isinstance (res [key ], datetime .datetime ):
23
- res [key ] = format_iso_datetime (res [key ])
24
- return res
18
+ class Config :
19
+ use_enum_values = True
25
20
26
21
27
- class OperationType (Enum ):
22
+ class OperationTypeEnum (Enum ):
28
23
"""
29
24
Supported search entity types
30
25
Each type corresponds to a different filter class
@@ -40,6 +35,13 @@ class OperationType(Enum):
40
35
TaskRemainingCount = 'task_remaining_count'
41
36
42
37
38
+ OperationType = Annotated [OperationTypeEnum ,
39
+ PlainSerializer (lambda x : x .value , return_type = str )]
40
+
41
+ IsoDatetimeType = Annotated [datetime .datetime ,
42
+ PlainSerializer (format_iso_datetime )]
43
+
44
+
43
45
class IdOperator (Enum ):
44
46
"""
45
47
Supported operators for ids like org ids, workspace ids, etc
@@ -75,7 +77,8 @@ class OrganizationFilter(BaseSearchFilter):
75
77
"""
76
78
Filter for organization to which projects belong
77
79
"""
78
- operation : Literal [OperationType .Organization ] = OperationType .Organization
80
+ operation : OperationType = Field (default = OperationTypeEnum .Organization ,
81
+ serialization_alias = 'type' )
79
82
operator : IdOperator
80
83
values : List [str ]
81
84
@@ -84,9 +87,10 @@ class SharedWithOrganizationFilter(BaseSearchFilter):
84
87
"""
85
88
Find project shared with the organization (i.e. not having this organization as a tenantId)
86
89
"""
87
- operation : Literal [
88
- OperationType .
89
- SharedWithOrganization ] = OperationType .SharedWithOrganization
90
+
91
+ operation : OperationType = Field (
92
+ default = OperationTypeEnum .SharedWithOrganization ,
93
+ serialization_alias = 'type' )
90
94
operator : IdOperator
91
95
values : List [str ]
92
96
@@ -95,7 +99,8 @@ class WorkspaceFilter(BaseSearchFilter):
95
99
"""
96
100
Filter for workspace
97
101
"""
98
- operation : Literal [OperationType .Workspace ] = OperationType .Workspace
102
+ operation : OperationType = Field (default = OperationTypeEnum .Workspace ,
103
+ serialization_alias = 'type' )
99
104
operator : IdOperator
100
105
values : List [str ]
101
106
@@ -105,7 +110,8 @@ class TagFilter(BaseSearchFilter):
105
110
Filter for project tags
106
111
values are tag ids
107
112
"""
108
- operation : Literal [OperationType .Tag ] = OperationType .Tag
113
+ operation : OperationType = Field (default = OperationTypeEnum .Tag ,
114
+ serialization_alias = 'type' )
109
115
operator : IdOperator
110
116
values : List [str ]
111
117
@@ -115,11 +121,12 @@ class ProjectStageFilter(BaseSearchFilter):
115
121
Filter labelbox service / aka project stages
116
122
Stages are: requested, in_progress, completed etc. as described by LabelingServiceStatus
117
123
"""
118
- operation : Literal [OperationType .Stage ] = OperationType .Stage
124
+ operation : OperationType = Field (default = OperationTypeEnum .Stage ,
125
+ serialization_alias = 'type' )
119
126
operator : IdOperator
120
127
values : List [LabelingServiceStatus ]
121
128
122
- @field_validator ('values' )
129
+ @field_validator ('values' , mode = 'before' )
123
130
def validate_values (cls , values ):
124
131
disallowed_values = [LabelingServiceStatus .Missing ]
125
132
for value in values :
@@ -143,7 +150,7 @@ class DateValue(BaseSearchFilter):
143
150
while the same string in EST will get converted to '2024-01-01T05:00:00Z'
144
151
"""
145
152
operator : RangeDateTimeOperatorWithSingleValue
146
- value : datetime . datetime
153
+ value : IsoDatetimeType
147
154
148
155
149
156
class IntegerValue (BaseSearchFilter ):
@@ -155,28 +162,28 @@ class WorkforceStageUpdatedFilter(BaseSearchFilter):
155
162
"""
156
163
Filter for workforce stage updated date
157
164
"""
158
- operation : Literal [
159
- OperationType .
160
- WorkforceStageUpdatedDate ] = OperationType . WorkforceStageUpdatedDate
165
+ operation : OperationType = Field (
166
+ default = OperationTypeEnum . WorkforceStageUpdatedDate ,
167
+ serialization_alias = 'type' )
161
168
value : DateValue
162
169
163
170
164
171
class WorkforceRequestedDateFilter (BaseSearchFilter ):
165
172
"""
166
173
Filter for workforce requested date
167
174
"""
168
- operation : Literal [
169
- OperationType .
170
- WorforceRequestedDate ] = OperationType . WorforceRequestedDate
175
+ operation : OperationType = Field (
176
+ default = OperationTypeEnum . WorforceRequestedDate ,
177
+ serialization_alias = 'type' )
171
178
value : DateValue
172
179
173
180
174
181
class DateRange (BaseSearchFilter ):
175
182
"""
176
183
Date range for a search filter
177
184
"""
178
- min : datetime . datetime
179
- max : datetime . datetime
185
+ min : IsoDatetimeType
186
+ max : IsoDatetimeType
180
187
181
188
182
189
class DateRangeValue (BaseSearchFilter ):
@@ -191,19 +198,19 @@ class WorkforceRequestedDateRangeFilter(BaseSearchFilter):
191
198
"""
192
199
Filter for workforce requested date range
193
200
"""
194
- operation : Literal [
195
- OperationType .
196
- WorforceRequestedDate ] = OperationType . WorforceRequestedDate
201
+ operation : OperationType = Field (
202
+ default = OperationTypeEnum . WorforceRequestedDate ,
203
+ serialization_alias = 'type' )
197
204
value : DateRangeValue
198
205
199
206
200
207
class WorkforceStageUpdatedRangeFilter (BaseSearchFilter ):
201
208
"""
202
209
Filter for workforce stage updated date range
203
210
"""
204
- operation : Literal [
205
- OperationType .
206
- WorkforceStageUpdatedDate ] = OperationType . WorkforceStageUpdatedDate
211
+ operation : OperationType = Field (
212
+ default = OperationTypeEnum . WorkforceStageUpdatedDate ,
213
+ serialization_alias = 'type' )
207
214
value : DateRangeValue
208
215
209
216
@@ -212,20 +219,19 @@ class TaskCompletedCountFilter(BaseSearchFilter):
212
219
Filter for completed tasks count
213
220
A task maps to a data row. Task completed should map to a data row in a labeling queue DONE
214
221
"""
215
- operation : Literal [
216
- OperationType .TaskCompletedCount ] = Field (default = OperationType .TaskCompletedCount , serialization_alias = 'type' )
222
+ operation : OperationType = Field (
223
+ default = OperationTypeEnum .TaskCompletedCount ,
224
+ serialization_alias = 'type' )
217
225
value : IntegerValue
218
226
219
227
220
-
221
-
222
-
223
228
class TaskRemainingCountFilter (BaseSearchFilter ):
224
229
"""
225
230
Filter for remaining tasks count. Reverse of TaskCompletedCountFilter
226
231
"""
227
- operation : Literal [
228
- OperationType .TaskRemainingCount ] = Field (OperationType .TaskRemainingCount , serialization_alias = 'type' )
232
+ operation : OperationType = Field (
233
+ default = OperationTypeEnum .TaskRemainingCount ,
234
+ serialization_alias = 'type' )
229
235
value : IntegerValue
230
236
231
237
@@ -253,5 +259,7 @@ def build_search_filter(filter: List[SearchFilter]):
253
259
"""
254
260
Converts a list of search filters to a graphql string
255
261
"""
256
- filters = [_dict_to_graphql_string (f .model_dump (by_alias = True )) for f in filter ]
262
+ filters = [
263
+ _dict_to_graphql_string (f .model_dump (by_alias = True )) for f in filter
264
+ ]
257
265
return "[" + ", " .join (filters ) + "]"
0 commit comments