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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
ci(types): satisfy pandas-stubs 3.0 (#9814)
Shahmir Varqha82043d195c35
diff --git a/marimo/_messaging/msgspec_encoder.py b/marimo/_messaging/msgspec_encoder.pyindex d80e948c1d..b908729f6c 100644--- a/marimo/_messaging/msgspec_encoder.py+++ b/marimo/_messaging/msgspec_encoder.py@@ -109,14 +109,15 @@ def enc_hook(obj: Any) -> Any:if isinstance(obj, pd.Index):return obj.to_list()- # Catch-all for other pandas objects- try:- if isinstance(obj, pd.core.base.PandasObject): # type: ignore+ # Catch-all for other pandas objects. Only some PandasObject+ # subclasses (Series/DataFrame, handled above) define `to_json`, so+ # guard the lookup instead of relying on AttributeError.+ if isinstance(obj, pd.core.base.PandasObject): # type: ignore[attr-defined]+ to_json = getattr(obj, "to_json", None)+ if callable(to_json):import json- return json.loads(obj.to_json(date_format="iso"))- except AttributeError:- pass+ return json.loads(to_json(date_format="iso"))# Handle shapely geometry objects from geopandasif DependencyManager.geopandas.imported():diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.pyindex bb81fc8841..d351eae5fe 100644--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py@@ -418,7 +418,7 @@ class PandasTableManagerFactory(TableManagerFactory):# Restore the original index structurenative_df = native_df.set_index(index_columns)- native_df.index.names = original_names+ native_df.index = native_df.index.set_names(original_names)return PandasTableManager(native_df)result = super().search(query)native_df = nw.to_native(result.data)diff --git a/tests/_messaging/test_enc_hook.py b/tests/_messaging/test_enc_hook.pyindex 4be3a0ec98..8c03f867fb 100644--- a/tests/_messaging/test_enc_hook.py+++ b/tests/_messaging/test_enc_hook.py@@ -104,6 +104,28 @@ def test_object_with_getattr_returning_non_callable() -> None:assert result["base"] == "http://example.com/"+ not DependencyManager.pandas.has(),+ reason="pandas not installed",+)+def test_serialize_pandas_object_without_to_json() -> None:+ """A PandasObject that doesn't define `to_json` must not crash.++ The catch-all branch guards the `to_json` lookup with getattr/callable+ (only Series/DataFrame define it, and those are handled earlier), so a+ GroupBy object should fall through to generic handling instead of raising.+ """+ import pandas as pd++ grouped = pd.DataFrame({"a": [1, 1, 2], "b": [1, 2, 3]}).groupby("a")+ assert isinstance(grouped, pd.core.base.PandasObject)+ assert not callable(getattr(grouped, "to_json", None))++ # Should not raise (regression guard for the getattr-based fallback).+ result = enc_hook(grouped)+ assert result is not None++def test_serialize_decimal() -> None:decimal_obj = decimal.Decimal("123.45")result = enc_hook(decimal_obj)