From 2fcf8509fa41802d06abdb6448ac5051ad976b76 Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Tue, 19 Mar 2024 10:21:30 -0800 Subject: [PATCH] chore: apply code review suggestions --- ibis/backends/tests/test_array.py | 9 ++++++--- ibis/backends/tests/test_map.py | 7 +++++-- ibis/backends/tests/test_struct.py | 6 ++++-- ibis/expr/types/arrays.py | 3 ++- ibis/expr/types/maps.py | 2 +- ibis/expr/types/structs.py | 1 + 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ibis/backends/tests/test_array.py b/ibis/backends/tests/test_array.py index 992ce6dc02020..fc42656f72bbf 100644 --- a/ibis/backends/tests/test_array.py +++ b/ibis/backends/tests/test_array.py @@ -73,10 +73,13 @@ def test_array_factory(con): a = ibis.array([1, 2, 3]) assert con.execute(a) == [1, 2, 3] + a2 = ibis.array(a) assert con.execute(a2) == [1, 2, 3] + typed = ibis.array([1, 2, 3], type="array") assert con.execute(typed) == [1.0, 2.0, 3.0] + typed2 = ibis.array(a, type="array") assert con.execute(typed2) == [1.0, 2.0, 3.0] @@ -86,8 +89,8 @@ def test_array_factory_empty(con): with pytest.raises(TypeError): ibis.array([]) - empty_typed = ibis.array([], type="array") - assert str(empty_typed.type()) == "array" + empty_typed = ibis.array([], type="array") + assert empty_typed.type() == dt.Array(value_type=dt.float64) assert con.execute(empty_typed) == [] @@ -100,7 +103,7 @@ def test_array_factory_null(con): with pytest.raises(TypeError): ibis.array(None) none_typed = ibis.array(None, type="array") - assert str(none_typed.type()) == "array" + assert none_typed.type() == dt.Array(value_type=dt.float64) assert con.execute(none_typed) is None diff --git a/ibis/backends/tests/test_map.py b/ibis/backends/tests/test_map.py index f03081f6c6011..1180f82ccf7f4 100644 --- a/ibis/backends/tests/test_map.py +++ b/ibis/backends/tests/test_map.py @@ -39,10 +39,13 @@ def test_map_factory(con): m = ibis.map({"a": 1, "b": 2}) assert con.execute(m) == {"a": 1, "b": 2} + m2 = ibis.map(m) assert con.execute(m2) == {"a": 1, "b": 2} + typed = ibis.map({"a": 1, "b": 2}, type="map") assert con.execute(typed) == {"a": 1.0, "b": 2.0} + typed2 = ibis.map(m, type="map") assert con.execute(typed2) == {"a": 1.0, "b": 2.0} @@ -57,7 +60,7 @@ def test_map_factory_empty(con): with pytest.raises(TypeError): ibis.map({}) empty_typed = ibis.map({}, type="map") - assert str(empty_typed.type()) == "map" + assert empty_typed.type() == dt.Map(key_type=dt.string, value_type=dt.string) assert con.execute(empty_typed) == {} @@ -68,7 +71,7 @@ def test_map_factory_null(con): with pytest.raises(TypeError): ibis.map(None) null_typed = ibis.map(None, type="map") - assert str(null_typed.type()) == "map" + assert null_typed.type() == dt.Map(key_type=dt.string, value_type=dt.string) assert con.execute(null_typed) is None diff --git a/ibis/backends/tests/test_struct.py b/ibis/backends/tests/test_struct.py index e2cf8fde0967a..29c5e579237fc 100644 --- a/ibis/backends/tests/test_struct.py +++ b/ibis/backends/tests/test_struct.py @@ -25,14 +25,16 @@ @pytest.mark.notimpl(["postgres"]) -# @pytest.mark.broken(["pandas", "dask"], reason="casting is broken") def test_struct_factory(con): s = ibis.struct({"a": 1, "b": 2}) assert con.execute(s) == {"a": 1, "b": 2} + s2 = ibis.struct(s) assert con.execute(s2) == {"a": 1, "b": 2} + typed = ibis.struct({"a": 1, "b": 2}, type="struct") assert con.execute(typed) == {"a": 1.0, "b": 2.0} + typed2 = ibis.struct(s, type="struct") assert con.execute(typed2) == {"a": 1.0, "b": 2.0} @@ -52,7 +54,7 @@ def test_struct_factory_null(con): with pytest.raises(TypeError): ibis.struct(None) none_typed = ibis.struct(None, type="struct") - assert str(none_typed.type()) == "struct" + assert none_typed.type() == dt.Struct(fields={"a": dt.float64, "b": dt.float64}) assert con.execute(none_typed) is None diff --git a/ibis/expr/types/arrays.py b/ibis/expr/types/arrays.py index 122988354579c..87e9daea732c7 100644 --- a/ibis/expr/types/arrays.py +++ b/ibis/expr/types/arrays.py @@ -14,7 +14,7 @@ if TYPE_CHECKING: from collections.abc import Iterable - from ibis.expr.types import dt + import ibis.expr.datatypes as dt from ibis.expr.types.typing import V import ibis.common.exceptions as com @@ -1082,6 +1082,7 @@ def __getitem__(self, index: int | ir.IntegerValue | slice) -> ir.Column: @deferrable def array( values: ArrayValue | Iterable[V] | None, + *, type: str | dt.DataType | None = None, ) -> ArrayValue: """Create an array expression. diff --git a/ibis/expr/types/maps.py b/ibis/expr/types/maps.py index b514ce56dfd4a..58dbf0bd0fb04 100644 --- a/ibis/expr/types/maps.py +++ b/ibis/expr/types/maps.py @@ -13,7 +13,7 @@ if TYPE_CHECKING: from collections.abc import Iterable, Mapping - from ibis.expr import datatypes as dt + import ibis.expr.datatypes as dt @public diff --git a/ibis/expr/types/structs.py b/ibis/expr/types/structs.py index af4c7c86e7a0c..49bb51ba79c67 100644 --- a/ibis/expr/types/structs.py +++ b/ibis/expr/types/structs.py @@ -23,6 +23,7 @@ @deferrable def struct( value: Iterable[tuple[str, V]] | Mapping[str, V] | StructValue | None, + *, type: str | dt.DataType | None = None, ) -> StructValue: """Create a struct expression.