Skip to content

Commit

Permalink
Merge branch 'main' into json_boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-ai authored Sep 10, 2024
2 parents e81fc93 + 418fc03 commit 4df3630
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions guidance/_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ def match(

try:
parser.consume_bytes(byte_string)
if not allow_partial:
parser.force_done()
except _parser.ByteParserException:
if raise_exceptions:
raise
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/test_grammar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import guidance
from guidance import gen, models, optional, select, string
from guidance._parser import ByteParserException


def test_select_reset_pos():
Expand Down Expand Up @@ -132,4 +133,12 @@ def test_partial_match(self, string):
match = g.match(f"123{string}", allow_partial=True)
assert match is not None
assert match.partial
assert match.captures["mycap"] == string
assert match.captures["mycap"] == string

def test_raises_on_incomplete_input(self):
g = "123" + gen(regex=r"\d+x?", name="mycap")
# Ok since we allow partial
assert g.match(b"123", raise_exceptions=True, allow_partial=True) is not None
# Shold raise since we don't allow partial
with pytest.raises(ByteParserException):
g.match(b"123", raise_exceptions=True)

0 comments on commit 4df3630

Please sign in to comment.