Skip to content

Commit 4583dee

Browse files
authored
feat(dtypes): allow passing nullable kwarg to string parsed dtypes (#10632)
## Description of changes Noted in #10243, passing `nullable` to `ibis.dtype` when using a string name for the dtype leads to an unhelpful error. Instead, we should just support this. This adds support for 'primitive' dtype specifications -- there's no defined behavior for how to specify nested nullable types with a single keyword argument, but this should cover most cases. ## Issues closed xref #10243 but doesn't close it
1 parent 69b848a commit 4583dee

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

ibis/expr/datatypes/core.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def dtype(value: Any, nullable: bool = True) -> DataType:
7373

7474

7575
@dtype.register(str)
76-
def from_string(value):
77-
return DataType.from_string(value)
76+
def from_string(value, nullable: bool = True):
77+
return DataType.from_string(value, nullable)
7878

7979

8080
@dtype.register("numpy.dtype")
@@ -165,14 +165,18 @@ def castable(self, to, **kwargs) -> bool:
165165
return castable(self, to, **kwargs)
166166

167167
@classmethod
168-
def from_string(cls, value) -> Self:
168+
def from_string(cls, value, nullable: bool = True) -> Self:
169169
from ibis.expr.datatypes.parse import parse
170170

171171
try:
172-
return parse(value)
172+
typ = parse(value)
173173
except SyntaxError:
174174
raise TypeError(f"{value!r} cannot be parsed as a datatype")
175175

176+
if not nullable:
177+
return typ.copy(nullable=nullable)
178+
return typ
179+
176180
@classmethod
177181
def from_typehint(cls, typ, nullable=True) -> Self:
178182
origin_type = get_origin(typ)

ibis/expr/datatypes/tests/test_parse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ibis.common.annotations import ValidationError
1414

1515

16+
@pytest.mark.parametrize("nullable", [True, False])
1617
@pytest.mark.parametrize(
1718
("spec", "expected"),
1819
[
@@ -43,8 +44,8 @@
4344
("multipolygon", dt.multipolygon),
4445
],
4546
)
46-
def test_primitive_from_string(spec, expected):
47-
assert dt.dtype(spec) == expected
47+
def test_primitive_from_string(nullable, spec, expected):
48+
assert dt.dtype(spec, nullable=nullable) == expected(nullable=nullable)
4849

4950

5051
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)