Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/outlines/types/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ class QuantifyExact(Term):
term: Term
count: int

def __post_init__(self):
if self.count < 0:
raise ValueError("QuantifyExact: `count` must be a non-negative integer.")

def _display_node(self) -> str:
return f"Quantify({{{self.count}}})"

Expand All @@ -589,6 +593,12 @@ class QuantifyMinimum(Term):
term: Term
min_count: int

def __post_init__(self):
if self.min_count < 0:
raise ValueError(
"QuantifyMinimum: `min_count` must be a non-negative integer."
)

def _display_node(self) -> str:
return f"Quantify({{{self.min_count},}})"

Expand All @@ -606,6 +616,12 @@ class QuantifyMaximum(Term):
term: Term
max_count: int

def __post_init__(self):
if self.max_count < 0:
raise ValueError(
"QuantifyMaximum: `max_count` must be a non-negative integer."
)

def _display_node(self) -> str:
return f"Quantify({{,{self.max_count}}})"

Expand All @@ -625,6 +641,10 @@ class QuantifyBetween(Term):
max_count: int

def __post_init__(self):
if self.min_count < 0:
raise ValueError(
"QuantifyBetween: `min_count` must be a non-negative integer."
)
if self.min_count > self.max_count:
raise ValueError(
"QuantifyBetween: `max_count` must be greater than `min_count`."
Expand Down
12 changes: 12 additions & 0 deletions tests/types/test_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ def test_dsl_init():
assert repr(maximum) == "QuantifyMaximum(term=String(value='test'), max_count=3)"
assert maximum.display_ascii_tree() == "└── Quantify({,3})\n └── String('test')\n"

with pytest.raises(ValueError, match="`count` must be a non-negative integer"):
QuantifyExact(string, -1)

with pytest.raises(ValueError, match="`min_count` must be a non-negative integer"):
QuantifyMinimum(string, -1)

with pytest.raises(ValueError, match="`max_count` must be a non-negative integer"):
QuantifyMaximum(string, -1)

between = QuantifyBetween(string, 1, 3)
assert between.term == string
assert between.min_count == 1
Expand All @@ -153,6 +162,9 @@ def test_dsl_init():
):
QuantifyBetween(string, 3, 1)

with pytest.raises(ValueError, match="`min_count` must be a non-negative integer"):
QuantifyBetween(string, -2, -1)


def test_dsl_term_methods():
a = String("a")
Expand Down
Loading