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
fix(sql): reject catch-all __getattr__ objects as DB connections (#10301)
Shahmir Varqhaeddce3b3b2d4
diff --git a/marimo/_sql/engines/adbc.py b/marimo/_sql/engines/adbc.pyindex 7133f6e344..a4a4ccd55c 100644--- a/marimo/_sql/engines/adbc.py+++ b/marimo/_sql/engines/adbc.py@@ -17,6 +17,7 @@ from marimo._sql.engines.types import (InferenceConfig,SQLConnection,default_inference_config,+ fabricates_attributes,)from marimo._sql.utils import convert_to_output, is_cheap_dialectfrom marimo._types.ids import VariableName@@ -461,6 +462,9 @@ class AdbcDBAPIEngine(SQLConnection[AdbcDbApiConnection]):if var_type_name == "ibis.common.deferred.Deferred":return False+ if fabricates_attributes(var):+ return False+try:# First, validate the connection-level surface area to avoid# accidentally classifying regular DB-API connections as ADBC.diff --git a/marimo/_sql/engines/dbapi.py b/marimo/_sql/engines/dbapi.pyindex 3ed9d8ac1a..8073b127c0 100644--- a/marimo/_sql/engines/dbapi.py+++ b/marimo/_sql/engines/dbapi.py@@ -5,7 +5,7 @@ from types import ModuleTypefrom typing import TYPE_CHECKING, Any, Protocolfrom marimo import _loggers-from marimo._sql.engines.types import QueryEngine+from marimo._sql.engines.types import QueryEngine, fabricates_attributesfrom marimo._sql.utils import convert_to_outputLOGGER = _loggers.marimo_logger()@@ -112,6 +112,9 @@ class DBAPIEngine(QueryEngine[DBAPIConnection]):if var_type_name == "ibis.common.deferred.Deferred":return False+ if fabricates_attributes(var):+ return False+try:required_methods = ["cursor", "commit", "rollback", "close"]has_required_methods = all(diff --git a/marimo/_sql/engines/types.py b/marimo/_sql/engines/types.pyindex a2b493f034..2ef09cf7d2 100644--- a/marimo/_sql/engines/types.py+++ b/marimo/_sql/engines/types.py@@ -23,6 +23,29 @@ from marimo._types.ids import VariableNameNO_SCHEMA_NAME = ""+# Probe name that no real DB connection defines.+_MISSING_ATTRIBUTE_PROBE = "_marimo_does_not_exist_"+_MISSING_ATTRIBUTE_SENTINEL = object()+++def fabricates_attributes(var: Any) -> bool:+ """Return True if `var` invents attributes via a catch-all `__getattr__`.++ Used to reject objects (e.g. ignite metrics) that pass every getattr-based+ duck-type check. Prefer this over `getattr_static` so connection proxies+ that forward `__getattr__` still work.++ See https://github.com/marimo-team/marimo/issues/10213.+ """+ try:+ return (+ getattr(var, _MISSING_ATTRIBUTE_PROBE, _MISSING_ATTRIBUTE_SENTINEL)+ is not _MISSING_ATTRIBUTE_SENTINEL+ )+ except Exception:+ # Not a usable connection if __getattr__ raises unexpectedly.+ return True+@dataclassclass InferenceConfig(ABC):diff --git a/tests/_sql/test_adbc.py b/tests/_sql/test_adbc.pyindex 7e6c281239..502b92faeb 100644--- a/tests/_sql/test_adbc.py+++ b/tests/_sql/test_adbc.py@@ -439,6 +439,19 @@ def test_adbc_is_compatible_does_not_create_cursor() -> None:assert conn._cursor.did_close is True # type: ignore[attr-defined]+def test_adbc_is_compatible_rejects_fabricated_attributes() -> None:+ # Catch-all __getattr__ (e.g. ignite metrics) must not match.+ # See https://github.com/marimo-team/marimo/issues/10213.+ class FabricatingAttributes:+ def __getattr__(self, name: str) -> object:+ def _lazy(*_args: object, **_kwargs: object) -> object:+ return FabricatingAttributes()++ return _lazy++ assert AdbcDBAPIEngine.is_compatible(FabricatingAttributes()) is False++def test_adbc_engine_uses_default_inference_config() -> None:"""ADBC shares the default discovery config (see #9775)."""conn = FakeAdbcDbApiConnection(diff --git a/tests/_sql/test_dbapi.py b/tests/_sql/test_dbapi.pyindex 1922b50e15..6fe1e4a638 100644--- a/tests/_sql/test_dbapi.py+++ b/tests/_sql/test_dbapi.py@@ -58,6 +58,62 @@ def test_is_compatible() -> None:assert not isinstance(engine, EngineCatalog)+def test_is_compatible_rejects_fabricated_attributes() -> None:+ # Catch-all __getattr__ (e.g. ignite metrics) must not match.+ class FabricatingAttributes:+ def __getattr__(self, name: str) -> object:+ def _lazy(*_args: object, **_kwargs: object) -> object:+ return FabricatingAttributes()++ return _lazy++ assert not DBAPIEngine.is_compatible(FabricatingAttributes())+++def test_is_compatible_rejects_getattr_returning_none() -> None:+ # Fabricators that return None for unknown names still invent the attr.+ class FabricatesNone:+ def __getattr__(self, name: str) -> None:+ return None++ def cursor(self) -> object:+ return self++ def commit(self) -> None:+ return None++ def rollback(self) -> None:+ return None++ def close(self) -> None:+ return None++ def execute(self, *_args: object, **_kwargs: object) -> None:+ return None++ def fetchall(self) -> list[object]:+ return []++ assert not DBAPIEngine.is_compatible(FabricatesNone())+++def test_is_compatible_accepts_getattr_forwarding_proxy() -> None:+ """Proxies that forward __getattr__ to a real connection still match."""++ class ForwardingProxy:+ def __init__(self, inner: sqlite3.Connection) -> None:+ object.__setattr__(self, "_inner", inner)++ def __getattr__(self, name: str) -> object:+ return getattr(self._inner, name)++ conn = sqlite3.connect(":memory:")+ try:+ assert DBAPIEngine.is_compatible(ForwardingProxy(conn))+ finally:+ conn.close()++def test_execute_native(dbapi_engine: DBAPIEngine) -> None:with patch.object(dbapi_engine, "sql_output_format", return_value="native"diff --git a/tests/_sql/test_get_engines.py b/tests/_sql/test_get_engines.pyindex 80f9515fe1..b202f0f6bb 100644--- a/tests/_sql/test_get_engines.py+++ b/tests/_sql/test_get_engines.py@@ -482,3 +482,17 @@ def test_variables_without_datasource_engine() -> None:variables = [("deferred_for_test", deferred_for_test)]engines = get_engines_from_variables(variables)assert not engines+++def test_fabricated_attributes_not_treated_as_engine() -> None:+ # Catch-all __getattr__ (e.g. ignite metrics) must not match.+ class FabricatingAttributes:+ def __getattr__(self, name: str) -> object:+ def _lazy(*_args: object, **_kwargs: object) -> object:+ return FabricatingAttributes()++ return _lazy++ variables = [(VariableName("metric"), FabricatingAttributes())]+ engines = get_engines_from_variables(variables)+ assert not engines