Skip to content

Commit

Permalink
feat(api): improve repr of deferred functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and cpcloud committed Sep 27, 2023
1 parent eda8ff4 commit f2b3744
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ibis/expr/deferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ def __init__(self, func: Any, *args: Any, **kwargs: Any) -> None:
def __repr__(self) -> str:
params = [repr(a) for a in self._args]
params.extend(f"{k}={v!r}" for k, v in self._kwargs.items())
return f"{self._func!r}({', '.join(params)})"
# Repr directly wrapped functions as their name, fallback to repr for
# deferred objects or callables without __name__ otherwise
func = getattr(self._func, "__name__", "") or repr(self._func)
return f"{func}({', '.join(params)})"

def _resolve(self, param: Any) -> Any:
func = _resolve(self._func, param)
Expand Down
14 changes: 14 additions & 0 deletions ibis/expr/tests/test_deferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import ibis
from ibis import _
from ibis.expr.deferred import deferred_apply


@pytest.fixture
Expand Down Expand Up @@ -100,6 +101,19 @@ def test_method_kwargs(table):
assert repr(expr) == "_.a.log(base=_.b)"


def test_deferred_apply(table):
expr = deferred_apply(operator.add, _.a, 2)
res = expr.resolve(table)
assert res.equals(table.a + 2)
assert repr(expr) == "add(_.a, 2)"

func = lambda a, b: a + b
expr = deferred_apply(func, _.a, 2)
res = expr.resolve(table)
assert res.equals(table.a + 2)
assert func.__name__ in repr(expr)


@pytest.mark.parametrize(
"symbol, op",
[
Expand Down

0 comments on commit f2b3744

Please sign in to comment.