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

fix(datatypes): document "!" as meaning non-nullable, enable nullable arg for type hints #10893

Merged
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
1 change: 1 addition & 0 deletions ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def schema(
>>> from ibis import schema, Schema
>>> sc = schema([("foo", "string"), ("bar", "int64"), ("baz", "boolean")])
>>> sc = schema(names=["foo", "bar", "baz"], types=["string", "int64", "boolean"])
>>> sc = schema({"nullable-str": "string", "non-nullable-str": "!string"})
>>> sc = schema(dict(foo="string"))
>>> sc = schema(Schema(dict(foo="string"))) # no-op

Expand Down
9 changes: 6 additions & 3 deletions ibis/expr/datatypes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ def dtype(value: Any, nullable: bool = True) -> DataType:
pyarrow types.
nullable
Whether the type should be nullable. Defaults to True.
If `value` is a string prefixed by "!", the type is always non-nullable.

Examples
--------
>>> import ibis
>>> ibis.dtype("int32")
Int32(nullable=True)
>>> ibis.dtype("!int32")
Int32(nullable=False)
>>> ibis.dtype("array<float>")
Array(value_type=Float64(nullable=True), length=None, nullable=True)

DataType objects may also be created from Python types:

>>> ibis.dtype(int)
Int64(nullable=True)
>>> ibis.dtype(int, nullable=False)
Int64(nullable=False)
>>> ibis.dtype(list[float])
Array(value_type=Float64(nullable=True), length=None, nullable=True)

Expand All @@ -70,7 +73,7 @@ def dtype(value: Any, nullable: bool = True) -> DataType:
if isinstance(value, DataType):
return value
else:
return DataType.from_typehint(value)
return DataType.from_typehint(value, nullable)


@dtype.register(str)
Expand Down
13 changes: 13 additions & 0 deletions ibis/expr/datatypes/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ def test_dtype(spec, expected):
assert dt.dtype(spec) == expected


@pytest.mark.parametrize(
("args", "expected"),
[
((int,), dt.Int64(nullable=True)),
((int, False), dt.Int64(nullable=False)),
(("!int",), dt.Int64(nullable=False)),
(("!int", True), dt.Int64(nullable=False)), # "!" overrides `nullable`
],
)
def test_nullable_dtype(args, expected):
assert dt.dtype(*args) == expected


@pytest.mark.parametrize(
("klass", "expected"),
[
Expand Down
Loading