Skip to content

Commit

Permalink
fix(patterns): support string inputs for builder()
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs committed Sep 26, 2023
1 parent f320c2e commit 3610e52
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ibis/common/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,12 +1800,12 @@ def builder(obj):
elif callable(obj):
# the callable builds the substitution
return Factory(obj)
elif isinstance(obj, Sequence):
# allow nesting builder patterns in tuples/lists
return Call(lambda *args: type(obj)(args), *obj)
elif isinstance(obj, Mapping):
# allow nesting builder patterns in dicts
return Call(type(obj), **obj)
elif is_iterable(obj):
# allow nesting builder patterns in tuples/lists
return Call(lambda *args: type(obj)(args), *obj)
else:
# the object is used as a constant value
return Just(obj)
Expand Down
20 changes: 20 additions & 0 deletions ibis/common/tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,15 @@ def fn(x, ctx):
assert builder(Just(Just(1))) == Just(1)
assert builder(fn) == Factory(fn)

b = builder(())
assert b.build({}) == ()

b = builder([])
assert b.build({}) == []

b = builder({})
assert b.build({}) == {}

b = builder((1, 2, 3))
assert b.args == (Just(1), Just(2), Just(3))
assert b.build({}) == (1, 2, 3)
Expand All @@ -1275,3 +1284,14 @@ def fn(x, ctx):
b = builder(FrozenDict({"a": 1, "b": 2, "c": 3}))
assert b.kwargs == {"a": Just(1), "b": Just(2), "c": Just(3)}
assert b.build({}) == FrozenDict({"a": 1, "b": 2, "c": 3})


def test_builder_supports_string_arguments():
# builder() is applied on all arguments of Call() except the first one and
# sequences are transparently handled, the check far sequences was incorrect
# for strings causing infinite recursion
b = builder("3.14")
assert b.build({}) == "3.14"

c = Call(float, "1.1")
assert c.build({}) == 1.1

0 comments on commit 3610e52

Please sign in to comment.