Skip to content

Commit

Permalink
fix(api): ensure that normalization of boolean, ints and floats fail …
Browse files Browse the repository at this point in the history
…with readable error message
  • Loading branch information
cpcloud committed Aug 21, 2023
1 parent d57a162 commit 556f7cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
28 changes: 28 additions & 0 deletions ibis/expr/datatypes/tests/test_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,31 @@ def test_normalize_json():

with pytest.raises(TypeError):
dt.normalize(dt.json, "invalid")


def test_normalize_none_with_non_nullable_type():
typ = dt.Int64(nullable=False)
with pytest.raises(TypeError, match="Cannot convert `None` to non-nullable type"):
dt.normalize(typ, None)


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"):
dt.normalize(typ, value)


@pytest.mark.parametrize("bits", [8, 16, 32, 64])
@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"):
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"):
dt.normalize(typ, "not convertible")
8 changes: 4 additions & 4 deletions ibis/expr/datatypes/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,19 @@ def normalize(typ, value):
dtype = dt.dtype(typ)
if value is None:
if not dtype.nullable:
raise TypeError("Cannot convert `None` to non-nullable type {typ!r}")
raise TypeError(f"Cannot convert `None` to non-nullable type {typ!r}")
return None

if dtype.is_boolean():
try:
return bool(value)
except ValueError:
raise TypeError("Unable to normalize {value!r} to {dtype!r}")
raise TypeError(f"Unable to normalize {value!r} to {dtype!r}")
elif dtype.is_integer():
try:
value = int(value)
except ValueError:
raise TypeError("Unable to normalize {value!r} to {dtype!r}")
raise TypeError(f"Unable to normalize {value!r} to {dtype!r}")
if value not in dtype.bounds:
raise TypeError(
f"Value {value} is out of bounds for type {dtype!r} "
Expand All @@ -272,7 +272,7 @@ def normalize(typ, value):
try:
return float(value)
except ValueError:
raise TypeError("Unable to normalize {value!r} to {dtype!r}")
raise TypeError(f"Unable to normalize {value!r} to {dtype!r}")
elif dtype.is_json():
if isinstance(value, str):
try:
Expand Down

0 comments on commit 556f7cc

Please sign in to comment.