Skip to content

Commit

Permalink
fix(repr): ensure that column expressions are not promoted to table w…
Browse files Browse the repository at this point in the history
…hen repring non-interactively
  • Loading branch information
cpcloud committed Aug 21, 2023
1 parent 7434068 commit d57a162
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
16 changes: 13 additions & 3 deletions ibis/expr/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ibis.common.exceptions import IbisError, TranslationError
from ibis.common.grounds import Immutable
from ibis.common.patterns import Coercible, CoercionError
from ibis.config import _default_backend, options
from ibis.config import _default_backend, options as opts
from ibis.util import experimental

if TYPE_CHECKING:
Expand Down Expand Up @@ -46,6 +46,16 @@ class Expr(Immutable, Coercible):
__slots__ = ("_arg",)
_arg: ops.Node

def __rich_console__(self, console, options):
if not opts.interactive:
from rich.text import Text

return console.render(Text(self._repr()), options=options)
return self.__interactive_rich_console__(console, options)

def __interactive_rich_console__(self, console, options):
raise NotImplementedError()

def __init__(self, arg: ops.Node) -> None:
object.__setattr__(self, "_arg", arg)

Expand All @@ -62,7 +72,7 @@ def __coerce__(cls, value):
raise CoercionError("Unable to coerce value to an expression")

def __repr__(self) -> str:
if not options.interactive:
if not opts.interactive:
return self._repr()

from ibis.expr.types.pretty import simple_console
Expand Down Expand Up @@ -132,7 +142,7 @@ def get_name(self):
return self._arg.name

def _repr_png_(self) -> bytes | None:
if options.interactive or not options.graphviz_repr:
if opts.interactive or not opts.graphviz_repr:
return None
try:
import ibis.expr.visualize as viz
Expand Down
8 changes: 2 additions & 6 deletions ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,11 +925,7 @@ def to_pandas(self, **kwargs) -> pd.Series:

@public
class Scalar(Value):
def __rich_console__(self, console, options):
from rich.text import Text

if not ibis.options.interactive:
return console.render(Text(self._repr()), options=options)
def __interactive_rich_console__(self, console, options):
return console.render(repr(self.execute()), options=options)

def __pyarrow_result__(self, table: pa.Table) -> pa.Scalar:
Expand Down Expand Up @@ -995,7 +991,7 @@ def __getitem__(self, _):
def __array__(self, dtype=None):
return self.execute().__array__(dtype)

def __rich_console__(self, console, options):
def __interactive_rich_console__(self, console, options):
named = self.name(self.op().name)
projection = named.as_table()
return console.render(projection, options=options)
Expand Down
7 changes: 1 addition & 6 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,9 @@ def _cast(self, schema: SupportsSchema, cast_method: str = "cast") -> Table:
cols.append(new_col)
return self.select(*cols)

def __rich_console__(self, console, options):
from rich.text import Text

def __interactive_rich_console__(self, console, options):
from ibis.expr.types.pretty import to_rich_table

if not ibis.options.interactive:
return console.render(Text(self._repr()), options=options)

if console.is_jupyter:
# Rich infers a console width in jupyter notebooks, but since
# notebooks can use horizontal scroll bars we don't want to apply a
Expand Down
12 changes: 12 additions & 0 deletions ibis/tests/expr/test_pretty_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pandas as pd
import pytest
from rich.console import Console

import ibis
import ibis.expr.datatypes as dt
Expand Down Expand Up @@ -166,3 +167,14 @@ def test_all_empty_groups_repr():
dtype = dt.float64
fmts, *_ = format_column(dtype, values)
assert list(map(str, fmts)) == ["nan", "nan"]


def test_non_interactive_column_repr():
t = ibis.table(dict(names="string", values="int"))
expr = t.names
console = Console()
with console.capture() as capture:
console.print(expr)

result = capture.get()
assert "Selection" not in result

0 comments on commit d57a162

Please sign in to comment.