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: allow custom pickler failure to fall back to default (#9070)

Dylan Madisetti04d47c93cfdb
diff --git a/marimo/_save/encode.py b/marimo/_save/encode.py
index 4fa9233b56..d109a13f54 100644
--- a/marimo/_save/encode.py
+++ b/marimo/_save/encode.py
@@ -161,10 +161,13 @@ def deterministic_dumps(obj: Any, hash_type: str) -> bytes:
def reducer_override(self, obj: Any) -> Any:
if stub := maybe_get_custom_stub(obj):
return (bytes, (stub.to_bytes(),))
- if not is_primitive(obj) and is_data_primitive(obj):
- h = hashlib.new(hash_type, usedforsecurity=False)
- h.update(_contiguous_tensor_bytes(obj))
- return (bytes, (h.digest(),))
+ try:
+ if not is_primitive(obj) and is_data_primitive(obj):
+ h = hashlib.new(hash_type, usedforsecurity=False)
+ h.update(_contiguous_tensor_bytes(obj))
+ return (bytes, (h.digest(),))
+ except (TypeError, ValueError):
+ pass
return NotImplemented
buf = io.BytesIO()
diff --git a/marimo/_save/hash.py b/marimo/_save/hash.py
index a5a8c26f13..ca87b47906 100644
--- a/marimo/_save/hash.py
+++ b/marimo/_save/hash.py
@@ -513,9 +513,7 @@ class BlockHasher:
if failed:
# Ruff didn't like a lambda here
def get_type(ref: Name) -> str:
- return (
- str(type(item)) if (item := scope[ref]) else "missing"
- )
+ return str(type(scope[ref])) if ref in scope else "missing"
ref_list = ", ".join(
[
diff --git a/tests/_save/test_cache.py b/tests/_save/test_cache.py
index 018c3bee08..f92e9c8963 100644
--- a/tests/_save/test_cache.py
+++ b/tests/_save/test_cache.py
@@ -11,6 +11,7 @@ import pytest
import marimo
from marimo._ast.app import App
+from marimo._dependencies.dependencies import DependencyManager
from marimo._plugins.ui._impl.input import dropdown
from marimo._runtime.commands import ExecuteCellCommand
from marimo._runtime.runtime import Kernel
@@ -2552,6 +2553,44 @@ class TestCacheDecorator:
assert future.result() == 5
return
+ @pytest.mark.skipif(
+ not DependencyManager.pandas.has(),
+ reason="pandas not installed",
+ )
+ async def test_cache_dataframe_object_column(
+ self, k: Kernel, exec_req: ExecReqProvider
+ ) -> None:
+ """Regression test for marimo-team/marimo#9068.
+
+ @cache with a DataFrame containing an object-dtype column previously
+ raised: ValueError: The truth value of a DataFrame is ambiguous.
+ Caused by an unsafe truthiness check in get_type() that called
+ bool() on the scope value instead of checking key presence.
+ """
+ await k.run(
+ [
+ exec_req.get(
+ """
+ import pandas as pd
+ from marimo._save.save import cache
+
+ @cache
+ def get_length(df):
+ return len(df)
+
+ result = get_length(
+ pd.DataFrame(
+ {"a": list(range(100)), "b": list(map(str, range(100)))}
+ )
+ )
+ """
+ ),
+ ]
+ )
+
+ assert not k.stderr.messages
+ assert k.globals["result"] == 100
+
class TestPersistentCache:
async def test_pickle_context(