Skip to content

Commit

Permalink
feat(builders): expose resolve() and support custom deferred repr
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs committed Sep 10, 2024
1 parent 2a846fe commit c7d5f6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 4 additions & 2 deletions koerce/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ def __init__(self, value: Any):
self.value = value

def __repr__(self):
if callable(self.value):
if hasattr(self.value, "__deferred_repr__"):
return self.value.__deferred_repr__()
elif callable(self.value):
return getattr(self.value, "__name__", repr(self.value))
else:
return repr(self.value)
Expand Down Expand Up @@ -767,6 +769,6 @@ def deferred(obj, allow_custom=False) -> Deferred:
return Deferred(builder(obj, allow_custom))


def resolve(obj, context):
def resolve(obj, **context):
bldr: Builder = builder(obj)
return bldr.build(context)
2 changes: 1 addition & 1 deletion koerce/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ class Custom(Pattern):
"""

func: Callable
func = cython.declare(object, visibility="readonly")

def __init__(self, func):
self.func = func
Expand Down
13 changes: 12 additions & 1 deletion koerce/tests/test_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from koerce._internal import (
Attr,
Binop,
Builder,
Call,
Call0,
Call1,
Expand All @@ -18,11 +19,11 @@
Item,
Just,
Map,
Builder,
Seq,
Unop,
Var,
builder,
resolve,
)

_ = Deferred(Var("_"))
Expand Down Expand Up @@ -349,6 +350,11 @@ def table():
)


def test_custom_deferred_repr(table):
expr = _.x + table.a
assert repr(expr) == "($_.x + <column[int]>)"


def test_deferred_getitem(table):
expr = _["a"]
assert resolve(expr, table) == table["a"]
Expand Down Expand Up @@ -518,3 +524,8 @@ def test_builder_coercion():
assert Builder.__coerce__(Var("a")) == Var("a")
with pytest.raises(ValueError):
Builder.__coerce__(1)


def test_resolve():
deferred = _["a"] + 1
assert resolve(deferred, _={"a": 1}) == 2

0 comments on commit c7d5f6f

Please sign in to comment.