Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: failing cache test on python 3.10 (#6863)

Dylan Madisetti7fa78212df0c
diff --git a/marimo/_save/cache.py b/marimo/_save/cache.py
index 53bd403d8a..619024716a 100644
--- a/marimo/_save/cache.py
+++ b/marimo/_save/cache.py
@@ -1,6 +1,7 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
+import abc
import inspect
import re
from collections import namedtuple
@@ -393,7 +394,7 @@ class Cache:
)
-class CacheContext:
+class CacheContext(abc.ABC):
"""Tracks cache loader state and statistics.
Base class for cache interfaces."""
@@ -454,3 +455,16 @@ class CacheContext:
if self._loader is None:
return 0.0
return self.loader.time_saved
+
+ @property
+ @abc.abstractmethod
+ def last_hash(self) -> Optional[str]:
+ """Last computed cache hash, if available."""
+
+ def __repr__(self) -> str:
+ return (
+ f"<{self.__class__.__name__} hits={self.hits} "
+ f"misses={self.misses} maxsize={self.maxsize} "
+ f"currsize={self.currsize} time_saved={self.time_saved:.4f}s "
+ f"last_hash={self.last_hash}>"
+ )
diff --git a/marimo/_save/save.py b/marimo/_save/save.py
index bff421435b..a7fd31ffc7 100644
--- a/marimo/_save/save.py
+++ b/marimo/_save/save.py
@@ -342,6 +342,11 @@ class _cache_call(CacheContext):
return "<cache>"
return self.__wrapped__.__name__
+ @property
+ def last_hash(self) -> Optional[str]:
+ """Return the last computed hash for this cache call."""
+ return self._last_hash
+
def __get__(
self, instance: Any, _owner: Optional[type] = None
) -> _cache_call:
@@ -565,7 +570,10 @@ class _cache_context(SkipContext, CacheContext):
self.pin_modules = pin_modules
self.hash_type = hash_type
# Wrap loader in State to match CacheContext's _loader type
- self._loader = State(loader, _name=name)
+ if isinstance(loader, MemoryLoader):
+ self._loader = loader.partial().create_or_reconfigure(name)
+ else:
+ self._loader = State(loader, _name=name)
self._start_time: float = 0.0
@property
@@ -731,6 +739,13 @@ class _cache_context(SkipContext, CacheContext):
return False
+ @property
+ def last_hash(self) -> Optional[str]:
+ """Return the last computed hash for this cache context."""
+ if self._cache is None:
+ return None
+ return self._cache.hash
+
# A note on overloading:
# Single dispatch cues off only the first argument, and expects a similar
diff --git a/tests/_save/external_decorators/app.py b/tests/_save/external_decorators/app.py
index 7f07522fb0..6728f744f2 100644
--- a/tests/_save/external_decorators/app.py
+++ b/tests/_save/external_decorators/app.py
@@ -19,11 +19,11 @@ def decorator_wrap():
@app.cell
def block_wrap(mo):
- with mo.cache("random"):
+ with mo.cache("random") as cache_block:
x = []
a = "need a final line to trigger invalid block capture"
- return (x,)
+ return (x, cache_block)
if __name__ == "__main__":