Skip to content

Commit

Permalink
Use tuple instead of list in validation options
Browse files Browse the repository at this point in the history
This enesures that this type can be serialized.
  • Loading branch information
naddeoa committed Mar 25, 2024
1 parent 8b07401 commit 3c599eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions langkit/validators/comparison.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass, replace
from functools import partial
from typing import Any, Callable, List, Literal, Optional, Sequence, Set, Union
from typing import Any, Callable, List, Literal, Optional, Sequence, Set, Tuple, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -147,8 +147,8 @@ class ConstraintValidatorOptions:
upper_threshold_inclusive: Optional[Union[float, int]] = None
lower_threshold: Optional[Union[float, int]] = None
lower_threshold_inclusive: Optional[Union[float, int]] = None
one_of: Optional[Sequence[Union[str, float, int]]] = None
none_of: Optional[Sequence[Union[str, float, int]]] = None
one_of: Optional[Tuple[Union[str, float, int], ...]] = None
none_of: Optional[Tuple[Union[str, float, int], ...]] = None
must_be_non_none: Optional[bool] = None
must_be_none: Optional[bool] = None

Expand Down Expand Up @@ -208,7 +208,7 @@ def validate_result(self, df: pd.DataFrame) -> Optional[ValidationResult]:

@dataclass
class MultiColumnConstraintValidatorOptions:
constraints: List[ConstraintValidatorOptions]
constraints: Tuple[ConstraintValidatorOptions, ...]
operator: Literal["AND", "OR"] = "AND"
report_mode: Literal["ALL_FAILED_METRICS", "FIRST_FAILED_METRIC"] = "FIRST_FAILED_METRIC"

Expand Down
6 changes: 3 additions & 3 deletions langkit/validators/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def constraint(
upper_threshold_inclusive=upper_threshold_inclusive,
lower_threshold=lower_threshold,
lower_threshold_inclusive=lower_threshold_inclusive,
one_of=one_of,
none_of=none_of,
one_of=tuple(one_of) if one_of else None,
none_of=tuple(none_of) if none_of else None,
must_be_non_none=must_be_non_none,
must_be_none=must_be_none,
)
Expand All @@ -94,5 +94,5 @@ def multi_column_constraint(
report_mode: Literal["ALL_FAILED_METRICS", "FIRST_FAILED_METRIC"] = "FIRST_FAILED_METRIC",
) -> Validator:
return MultiColumnConstraintValidator(
MultiColumnConstraintValidatorOptions(constraints=constraints, operator=operator, report_mode=report_mode)
MultiColumnConstraintValidatorOptions(constraints=tuple(constraints), operator=operator, report_mode=report_mode)
)
24 changes: 12 additions & 12 deletions tests/langkit/validators/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ def test_must_be_non_none():
def test_multiple_contraint_first_failure():
validator = MultiColumnConstraintValidator(
MultiColumnConstraintValidatorOptions(
[
(
ConstraintValidatorOptions("prompt.stats.char_count", lower_threshold=5),
ConstraintValidatorOptions("prompt.stats.token_count", lower_threshold=5),
]
)
)
)
wf = Workflow(
Expand Down Expand Up @@ -188,10 +188,10 @@ def test_multiple_contraint_first_failure():
def test_multiple_constriant_all_failure():
validator = MultiColumnConstraintValidator(
MultiColumnConstraintValidatorOptions(
[
(
ConstraintValidatorOptions("prompt.stats.char_count", lower_threshold=5),
ConstraintValidatorOptions("prompt.stats.token_count", lower_threshold=5),
],
),
report_mode="ALL_FAILED_METRICS",
operator="AND",
)
Expand Down Expand Up @@ -234,10 +234,10 @@ def test_multiple_constriant_all_failure():
def test_multiple_constriant_all_failure_or():
validator = MultiColumnConstraintValidator(
MultiColumnConstraintValidatorOptions(
[
(
ConstraintValidatorOptions("prompt.stats.char_count", lower_threshold=4),
ConstraintValidatorOptions("prompt.stats.token_count", lower_threshold=4),
],
),
report_mode="ALL_FAILED_METRICS",
operator="OR",
)
Expand Down Expand Up @@ -268,10 +268,10 @@ def test_multiple_constriant_all_failure_or():
def test_multiple_constriant_first_failure_or():
validator = MultiColumnConstraintValidator(
MultiColumnConstraintValidatorOptions(
[
(
ConstraintValidatorOptions("prompt.stats.char_count", lower_threshold=4),
ConstraintValidatorOptions("prompt.stats.token_count", lower_threshold=4),
],
),
report_mode="FIRST_FAILED_METRIC",
operator="OR",
)
Expand Down Expand Up @@ -302,11 +302,11 @@ def test_multiple_constriant_first_failure_or():
def test_multiple_constriant_first_failure_or_multiple_failures():
validator = MultiColumnConstraintValidator(
MultiColumnConstraintValidatorOptions(
[
(
ConstraintValidatorOptions("prompt.stats.char_count", upper_threshold=100),
ConstraintValidatorOptions("prompt.stats.token_count", upper_threshold=1),
ConstraintValidatorOptions("prompt.regex.email_address", upper_threshold=0),
],
),
report_mode="FIRST_FAILED_METRIC",
operator="OR",
)
Expand Down Expand Up @@ -338,11 +338,11 @@ def test_multiple_constriant_first_failure_or_multiple_failures():
def test_multiple_constriant_first_failure_and_ordering():
validator = MultiColumnConstraintValidator(
MultiColumnConstraintValidatorOptions(
[
(
ConstraintValidatorOptions("prompt.regex.email_address", upper_threshold=0),
ConstraintValidatorOptions("prompt.stats.char_count", upper_threshold=1),
ConstraintValidatorOptions("prompt.stats.token_count", upper_threshold=1),
],
),
report_mode="FIRST_FAILED_METRIC",
operator="AND",
)
Expand Down

0 comments on commit 3c599eb

Please sign in to comment.