Skip to content

Commit

Permalink
fix(datatypes): disallow empty structs when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Oct 30, 2024
1 parent 2cd97c1 commit 6662f7f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ibis/expr/datatypes/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def geotype_parser(typ: type[dt.DataType]) -> dt.DataType:
struct = (
spaceless_string("struct")
.then(LANGLE)
.then(parsy.seq(spaceless(FIELD).skip(COLON), ty).sep_by(COMMA))
.then(parsy.seq(spaceless(FIELD).skip(COLON), ty).sep_by(COMMA, min=1))
.skip(COMMA.optional())
.skip(RANGLE)
.map(dt.Struct.from_tuples)
Expand Down
37 changes: 31 additions & 6 deletions ibis/expr/datatypes/tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,39 @@ def test_struct_with_string_types():
)


def test_struct_trailing_comma_single():
result = dt.dtype("struct<a: int32,>")
assert result == dt.Struct.from_tuples([("a", dt.int32)])
@pytest.mark.parametrize(
("type_string", "expected_dtype"),
[
("struct<a: int32,>", {"a": dt.int32}),
("struct<a: int32, b: string , >", {"a": dt.int32, "b": dt.string}),
],
ids=["single_field", "multiple_fields"],
)
def test_struct_trailing_comma(type_string, expected_dtype):
result = dt.dtype(type_string)
assert result == dt.Struct(expected_dtype)


def test_struct_trailing_comma_multi():
result = dt.dtype("struct<a: int32, b: string , >")
assert result == dt.Struct.from_tuples([("a", dt.int32), ("b", dt.string)])
@pytest.mark.parametrize(
"invalid_type_string",
[
"struct<,>",
"struct<a,>",
"struct<b: ,>",
"struct<c:in,>",
"struct<a:int,b:int64,,>",
],
ids=[
"missing_key",
"missing_colon",
"missing_value",
"invalid_type",
"double_comma",
],
)
def test_struct_trailing_comma_invalid(invalid_type_string):
with pytest.raises(parsy.ParseError):
dt.dtype(invalid_type_string)


def test_array_with_string_value_types():
Expand Down

0 comments on commit 6662f7f

Please sign in to comment.