Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return DSL string for DataDatatype.__repr__() #10726

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions ibis/expr/datatypes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,36 @@ def dtype(value: Any, nullable: bool = True) -> DataType:
Examples
--------
>>> import ibis
>>> ibis.dtype("int32")
Int32(nullable=True)
>>> ibis.dtype("array<float>")
Array(value_type=Float64(nullable=True), nullable=True)
>>> type(ibis.dtype("int32"))
<class 'ibis.expr.datatypes.core.Int32>
>>> dt = ibis.dtype("array<!float>")
>>> type(dt)
<class 'ibis.expr.datatypes.core.Array'>
>>> type(dt.value_type)
<class 'ibis.expr.datatypes.core.Float64'>
>>> dt.nullable
True
>>> dt.value_type.nullable
False

DataType objects may also be created from Python types:

>>> ibis.dtype(int)
Int64(nullable=True)
int64
>>> ibis.dtype(list[float])
Array(value_type=Float64(nullable=True), nullable=True)
array<float64>

Or other type systems, like numpy/pandas/pyarrow types:

>>> import pyarrow as pa
>>> ibis.dtype(pa.int32())
Int32(nullable=True)
int32

"""
if isinstance(value, DataType):
return value
else:
return DataType.from_typehint(value)
return DataType.from_typehint(value, nullable=nullable)


@dtype.register(str)
Expand Down Expand Up @@ -145,6 +152,9 @@ def __str__(self) -> str:
prefix = "!" * (not self.nullable)
return f"{prefix}{self.name.lower()}{self._pretty_piece}"

def __repr__(self) -> str:
return str(self)

def equals(self, other):
if not isinstance(other, DataType):
raise TypeError(
Expand Down
10 changes: 5 additions & 5 deletions ibis/expr/datatypes/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,16 +487,16 @@ def test_timestamp_with_invalid_timezone():
assert str(ts) == "timestamp('Foo/Bar&234')"


def test_timestamp_with_timezone_repr():
ts = dt.Timestamp("UTC")
assert repr(ts) == "Timestamp(timezone='UTC', scale=None, nullable=True)"


def test_timestamp_with_timezone_str():
ts = dt.Timestamp("UTC")
assert str(ts) == "timestamp('UTC')"


def test_timestamp_with_timezone_scale_str():
ts = dt.Timestamp("UTC", scale=4)
assert str(ts) == "timestamp('UTC', 4)"


def test_time_str():
assert str(dt.time) == "time"

Expand Down
2 changes: 1 addition & 1 deletion ibis/expr/datatypes/tests/test_pandas_numpy_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,5 @@
def test_normalize_non_convertible_boolean():
typ = dt.boolean
value = np.array([1, 2, 3])
with pytest.raises(TypeError, match="Unable to normalize .+ to Boolean"):
with pytest.raises(TypeError, match="Unable to normalize .+ to boolean"):

Check warning on line 198 in ibis/expr/datatypes/tests/test_pandas_numpy_value.py

View check run for this annotation

Codecov / codecov/patch

ibis/expr/datatypes/tests/test_pandas_numpy_value.py#L198

Added line #L198 was not covered by tests
dt.normalize(typ, value)
4 changes: 2 additions & 2 deletions ibis/expr/datatypes/tests/test_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ def test_normalize_none_with_non_nullable_type():
@pytest.mark.parametrize("kind", ["uint", "int"])
def test_normalize_non_convertible_int(kind, bits):
typ = getattr(dt, f"{kind}{bits:d}")
with pytest.raises(TypeError, match="Unable to normalize .+ to U?Int"):
with pytest.raises(TypeError, match="Unable to normalize .+ to u?int"):
dt.normalize(typ, "not convertible")


@pytest.mark.parametrize("typename", ["float32", "float64"])
def test_normalize_non_convertible_float(typename):
typ = getattr(dt, typename)
with pytest.raises(TypeError, match="Unable to normalize .+ to Float"):
with pytest.raises(TypeError, match="Unable to normalize .+ to float"):
dt.normalize(typ, "not convertible")


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Literal(1, Int8(nullable=True), 'foo') too many positional arguments
Literal(1, int8, 'foo') too many positional arguments

Expected signature: Literal(value: Annotated[Any, Not(pattern=InstanceOf(type=<class 'Deferred'>))], dtype: DataType)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Literal(1, Int8(nullable=True), name='foo') got an unexpected keyword argument 'name'
Literal(1, int8, name='foo') got an unexpected keyword argument 'name'

Expected signature: Literal(value: Annotated[Any, Not(pattern=InstanceOf(type=<class 'Deferred'>))], dtype: DataType)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Literal(1, Int8(nullable=True), dtype=Int16(nullable=True)) multiple values for argument 'dtype'
Literal(1, int8, dtype=int16) multiple values for argument 'dtype'

Expected signature: Literal(value: Annotated[Any, Not(pattern=InstanceOf(type=<class 'Deferred'>))], dtype: DataType)
18 changes: 9 additions & 9 deletions ibis/expr/types/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ def type(self) -> dt.DataType:
... "timestamp_col": [
... datetime(2024, 11, 2, 10, 5, 2),
... ],
... "string_col": ["a"],
... "array_of_string_col": [["a", "b", "c"]],
... }
... )

>>> t.int_col.type()
Int64(nullable=True)
>>> t.timestamp_col.type()
Timestamp(timezone=None, scale=None, nullable=True)
>>> t.string_col.type()
String(nullable=True)
>>> t.int_col.type(), type(t.int_col.type())
(int64, <class 'ibis.expr.datatypes.core.Int64'>)
>>> t.timestamp_col.type(), type(t.timestamp_col.type())
(timestamp, <class 'ibis.expr.datatypes.core.Timestamp'>)
>>> t.timestamp_col.type(), type(t.array_of_string_col.type())
(array<string>, <class 'ibis.expr.datatypes.core.Array'>)
"""
return self.op().dtype

Expand Down Expand Up @@ -2934,13 +2934,13 @@ def literal(value: Any, type: dt.DataType | str | None = None) -> Scalar:
>>> import ibis
>>> x = ibis.literal(42)
>>> x.type()
Int8(nullable=True)
int8

Construct a `float64` literal from an `int`

>>> y = ibis.literal(42, type="double")
>>> y.type()
Float64(nullable=True)
float64

Ibis checks for invalid types

Expand Down
2 changes: 1 addition & 1 deletion ibis/tests/expr/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,5 @@ def test_decimal_str(lineitem):
def test_decimal_repr(lineitem):
col = lineitem.l_extendedprice
t = col.type()
expected = f"Decimal(precision={t.precision:d}, scale={t.scale:d}, nullable=True)"
expected = f"decimal({t.precision:d}, {t.scale:d})"
assert repr(t) == expected
Loading