Repository: TileDB-Inc/TileDB-Py
Pull request #2172 (2025-03-06 11:58:48)

Fix `stats_reset()` behavior
This PR fixes `stats_reset()`, which was noticed to not reset stats after calling `tiledb.stats_reset()`.

---

[sc-63203]


Patch:
diff --git a/tiledb/stats.py b/tiledb/stats.py
index cbcc7382..e21b87dc 100644
--- a/tiledb/stats.py
+++ b/tiledb/stats.py
@@ -20,9 +20,9 @@ def stats_disable():
 
 def stats_reset():
     """Reset all TileDB internal statistics to 0."""
-    from .main import init_stats
+    from .main import reset_stats
 
-    init_stats()
+    reset_stats()
 
 
 def stats_dump(


Test patch:
diff --git a/tiledb/tests/common.py b/tiledb/tests/common.py
index 7ff55b8f..087089b3 100644
--- a/tiledb/tests/common.py
+++ b/tiledb/tests/common.py
@@ -363,7 +363,9 @@ def assert_dict_arrays_equal(d1, d2, ordered=True):
         assert_unordered_equal(array1, array2, True)
 
 
-def assert_captured(cap, expected):
+def assert_captured(cap, val, expected=True):
+    """Assert that the captured output contains
+    or does not contain a value"""
     if sys.platform != "win32":
         import ctypes
 
@@ -372,7 +374,7 @@ def assert_captured(cap, expected):
 
         out, err = cap.readouterr()
         assert not err
-        assert expected in out
+        assert (val in out) == expected
 
 
 @pytest.fixture(scope="module", params=["hilbert", "row-major", "col-major"])
diff --git a/tiledb/tests/test_stats.py b/tiledb/tests/test_stats.py
index b94dac80..6e699ff3 100644
--- a/tiledb/tests/test_stats.py
+++ b/tiledb/tests/test_stats.py
@@ -22,8 +22,25 @@ class StatsTest(DiskTestCase):
         with tiledb.from_numpy(path, np.arange(10)) as T:
             pass
 
-        # basic output check for read stats
+        # check that Writer stats are printed
+        tiledb.stats_dump()
+
+        if tiledb.libtiledb.version() >= (2, 27):
+            assert_captured(capfd, "Context.Query.Writer")
+        else:
+            assert_captured(capfd, "Context.StorageManager.Query.Writer")
+
+        # check that Writer stats are not printed because of reset
         tiledb.stats_reset()
+        tiledb.stats_dump()
+
+        if tiledb.libtiledb.version() >= (2, 27):
+            assert_captured(capfd, "Context.Query.Writer", expected=False)
+        else:
+            assert_captured(
+                capfd, "Context.StorageManager.Query.Writer", expected=False
+            )
+
         with tiledb.open(path) as T:
             tiledb.stats_enable()
             assert_array_equal(T, np.arange(10))
@@ -32,6 +49,13 @@ class StatsTest(DiskTestCase):
             tiledb.stats_dump()
             assert_captured(capfd, "TileDB Embedded Version:")
 
+            # check that Reader stats are printed
+            tiledb.stats_dump()
+            if tiledb.libtiledb.version() >= (2, 27):
+                assert_captured(capfd, "Context.Query.Reader")
+            else:
+                assert_captured(capfd, "Context.StorageManager.Query.Reader")
+
             # test string version
             stats_v = tiledb.stats_dump(print_out=False)
             if tiledb.libtiledb.version() < (2, 3):
@@ -51,6 +75,16 @@ class StatsTest(DiskTestCase):
             else:
                 self.assertTrue("==== READ ====" in stats_quiet)
 
+            # check that Writer stats are not printed because of reset
+            tiledb.stats_reset()
+            tiledb.stats_dump()
+            if tiledb.libtiledb.version() >= (2, 27):
+                assert_captured(capfd, "Context.Query.Reader", expected=False)
+            else:
+                assert_captured(
+                    capfd, "Context.StorageManager.Query.Reader", expected=False
+                )
+
     def test_stats_include_python_json(self):
         tiledb.stats_enable()