Repository: TileDB-Inc/TileDB-Py
Pull request #2140 (2025-01-10 23:32:15)

Add specific error messages based on the mode the Array is opened
[sc-61651]


Patch:
diff --git a/tiledb/dense_array.py b/tiledb/dense_array.py
index 17224809..b9050f31 100644
--- a/tiledb/dense_array.py
+++ b/tiledb/dense_array.py
@@ -165,8 +165,19 @@ class DenseArrayImpl(Array):
         ...                {"a1": np.zeros(5)})
 
         """
-        if not self.isopen or self.mode != "r":
-            raise tiledb.TileDBError("DenseArray must be opened in read mode")
+        if not self.isopen:
+            raise tiledb.TileDBError("Array is not opened")
+
+        if self.mode == "w":
+            raise tiledb.TileDBError(
+                "Write mode is not supported for queries on Dense Arrays"
+            )
+        elif self.mode == "d":
+            raise tiledb.TileDBError(
+                "Delete mode is not supported for queries on Dense Arrays"
+            )
+        elif self.mode != "r":
+            raise tiledb.TileDBError("Invalid mode for queries on Dense Arrays")
 
         return Query(
             self,
@@ -215,8 +226,19 @@ class DenseArrayImpl(Array):
         ...                 OrderedDict({'a1': np.zeros(5)}))
 
         """
-        if not self.isopen or self.mode != "r":
-            raise tiledb.TileDBError("DenseArray must be opened in read mode")
+        if not self.isopen:
+            raise tiledb.TileDBError("Array is not opened")
+
+        if self.mode == "w":
+            raise tiledb.TileDBError(
+                "Write mode is not supported for subarray queries on Dense Arrays"
+            )
+        elif self.mode == "d":
+            raise tiledb.TileDBError(
+                "Delete mode is not supported for subarray queries on Dense Arrays"
+            )
+        elif self.mode != "r":
+            raise tiledb.TileDBError("Invalid mode for subarray query on Dense Array")
 
         layout = lt.LayoutType.UNORDERED
         if order is None or order == "C":
diff --git a/tiledb/sparse_array.py b/tiledb/sparse_array.py
index 255dc2e7..a44c5ba8 100644
--- a/tiledb/sparse_array.py
+++ b/tiledb/sparse_array.py
@@ -372,10 +372,15 @@ class SparseArrayImpl(Array):
         ...                    OrderedDict({'a1': np.array([1, 2])}))
 
         """
-        if not self.isopen or self.mode not in ("r", "d"):
+        if not self.isopen:
+            raise tiledb.TileDBError("Array is not opened")
+
+        if self.mode == "w":
             raise tiledb.TileDBError(
-                "SparseArray must be opened in read or delete mode"
+                "Write mode is not supported for queries on Sparse Arrays"
             )
+        elif self.mode not in ("r", "d"):
+            raise tiledb.TileDBError("Invalid mode for queries on Sparse Arrays")
 
         # backwards compatibility
         _coords = coords
@@ -484,8 +489,15 @@ class SparseArrayImpl(Array):
         """
         from .subarray import Subarray
 
-        if not self.isopen or self.mode not in ("r", "d"):
-            raise tiledb.TileDBError("SparseArray is not opened in read or delete mode")
+        if not self.isopen:
+            raise tiledb.TileDBError("Array is not opened")
+
+        if self.mode == "w":
+            raise tiledb.TileDBError(
+                "Write mode is not supported for subarray queries on Sparse Arrays"
+            )
+        elif self.mode not in ("r", "d"):
+            raise tiledb.TileDBError("Invalid mode for subarray query on Sparse Array")
 
         layout = lt.LayoutType.UNORDERED
         if order is None or order == "U":


Test patch:
diff --git a/tiledb/tests/test_query_condition.py b/tiledb/tests/test_query_condition.py
index a3c3f157..6a4fa000 100644
--- a/tiledb/tests/test_query_condition.py
+++ b/tiledb/tests/test_query_condition.py
@@ -1022,7 +1022,7 @@ class QueryDeleteTest(DiskTestCase):
 
             with pytest.raises(
                 tiledb.TileDBError,
-                match="SparseArray must be opened in read or delete mode",
+                match="Write mode is not supported for queries on Sparse Arrays",
             ):
                 A.query(cond=qc).submit()
 
@@ -1052,7 +1052,7 @@ class QueryDeleteTest(DiskTestCase):
         with tiledb.open(path, "d") as A:
             with pytest.raises(
                 tiledb.TileDBError,
-                match="DenseArray must be opened in read mode",
+                match="Delete mode is not supported for queries on Dense Arrays",
             ):
                 A.query()