From 3610e523ddd05140873dbeb041a27e6ebe931e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20Sz=C5=B1cs?= Date: Tue, 26 Sep 2023 15:27:17 +0200 Subject: [PATCH] fix(patterns): support string inputs for builder() --- ibis/common/patterns.py | 6 +++--- ibis/common/tests/test_patterns.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ibis/common/patterns.py b/ibis/common/patterns.py index 0f7b502d5be2..af2f58fecc65 100644 --- a/ibis/common/patterns.py +++ b/ibis/common/patterns.py @@ -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) diff --git a/ibis/common/tests/test_patterns.py b/ibis/common/tests/test_patterns.py index c7f4b44261e2..5f509bba19c0 100644 --- a/ibis/common/tests/test_patterns.py +++ b/ibis/common/tests/test_patterns.py @@ -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) @@ -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