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
- from pydantic import BaseModel , Field
6
8
from labelbox .schema .labeling_service_status import LabelingServiceStatus
7
9
from labelbox .utils import format_iso_datetime
8
- from pydantic .config import ConfigDict
9
10
10
11
11
12
class BaseSearchFilter (BaseModel ):
12
13
"""
13
14
Shared code for all search filters
14
15
"""
15
16
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
17
+ class Config :
18
+ use_enum_values = True
25
19
26
20
27
- class OperationType (Enum ):
21
+ class OperationTypeEnum (Enum ):
28
22
"""
29
23
Supported search entity types
30
24
Each type corresponds to a different filter class
@@ -40,6 +34,13 @@ class OperationType(Enum):
40
34
TaskRemainingCount = 'task_remaining_count'
41
35
42
36
37
+ OperationType = Annotated [OperationTypeEnum ,
38
+ PlainSerializer (lambda x : x .value , return_type = str )]
39
+
40
+ IsoDatetimeType = Annotated [datetime .datetime ,
41
+ PlainSerializer (format_iso_datetime )]
42
+
43
+
43
44
class IdOperator (Enum ):
44
45
"""
45
46
Supported operators for ids like org ids, workspace ids, etc
@@ -75,7 +76,8 @@ class OrganizationFilter(BaseSearchFilter):
75
76
"""
76
77
Filter for organization to which projects belong
77
78
"""
78
- operation : Literal [OperationType .Organization ] = OperationType .Organization
79
+ operation : OperationType = Field (default = OperationTypeEnum .Organization ,
80
+ serialization_alias = 'type' )
79
81
operator : IdOperator
80
82
values : List [str ]
81
83
@@ -84,9 +86,10 @@ class SharedWithOrganizationFilter(BaseSearchFilter):
84
86
"""
85
87
Find project shared with the organization (i.e. not having this organization as a tenantId)
86
88
"""
87
- operation : Literal [
88
- OperationType .
89
- SharedWithOrganization ] = OperationType .SharedWithOrganization
89
+
90
+ operation : OperationType = Field (
91
+ default = OperationTypeEnum .SharedWithOrganization ,
92
+ serialization_alias = 'type' )
90
93
operator : IdOperator
91
94
values : List [str ]
92
95
@@ -95,7 +98,8 @@ class WorkspaceFilter(BaseSearchFilter):
95
98
"""
96
99
Filter for workspace
97
100
"""
98
- operation : Literal [OperationType .Workspace ] = OperationType .Workspace
101
+ operation : OperationType = Field (default = OperationTypeEnum .Workspace ,
102
+ serialization_alias = 'type' )
99
103
operator : IdOperator
100
104
values : List [str ]
101
105
@@ -104,7 +108,8 @@ class TagFilter(BaseSearchFilter):
104
108
"""
105
109
Filter for project tags
106
110
"""
107
- operation : Literal [OperationType .Tag ] = OperationType .Tag
111
+ operation : OperationType = Field (default = OperationTypeEnum .Tag ,
112
+ serialization_alias = 'type' )
108
113
operator : IdOperator
109
114
values : List [str ]
110
115
@@ -114,7 +119,8 @@ class ProjectStageFilter(BaseSearchFilter):
114
119
Filter labelbox service / aka project stages
115
120
Stages are: requested, in_progress, completed etc. as described by LabelingServiceStatus
116
121
"""
117
- operation : Literal [OperationType .Stage ] = OperationType .Stage
122
+ operation : OperationType = Field (default = OperationTypeEnum .Stage ,
123
+ serialization_alias = 'type' )
118
124
operator : IdOperator
119
125
values : List [LabelingServiceStatus ]
120
126
@@ -132,7 +138,7 @@ class DateValue(BaseSearchFilter):
132
138
while the same string in EST will get converted to '2024-01-01T05:00:00Z'
133
139
"""
134
140
operator : RangeDateTimeOperatorWithSingleValue
135
- value : datetime . datetime
141
+ value : IsoDatetimeType
136
142
137
143
138
144
class IntegerValue (BaseSearchFilter ):
@@ -144,28 +150,28 @@ class WorkforceStageUpdatedFilter(BaseSearchFilter):
144
150
"""
145
151
Filter for workforce stage updated date
146
152
"""
147
- operation : Literal [
148
- OperationType .
149
- WorkforceStageUpdatedDate ] = OperationType . WorkforceStageUpdatedDate
153
+ operation : OperationType = Field (
154
+ default = OperationTypeEnum . WorkforceStageUpdatedDate ,
155
+ serialization_alias = 'type' )
150
156
value : DateValue
151
157
152
158
153
159
class WorkforceRequestedDateFilter (BaseSearchFilter ):
154
160
"""
155
161
Filter for workforce requested date
156
162
"""
157
- operation : Literal [
158
- OperationType .
159
- WorforceRequestedDate ] = OperationType . WorforceRequestedDate
163
+ operation : OperationType = Field (
164
+ default = OperationTypeEnum . WorforceRequestedDate ,
165
+ serialization_alias = 'type' )
160
166
value : DateValue
161
167
162
168
163
169
class DateRange (BaseSearchFilter ):
164
170
"""
165
171
Date range for a search filter
166
172
"""
167
- min : datetime . datetime
168
- max : datetime . datetime
173
+ min : IsoDatetimeType
174
+ max : IsoDatetimeType
169
175
170
176
171
177
class DateRangeValue (BaseSearchFilter ):
@@ -180,19 +186,19 @@ class WorkforceRequestedDateRangeFilter(BaseSearchFilter):
180
186
"""
181
187
Filter for workforce requested date range
182
188
"""
183
- operation : Literal [
184
- OperationType .
185
- WorforceRequestedDate ] = OperationType . WorforceRequestedDate
189
+ operation : OperationType = Field (
190
+ default = OperationTypeEnum . WorforceRequestedDate ,
191
+ serialization_alias = 'type' )
186
192
value : DateRangeValue
187
193
188
194
189
195
class WorkforceStageUpdatedRangeFilter (BaseSearchFilter ):
190
196
"""
191
197
Filter for workforce stage updated date range
192
198
"""
193
- operation : Literal [
194
- OperationType .
195
- WorkforceStageUpdatedDate ] = OperationType . WorkforceStageUpdatedDate
199
+ operation : OperationType = Field (
200
+ default = OperationTypeEnum . WorkforceStageUpdatedDate ,
201
+ serialization_alias = 'type' )
196
202
value : DateRangeValue
197
203
198
204
@@ -201,20 +207,19 @@ class TaskCompletedCountFilter(BaseSearchFilter):
201
207
Filter for completed tasks count
202
208
A task maps to a data row. Task completed should map to a data row in a labeling queue DONE
203
209
"""
204
- operation : Literal [
205
- OperationType .TaskCompletedCount ] = Field (default = OperationType .TaskCompletedCount , serialization_alias = 'type' )
210
+ operation : OperationType = Field (
211
+ default = OperationTypeEnum .TaskCompletedCount ,
212
+ serialization_alias = 'type' )
206
213
value : IntegerValue
207
214
208
215
209
-
210
-
211
-
212
216
class TaskRemainingCountFilter (BaseSearchFilter ):
213
217
"""
214
218
Filter for remaining tasks count. Reverse of TaskCompletedCountFilter
215
219
"""
216
- operation : Literal [
217
- OperationType .TaskRemainingCount ] = Field (OperationType .TaskRemainingCount , serialization_alias = 'type' )
220
+ operation : OperationType = Field (
221
+ default = OperationTypeEnum .TaskRemainingCount ,
222
+ serialization_alias = 'type' )
218
223
value : IntegerValue
219
224
220
225
@@ -242,5 +247,7 @@ def build_search_filter(filter: List[SearchFilter]):
242
247
"""
243
248
Converts a list of search filters to a graphql string
244
249
"""
245
- filters = [_dict_to_graphql_string (f .model_dump (by_alias = True )) for f in filter ]
250
+ filters = [
251
+ _dict_to_graphql_string (f .model_dump (by_alias = True )) for f in filter
252
+ ]
246
253
return "[" + ", " .join (filters ) + "]"
0 commit comments