Skip to content

Commit

Permalink
fix(patterns): allow deferred and builder in Capture pattern (#5)
Browse files Browse the repository at this point in the history
Accept Deferred and Builder in Capture instead of only accepting str.
  • Loading branch information
cpcloud authored Aug 6, 2024
1 parent 0fb1449 commit 694156e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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

0 comments on commit 694156e

Please sign in to comment.