Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(patterns): allow deferred and builder in Capture pattern #5

Merged
merged 2 commits into from
Aug 6, 2024
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
2 changes: 1 addition & 1 deletion koerce/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ class Capture(Pattern):
key: str
what: Pattern

def __init__(self, key, what=_any):
def __init__(self, key: str | Deferred | Builder, what=_any):
if isinstance(key, (Deferred, Builder)):
key = builder(key)
if isinstance(key, Variable):
Expand Down
15 changes: 15 additions & 0 deletions koerce/tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,21 @@ def test_capture():
assert ctx == {"result": 12}


@pytest.mark.parametrize(
"x", [Deferred(Variable("x")), Variable("x")], ids=["deferred", "builder"]
)
def test_capture_with_deferred_and_builder(x):
ctx = {}

p = Capture(x, InstanceOf(float))

assert p.apply(1, context=ctx) is NoMatch
assert ctx == {}

assert p.apply(1.0, ctx) == 1.0
assert ctx == {"x": 1.0}


def test_none_of():
def negative(x):
return x < 0
Expand Down
Loading