Skip to content

Commit f129378

Browse files
committed
fix(patterns): allow deferred and builder in Capture pattern
1 parent 0fb1449 commit f129378

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

koerce/patterns.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,10 +1220,10 @@ class Capture(Pattern):
12201220
12211221
"""
12221222

1223-
key: str
1223+
key: str | Deferred | Builder
12241224
what: Pattern
12251225

1226-
def __init__(self, key, what=_any):
1226+
def __init__(self, key: str | Deferred | Builder, what=_any):
12271227
if isinstance(key, (Deferred, Builder)):
12281228
key = builder(key)
12291229
if isinstance(key, Variable):

koerce/tests/test_patterns.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,21 @@ def test_capture():
520520
assert ctx == {"result": 12}
521521

522522

523+
@pytest.mark.parametrize(
524+
"x", [Deferred(Variable("x")), Variable("x")], ids=["deferred", "builder"]
525+
)
526+
def test_capture_with_deferred_and_builder(x):
527+
ctx = {}
528+
529+
p = Capture(x, InstanceOf(float))
530+
531+
assert p.apply(1, context=ctx) is NoMatch
532+
assert ctx == {}
533+
534+
assert p.apply(1.0, ctx) == 1.0
535+
assert ctx == {"x": 1.0}
536+
537+
523538
def test_none_of():
524539
def negative(x):
525540
return x < 0

0 commit comments

Comments
 (0)