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

Use safe repr if ArraySchema was not properly constructed #1896

Merged
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
59 changes: 35 additions & 24 deletions tiledb/array_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@
from .domain import Domain
from .filter import Filter, FilterList

_tiledb_order_to_string = {
lt.LayoutType.ROW_MAJOR: "row-major",
lt.LayoutType.COL_MAJOR: "col-major",
lt.LayoutType.GLOBAL_ORDER: "global",
lt.LayoutType.UNORDERED: "unordered",
lt.LayoutType.HILBERT: "hilbert",
}

_string_to_tiledb_order = {v: k for k, v in _tiledb_order_to_string.items()}
_string_to_tiledb_order.update(
{
"C": lt.LayoutType.ROW_MAJOR,
"R": lt.LayoutType.COL_MAJOR,
"H": lt.LayoutType.HILBERT,
"U": lt.LayoutType.UNORDERED,
None: lt.LayoutType.ROW_MAJOR, # default (fixed in SC-27374)
}
)


class ArraySchema(CtxMixin, lt.ArraySchema):
"""
Expand Down Expand Up @@ -68,12 +87,14 @@ def __init__(
)
self._add_attr(att)

self._cell_order = _string_to_tiledb_order.get(cell_order)
if self._cell_order is None:
try:
self._cell_order = _string_to_tiledb_order.get(cell_order)
except (TypeError, ValueError):
raise ValueError(f"unknown tiledb layout: {cell_order}")

self._tile_order = _string_to_tiledb_order.get(tile_order)
if self._tile_order is None:
try:
self._tile_order = _string_to_tiledb_order.get(tile_order)
except (TypeError, ValueError):
raise ValueError(f"unknown tiledb layout: {tile_order}")

if capacity > 0:
Expand Down Expand Up @@ -384,6 +405,16 @@ def dump(self):
print(self._dump(), "\n")

def __repr__(self):
# use safe repr if pybind11 constructor failed or the array schema did
# not construct properly
try:
self._check()
except lt.TileDBError:
return object.__repr__(self)

if self._ctx is None:
return object.__repr__(self)

# TODO support/use __qualname__
output = io.StringIO()
output.write("ArraySchema(\n")
Expand Down Expand Up @@ -454,23 +485,3 @@ def _repr_html_(self):
output.write("</table>")

return output.getvalue()


_tiledb_order_to_string = {
lt.LayoutType.ROW_MAJOR: "row-major",
lt.LayoutType.COL_MAJOR: "col-major",
lt.LayoutType.GLOBAL_ORDER: "global",
lt.LayoutType.UNORDERED: "unordered",
lt.LayoutType.HILBERT: "hilbert",
}

_string_to_tiledb_order = {v: k for k, v in _tiledb_order_to_string.items()}
_string_to_tiledb_order.update(
{
"C": lt.LayoutType.ROW_MAJOR,
"R": lt.LayoutType.COL_MAJOR,
"H": lt.LayoutType.HILBERT,
"U": lt.LayoutType.UNORDERED,
None: lt.LayoutType.ROW_MAJOR, # default (fixed in SC-27374)
}
)
4 changes: 4 additions & 0 deletions tiledb/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ def enum_label(self):
return self._get_enumeration_name(self._ctx)

def __repr__(self):
# use safe repr if pybind11 constructor failed
if self._ctx is None:
return object.__repr__(self)

filters_str = ""
if self.filters:
filters_str = ", filters=FilterList(["
Expand Down
4 changes: 4 additions & 0 deletions tiledb/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def __init__(
self._filters = FilterList(filters)

def __repr__(self) -> str:
# use safe repr if pybind11 constructor failed
if self._ctx is None:
return object.__repr__(self)

filters_str = ""
if self.filters:
filters_str = ", filters=FilterList(["
Expand Down
4 changes: 4 additions & 0 deletions tiledb/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def clone_dim_with_name(dim, name):
self._add_dim(d)

def __repr__(self):
# use safe repr if pybind11 constructor failed
if self._ctx is None:
return object.__repr__(self)

dims = ",\n ".join(repr(self.dim(i)) for i in range(self.ndim))
return "Domain({0!s})".format(dims)

Expand Down
4 changes: 4 additions & 0 deletions tiledb/enumeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def __eq__(self, other):
)

def __repr__(self):
# use safe repr if pybind11 constructor failed
if self._ctx is None:
return object.__repr__(self)

return f"Enumeration(name='{self.name}', cell_val_num={self.cell_val_num}, ordered={self.ordered}, values={list(self.values())})"

def _repr_html_(self):
Expand Down
4 changes: 4 additions & 0 deletions tiledb/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def __init__(self, type: lt.FilterOption, ctx: Optional[Ctx] = None):
super().__init__(ctx, type)

def __repr__(self) -> str:
# use safe repr if pybind11 constructor failed
if self._ctx is None:
return object.__repr__(self)

output = io.StringIO()
output.write(f"{type(self).__name__}(")
if hasattr(self, "_attrs_"):
Expand Down
4 changes: 4 additions & 0 deletions tiledb/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ def __iter__(self):
yield key

def __repr__(self):
# use safe repr if pybind11 constructor failed
if self._ctx is None:
return object.__repr__(self)

return str(dict(self._iter(keys_only=False)))

def setdefault(self, key, default=None):
Expand Down
10 changes: 10 additions & 0 deletions tiledb/tests/test_array_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ def test_dense_array_schema_fp_domain_error(self):
with self.assertRaises(tiledb.TileDBError):
tiledb.ArraySchema(domain=dom, attrs=(att,))

def test_dense_array_schema_invalid_cell_and_tile_order(self):
dom = tiledb.Domain(tiledb.Dim(domain=(1, 8), tile=2, dtype=np.float64))
att = tiledb.Attr("val", dtype=np.float64)

with self.assertRaises(ValueError):
tiledb.ArraySchema(domain=dom, attrs=(att,), cell_order="invalid")

with self.assertRaises(ValueError):
tiledb.ArraySchema(domain=dom, attrs=(att,), tile_order="invalid")

def test_sparse_schema(self):
# create dimensions
d1 = tiledb.Dim("d1", domain=(1, 1000), tile=10, dtype="uint64")
Expand Down
Loading