Skip to content

Commit

Permalink
fix(interactive-repr): ensure that null scalars can be repred interac…
Browse files Browse the repository at this point in the history
…tively
  • Loading branch information
cpcloud committed Feb 4, 2025
1 parent 3e86e2d commit a3d8de2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
67 changes: 67 additions & 0 deletions ibis/backends/duckdb/tests/test_datatypes.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from __future__ import annotations

import string

Check warning on line 3 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L3

Added line #L3 was not covered by tests

import hypothesis as h
import hypothesis.strategies as st

Check warning on line 6 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L5-L6

Added lines #L5 - L6 were not covered by tests
import numpy as np
import pytest
from pytest import param

import ibis
import ibis.expr.datatypes as dt
import ibis.tests.strategies as its

Check warning on line 13 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L13

Added line #L13 was not covered by tests
from ibis.backends.sql.datatypes import DuckDBType


Expand Down Expand Up @@ -115,3 +120,65 @@ def test_cast_to_floating_point_type(con, snapshot, typ):

sql = str(ibis.to_sql(expr, dialect="duckdb"))
snapshot.assert_match(sql, "out.sql")


def null_literal(array_min_size: int = 0, array_max_size: int | None = None):
true = st.just(True)

Check warning on line 126 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L125-L126

Added lines #L125 - L126 were not covered by tests

field_names = st.text(alphabet=string.ascii_letters + string.digits, min_size=1)
num_fields = st.integers(min_value=1, max_value=20)

Check warning on line 129 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L128-L129

Added lines #L128 - L129 were not covered by tests

recursive = st.deferred(

Check warning on line 131 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L131

Added line #L131 was not covered by tests
lambda: (
its.primitive_dtypes(nullable=true, include_null=False)
| its.string_dtype(nullable=true)
| its.binary_dtype(nullable=true)
| st.builds(dt.JSON, binary=st.just(False), nullable=true)
| its.macaddr_dtype(nullable=true)
| its.uuid_dtype(nullable=true)
| its.temporal_dtypes(nullable=true)
| its.array_dtypes(
recursive,
nullable=true,
length=st.integers(min_value=array_min_size, max_value=array_max_size),
)
| its.map_dtypes(recursive, recursive, nullable=true)
| its.struct_dtypes(
recursive, nullable=true, names=field_names, num_fields=num_fields
)
)
)

return st.builds(

Check warning on line 152 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L152

Added line #L152 was not covered by tests
ibis.null,
its.primitive_dtypes(nullable=true, include_null=False)
| its.string_dtype(nullable=true)
| its.binary_dtype(nullable=true)
| st.builds(dt.JSON, binary=st.just(False), nullable=true)
| its.macaddr_dtype(nullable=true)
| its.uuid_dtype(nullable=true)
| its.temporal_dtypes(nullable=true)
| its.array_dtypes(
recursive,
nullable=true,
length=st.integers(min_value=array_min_size, max_value=array_max_size),
)
| its.map_dtypes(recursive, recursive, nullable=true)
| its.struct_dtypes(
recursive, nullable=true, names=field_names, num_fields=num_fields
),
)


@h.given(lit=null_literal(array_min_size=1, array_max_size=8192))
@h.settings(suppress_health_check=[h.HealthCheck.function_scoped_fixture])
def test_null_scalar(con, lit, monkeypatch):
monkeypatch.setattr(ibis.options, "default_backend", con)
monkeypatch.setattr(ibis.options, "interactive", True)

Check warning on line 177 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L173-L177

Added lines #L173 - L177 were not covered by tests

result = repr(lit)
expected = """\

Check warning on line 180 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L179-L180

Added lines #L179 - L180 were not covered by tests
┌──────┐
│ NULL │
└──────┘"""
assert result == expected

Check warning on line 184 in ibis/backends/duckdb/tests/test_datatypes.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/duckdb/tests/test_datatypes.py#L184

Added line #L184 was not covered by tests
2 changes: 1 addition & 1 deletion ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2881,7 +2881,7 @@ def null(type: dt.DataType | str | None = None) -> Value:
AttributeError: 'NullScalar' object has no attribute 'upper'
>>> ibis.null(str).upper()
┌──────┐
None
NULL
└──────┘
>>> ibis.null(str).upper().isnull()
┌──────┐
Expand Down
22 changes: 14 additions & 8 deletions ibis/expr/types/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,20 @@ def _to_rich_scalar(
max_string: int | None = None,
max_depth: int | None = None,
) -> Pretty:
value = format_values(
expr.type(),
[expr.to_pyarrow().as_py()],
max_length=max_length or ibis.options.repr.interactive.max_length,
max_string=max_string or ibis.options.repr.interactive.max_string,
max_depth=max_depth or ibis.options.repr.interactive.max_depth,
)[0]
return Panel(value, expand=False, box=box.SQUARE)
value = expr.to_pyarrow().as_py()

if value is None:
formatted_value = Text.styled("NULL", style="dim")
else:
(formatted_value,) = format_values(
expr.type(),
[value],
max_length=max_length or ibis.options.repr.interactive.max_length,
max_string=max_string or ibis.options.repr.interactive.max_string,
max_depth=max_depth or ibis.options.repr.interactive.max_depth,
)

return Panel(formatted_value, expand=False, box=box.SQUARE)


def _to_rich_table(
Expand Down
20 changes: 11 additions & 9 deletions ibis/tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,25 @@ def interval_dtype(interval=_interval, nullable=_nullable):
return st.builds(dt.Interval, unit=interval, nullable=nullable)


def temporal_dtypes(timezone=_timezone, interval=_interval, nullable=_nullable):
def temporal_dtypes(timezone=_timezone, nullable=_nullable):
return st.one_of(
date_dtype(nullable=nullable),
time_dtype(nullable=nullable),
timestamp_dtype(timezone=timezone, nullable=nullable),
)


def primitive_dtypes(nullable=_nullable):
return st.one_of(
null_dtype,
boolean_dtype(nullable=nullable),
integer_dtypes(nullable=nullable),
floating_dtypes(nullable=nullable),
date_dtype(nullable=nullable),
time_dtype(nullable=nullable),
def primitive_dtypes(nullable=_nullable, include_null=True):
primitive = (
boolean_dtype(nullable=nullable)
| integer_dtypes(nullable=nullable)
| floating_dtypes(nullable=nullable)
| date_dtype(nullable=nullable)
| time_dtype(nullable=nullable)
)
if include_null:
primitive |= null_dtype
return primitive


_item_strategy = primitive_dtypes()
Expand Down

0 comments on commit a3d8de2

Please sign in to comment.