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: ignore sql_refs for top level determination (#8673)
Dylan Madisetti6c6a86b63d85
diff --git a/marimo/_ast/toplevel.py b/marimo/_ast/toplevel.pyindex 6ee25f3bc1..c6c8801d98 100644--- a/marimo/_ast/toplevel.py+++ b/marimo/_ast/toplevel.py@@ -167,7 +167,12 @@ class TopLevelStatus:self.demote(HINT_HAS_REFS.format(self.dependencies))return- dependent_refs = self._cell.refs - (allowed_refs | toplevel)+ # SQL refs are runtime dependencies, not definition-time dependencies.+ # Exclude them from top-level resolution so SQL-using functions are not+ # incorrectly demoted. General ref tracking in visitor.py is unchanged.+ dependent_refs = (self._cell.refs - self._cell.sql_refs.keys()) - (+ allowed_refs | toplevel+ )if not dependent_refs:self.type = TopLevelType.TOPLEVELself.hint = HINT_VALIDdiff --git a/tests/_ast/test_toplevel.py b/tests/_ast/test_toplevel.pyindex 4b8098e19f..2544e0d85f 100644--- a/tests/_ast/test_toplevel.py+++ b/tests/_ast/test_toplevel.py@@ -14,6 +14,9 @@ from marimo._ast.toplevel import (TopLevelType,)from marimo._ast.variables import BUILTINS+from marimo._dependencies.dependencies import DependencyManager++HAS_DUCKDB = DependencyManager.duckdb.has()class TestTopLevelStatus:@@ -633,3 +636,79 @@ class TestTopLevelHook:status = TopLevelStatus.from_cell(cell._cell, BUILTINS)assert status.type == TopLevelType.TOPLEVEL+++class TestTopLevelSQL:+ @staticmethod+ @pytest.mark.skipif(not HAS_DUCKDB, reason="Requires duckdb")+ def test_sql_self_contained_two_calls(app) -> None:+ """Two separate duckdb.sql() calls (create then use) — should be TOPLEVEL."""++ @app.function+ def create_and_query():+ import duckdb # noqa: F401++ duckdb.sql("CREATE TABLE t AS SELECT 1 AS id")+ return duckdb.sql("SELECT * FROM t").df()++ extraction = TopLevelExtraction.from_app(InternalApp(app))+ assert [TopLevelType.TOPLEVEL] == [s.type for s in extraction], [+ s.hint for s in extraction+ ]++ @staticmethod+ @pytest.mark.skipif(not HAS_DUCKDB, reason="Requires duckdb")+ def test_sql_self_contained_single_call(app) -> None:+ """Single duckdb.sql() with multiple statements (create then use) — should be TOPLEVEL."""++ @app.function+ def create_and_query():+ import duckdb # noqa: F401++ return duckdb.sql(+ "CREATE TABLE t AS SELECT 1 AS id; SELECT * FROM t"+ ).df()++ extraction = TopLevelExtraction.from_app(InternalApp(app))+ assert [TopLevelType.TOPLEVEL] == [s.type for s in extraction], [+ s.hint for s in extraction+ ]++ @staticmethod+ @pytest.mark.skipif(not HAS_DUCKDB, reason="Requires duckdb")+ def test_sql_attach_detach_self_contained(app) -> None:+ """ATTACH/DETACH pattern — db alias defined in same function — should be TOPLEVEL."""++ @app.function+ def attach_and_query():+ import duckdb # noqa: F401++ duckdb.sql(+ "DETACH DATABASE IF EXISTS mydb; ATTACH DATABASE 'mydb.duckdb' AS mydb"+ )+ return duckdb.sql("SELECT * FROM mydb.users").df()++ extraction = TopLevelExtraction.from_app(InternalApp(app))+ assert [TopLevelType.TOPLEVEL] == [s.type for s in extraction], [+ s.hint for s in extraction+ ]++ @staticmethod+ @pytest.mark.skipif(not HAS_DUCKDB, reason="Requires duckdb")+ def test_sql_external_refs_toplevel(app) -> None:+ """@app.function referencing external SQL tables — still TOPLEVEL.++ SQL table references are runtime dependencies, not definition-time+ dependencies, so they don't prevent a function from being top-level.+ """++ @app.function+ def query_external():+ import duckdb # noqa: F401++ return duckdb.sql("SELECT * FROM some_external_table").df()++ extraction = TopLevelExtraction.from_app(InternalApp(app))+ assert [TopLevelType.TOPLEVEL] == [s.type for s in extraction], [+ s.hint for s in extraction+ ]