2222 ConstraintInitializer: Function signature for constraint factory.
2323"""
2424
25+ from __future__ import annotations
26+
2527import time
26- from typing import Any , Optional , Protocol , Union , runtime_checkable
28+ from typing import Any , Protocol , runtime_checkable
2729
2830from pydantic import Field
2931
3234 ScheduledRequestInfo ,
3335 SchedulerState ,
3436 SchedulerUpdateAction ,
37+ SchedulerUpdateActionProgress ,
3538)
3639from guidellm .utils import RegistryMixin
3740
@@ -127,7 +130,7 @@ def resolve(
127130 cls ,
128131 initializers : dict [
129132 str ,
130- Union [ Any , dict [str , Any ], Constraint , ConstraintInitializer ] ,
133+ Any | dict [str , Any ] | Constraint | ConstraintInitializer ,
131134 ],
132135 ) -> dict [str , Constraint ]:
133136 """
@@ -154,7 +157,7 @@ def resolve(
154157 @classmethod
155158 def resolve_constraints (
156159 cls ,
157- constraints : dict [str , Union [ Any , dict [str , Any ], Constraint ] ],
160+ constraints : dict [str , Any | dict [str , Any ] | Constraint ],
158161 ) -> dict [str , Constraint ]:
159162 """
160163 Resolve constraints from mixed constraint specifications.
@@ -177,9 +180,7 @@ def resolve_constraints(
177180
178181
179182class _MaxNumberBase (StandardBaseModel ):
180- max_num : Union [int , float ] = Field (
181- gt = 0 , description = "Maximum number of requests allowed"
182- )
183+ max_num : int | float = Field (gt = 0 , description = "Maximum number of requests allowed" )
183184
184185
185186class MaxNumberConstraint (_MaxNumberBase ):
@@ -208,12 +209,12 @@ def __call__(
208209 "created_requests" : state .created_requests ,
209210 "processed_requests" : state .processed_requests ,
210211 },
211- progress = {
212- " remaining_fraction" : max (
212+ progress = SchedulerUpdateActionProgress (
213+ remaining_fraction = max (
213214 0.0 , 1.0 - state .processed_requests / float (self .max_num )
214215 ),
215- " remaining_requests" : max (0 , self .max_num - state .processed_requests ),
216- } ,
216+ remaining_requests = max (0 , self .max_num - state .processed_requests ),
217+ ) ,
217218 )
218219
219220
@@ -222,9 +223,7 @@ class MaxNumberConstraintInitializer(_MaxNumberBase):
222223 """Factory for creating MaxNumberConstraint instances."""
223224
224225 @classmethod
225- def from_simple_value (
226- cls , value : Union [int , float ]
227- ) -> "MaxNumberConstraintInitializer" :
226+ def from_simple_value (cls , value : int | float ) -> MaxNumberConstraintInitializer :
228227 """
229228 Create a MaxNumberConstraintInitializer from a simple scalar value.
230229
@@ -246,9 +245,7 @@ def create_constraint(self, **_kwargs) -> Constraint:
246245
247246
248247class _MaxDurationBase (StandardBaseModel ):
249- max_duration : Union [int , float ] = Field (
250- gt = 0 , description = "Maximum duration in seconds"
251- )
248+ max_duration : int | float = Field (gt = 0 , description = "Maximum duration in seconds" )
252249
253250
254251class MaxDurationConstraint (_MaxDurationBase ):
@@ -278,12 +275,10 @@ def __call__(
278275 "start_time" : state .start_time ,
279276 "current_time" : current_time ,
280277 },
281- progress = {
282- "remaining_fraction" : max (
283- 0.0 , 1.0 - elapsed / float (self .max_duration )
284- ),
285- "remaining_duration" : max (0.0 , self .max_duration - elapsed ),
286- },
278+ progress = SchedulerUpdateActionProgress (
279+ remaining_fraction = max (0.0 , 1.0 - elapsed / float (self .max_duration )),
280+ remaining_duration = max (0.0 , self .max_duration - elapsed ),
281+ ),
287282 )
288283
289284
@@ -292,9 +287,7 @@ class MaxDurationConstraintInitializer(_MaxDurationBase):
292287 """Factory for creating MaxDurationConstraint instances."""
293288
294289 @classmethod
295- def from_simple_value (
296- cls , value : Union [int , float ]
297- ) -> "MaxDurationConstraintInitializer" :
290+ def from_simple_value (cls , value : int | float ) -> MaxDurationConstraintInitializer :
298291 """
299292 Create a MaxDurationConstraintInitializer from a simple scalar value.
300293
@@ -316,7 +309,7 @@ def create_constraint(self, **_kwargs) -> Constraint:
316309
317310
318311class _MaxErrorsBase (StandardBaseModel ):
319- max_errors : Union [ int , float ] = Field (
312+ max_errors : int | float = Field (
320313 gt = 0 , description = "Maximum number of errors allowed"
321314 )
322315
@@ -352,9 +345,7 @@ class MaxErrorsConstraintInitializer(_MaxErrorsBase):
352345 """Factory for creating MaxErrorsConstraint instances."""
353346
354347 @classmethod
355- def from_simple_value (
356- cls , value : Union [int , float ]
357- ) -> "MaxErrorsConstraintInitializer" :
348+ def from_simple_value (cls , value : int | float ) -> MaxErrorsConstraintInitializer :
358349 """
359350 Create a MaxErrorsConstraintInitializer from a simple scalar value.
360351
@@ -376,10 +367,10 @@ def create_constraint(self, **_kwargs) -> Constraint:
376367
377368
378369class _MaxErrorRateBase (StandardBaseModel ):
379- max_error_rate : Union [ int , float ] = Field (
370+ max_error_rate : int | float = Field (
380371 gt = 0 , le = 1 , description = "Maximum error rate allowed (0.0 to 1.0)"
381372 )
382- window_size : Union [ int , float ] = Field (
373+ window_size : int | float = Field (
383374 default = 50 ,
384375 gt = 0 ,
385376 description = "Size of sliding window for calculating error rate" ,
@@ -444,9 +435,7 @@ class MaxErrorRateConstraintInitializer(_MaxErrorRateBase):
444435 """Factory for creating MaxErrorRateConstraint instances."""
445436
446437 @classmethod
447- def from_simple_value (
448- cls , value : Union [int , float ]
449- ) -> "MaxErrorRateConstraintInitializer" :
438+ def from_simple_value (cls , value : int | float ) -> MaxErrorRateConstraintInitializer :
450439 """
451440 Create a MaxErrorRateConstraintInitializer from a simple scalar value.
452441
@@ -469,10 +458,10 @@ def create_constraint(self, **_kwargs) -> Constraint:
469458
470459
471460class _MaxGlobalErrorRateBase (StandardBaseModel ):
472- max_error_rate : Union [ int , float ] = Field (
461+ max_error_rate : int | float = Field (
473462 gt = 0 , le = 1 , description = "Maximum error rate allowed (0.0 to 1.0)"
474463 )
475- min_processed : Optional [ Union [ int , float ]] = Field (
464+ min_processed : int | float | None = Field (
476465 default = 50 ,
477466 gt = 30 ,
478467 description = (
@@ -524,8 +513,8 @@ class MaxGlobalErrorRateConstraintInitializer(_MaxGlobalErrorRateBase):
524513
525514 @classmethod
526515 def from_simple_value (
527- cls , value : Union [ int , float ]
528- ) -> " MaxGlobalErrorRateConstraintInitializer" :
516+ cls , value : int | float
517+ ) -> MaxGlobalErrorRateConstraintInitializer :
529518 """
530519 Create a MaxGlobalErrorRateConstraintInitializer from a simple scalar value.
531520
0 commit comments