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: false positive dataframe-like check (#1603)

Myles Scolnickdbf4ce97bf70
diff --git a/marimo/_plugins/ui/_impl/tables/df_protocol_table.py b/marimo/_plugins/ui/_impl/tables/df_protocol_table.py
index a0de456c0d..651d7ce05a 100644
--- a/marimo/_plugins/ui/_impl/tables/df_protocol_table.py
+++ b/marimo/_plugins/ui/_impl/tables/df_protocol_table.py
@@ -17,7 +17,10 @@ from marimo._plugins.ui._impl.tables.table_manager import (
FieldTypes,
TableManager,
)
-from marimo._plugins.ui._impl.tables.types import DataFrameLike
+from marimo._plugins.ui._impl.tables.types import (
+ DataFrameLike,
+ is_dataframe_like,
+)
if TYPE_CHECKING:
import pyarrow as pa # type: ignore
@@ -29,6 +32,12 @@ class DataFrameProtocolTableManager(TableManager[DataFrameLike]):
def __init__(self, data: DataFrameLike):
self.data = data
self._df = data.__dataframe__()
+
+ if not hasattr(self._df, "num_columns"):
+ raise ValueError(
+ "The DataFrameLike object must have a num_columns method"
+ )
+
self._delegate: Optional[
TableManager[Union[pa.Table, pa.RecordBatch]]
] = None
@@ -54,7 +63,7 @@ class DataFrameProtocolTableManager(TableManager[DataFrameLike]):
@staticmethod
def is_type(value: Any) -> bool:
- return isinstance(value, DataFrameLike)
+ return is_dataframe_like(value)
def select_rows(
self, indices: list[int]
diff --git a/marimo/_plugins/ui/_impl/tables/types.py b/marimo/_plugins/ui/_impl/tables/types.py
index 787d089498..f3c97b07bf 100644
--- a/marimo/_plugins/ui/_impl/tables/types.py
+++ b/marimo/_plugins/ui/_impl/tables/types.py
@@ -11,3 +11,15 @@ class DataFrameLike(Protocol):
def __dataframe__(
self, nan_as_null: bool = False, allow_copy: bool = True
) -> DataFrame: ...
+
+
+def is_dataframe_like(value: object) -> bool:
+ return (
+ isinstance(value, DataFrameLike)
+ and hasattr(value, "__dataframe__")
+ and callable(value.__dataframe__)
+ # By checking these are equal, we likely guarding against
+ # __getattr__ implementations that may return different callables
+ and value.__dataframe__ == value.__dataframe__
+ # We don't want to call __dataframe__, in case it has side effects
+ )
diff --git a/marimo/_plugins/ui/_impl/tables/utils.py b/marimo/_plugins/ui/_impl/tables/utils.py
index 58f9f58237..279837a9ea 100644
--- a/marimo/_plugins/ui/_impl/tables/utils.py
+++ b/marimo/_plugins/ui/_impl/tables/utils.py
@@ -21,7 +21,9 @@ from marimo._plugins.ui._impl.tables.table_manager import (
TableManager,
TableManagerFactory,
)
-from marimo._plugins.ui._impl.tables.types import DataFrameLike
+from marimo._plugins.ui._impl.tables.types import (
+ is_dataframe_like,
+)
MANAGERS: List[TableManagerFactory] = [
PandasTableManagerFactory(),
@@ -46,7 +48,10 @@ def get_table_manager_or_none(data: Any) -> TableManager[Any] | None:
return manager(data)
# If we have a DataFrameLike object, use the DataFrameProtocolTableManager
- if isinstance(data, DataFrameLike):
- return DataFrameProtocolTableManager(data)
+ if is_dataframe_like(data):
+ try:
+ return DataFrameProtocolTableManager(data)
+ except Exception:
+ return None
return None
diff --git a/marimo/_smoke_tests/bugs/1602.py b/marimo/_smoke_tests/bugs/1602.py
new file mode 100644
index 0000000000..ac614c7123
--- /dev/null
+++ b/marimo/_smoke_tests/bugs/1602.py
@@ -0,0 +1,28 @@
+import marimo
+
+__generated_with = "0.6.17"
+app = marimo.App(width="medium")
+
+
+def __():
+ class Boom:
+ def __getattr__(self, _):
+ return ...
+ return Boom,
+
+
+def __(Boom):
+ b = Boom()
+ return b,
+
+
+def __(b):
+ callable(b.__dataframe__)
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_plugins/ui/_impl/tables/test_table_utils.py b/tests/_plugins/ui/_impl/tables/test_table_utils.py
new file mode 100644
index 0000000000..696413702e
--- /dev/null
+++ b/tests/_plugins/ui/_impl/tables/test_table_utils.py
@@ -0,0 +1,41 @@
+from __future__ import annotations
+
+import pytest
+
+from marimo._plugins.ui._impl.tables.df_protocol_table import (
+ DataFrameProtocolTableManager,
+)
+from marimo._plugins.ui._impl.tables.types import is_dataframe_like
+
+
+class Fake:
+ def __getattr__(self, _):
+ return ...
+
+
+class FakeReturningCallable:
+ def __getattr__(self, _):
+ return lambda: ...
+
+
+class Real:
+ def __dataframe__(self):
+ return ...
+
+
+def test_is_dataframe_like():
+ # Primitives
+ assert is_dataframe_like(1) is False
+ assert is_dataframe_like("1") is False
+ assert is_dataframe_like([]) is False
+ assert is_dataframe_like({}) is False
+ assert is_dataframe_like(()) is False
+
+ assert is_dataframe_like(Fake()) is False
+ assert is_dataframe_like(FakeReturningCallable()) is False
+ assert is_dataframe_like(Real()) is True
+
+
+def test_df_protocol_manager_throws():
+ with pytest.raises(ValueError):
+ DataFrameProtocolTableManager(Real())