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: f-string parsing in some sql cases (#2579)
Myles Scolnick36b4839eb0c3
diff --git a/marimo/_ast/sql_visitor.py b/marimo/_ast/sql_visitor.pyindex b7a9a5bbb2..e97d4d63cb 100644--- a/marimo/_ast/sql_visitor.py+++ b/marimo/_ast/sql_visitor.py@@ -63,8 +63,8 @@ def normalize_sql_f_string(node: ast.JoinedStr) -> str:elif isinstance(part, ast.Constant):return str(part.s)else:- # Just add '_' as a placeholder for {...} expressions- return "'_'"+ # Just add null as a placeholder for {...} expressions+ return "null"result = "".join(print_part(part) for part in node.values)# remove any double '' created by the f-stringdiff --git a/tests/_ast/test_sql_visitor.py b/tests/_ast/test_sql_visitor.pyindex 58f321b334..66ba0c6e59 100644--- a/tests/_ast/test_sql_visitor.py+++ b/tests/_ast/test_sql_visitor.py@@ -38,7 +38,7 @@ def test_execute_with_f_string() -> None:tree = ast.parse(source_code)visitor = SQLVisitor()visitor.visit(tree)- assert visitor.get_sqls() == ["SELECT * FROM users WHERE name = '_'"]+ assert visitor.get_sqls() == ["SELECT * FROM users WHERE name = null"]def test_no_sql_calls() -> None:diff --git a/tests/_ast/test_visitor.py b/tests/_ast/test_visitor.pyindex 08296dc900..eb986b5c74 100644--- a/tests/_ast/test_visitor.py+++ b/tests/_ast/test_visitor.py@@ -1017,17 +1017,19 @@ def test_print_f_string() -> None:import astjoined_str = ast.parse("f'select * from cars where name = {name}'")+ assert isinstance(joined_str.body[0].value, ast.JoinedStr) # type: ignoreassert (normalize_sql_f_string(joined_str.body[0].value) # type: ignore- == "select * from cars where name = '_'"+ == "select * from cars where name = null")joined_str = ast.parse("f'select * from \\'{table}\\' where name = {name}'")+ assert isinstance(joined_str.body[0].value, ast.JoinedStr) # type: ignoreassert (normalize_sql_f_string(joined_str.body[0].value) # type: ignore- == "select * from '_' where name = '_'"+ == "select * from 'null' where name = null")@@ -1125,7 +1127,6 @@ def test_sql_attach() -> None:assert v.refs == set(["mo"])[email protected](reason="f-string interpolation breaks SQL parsing")@pytest.mark.skipif(not HAS_DEPS, reason="Requires duckdb")def test_sql_attach_f_string() -> None:code = "\n".join(@@ -1137,15 +1138,44 @@ def test_sql_attach_f_string() -> None:mod = ast.parse(code)v.visit(mod)assert v.defs == set(["db"])- assert v.refs == set(["mo"])+ assert v.refs == set(["mo", "PASSWORD"])[email protected](reason="f-string interpolation breaks SQL parsing")@pytest.mark.skipif(not HAS_DEPS, reason="Requires duckdb")-def test_sql_ref_f_string() -> None:+def test_sql_int_f_string() -> None:code = "\n".join(["mo.sql(f'SELECT * FROM df LIMIT {lim}')"])v = visitor.ScopedVisitor()mod = ast.parse(code)v.visit(mod)assert not v.defsassert v.refs == set(["mo", "df", "lim"])++[email protected](not HAS_DEPS, reason="Requires duckdb")+def test_sql_column_f_string() -> None:+ code = "\n".join(["mo.sql(f'SELECT {col} FROM df LIMIT {lim}')"])+ v = visitor.ScopedVisitor()+ mod = ast.parse(code)+ v.visit(mod)+ assert not v.defs+ assert v.refs == set(["mo", "df", "lim", "col"])++[email protected](not HAS_DEPS, reason="Requires duckdb")+def test_sql_value_f_string() -> None:+ code = "\n".join(["mo.sql(f'SELECT * FROM df WHERE {col} = {val}')"])+ v = visitor.ScopedVisitor()+ mod = ast.parse(code)+ v.visit(mod)+ assert not v.defs+ assert v.refs == set(["mo", "df", "col", "val"])++[email protected](not HAS_DEPS, reason="Requires duckdb")+def test_sql_table_f_string() -> None:+ code = "\n".join(["mo.sql(f'SELECT * FROM {my_table} LIMIT {lim}')"])+ v = visitor.ScopedVisitor()+ mod = ast.parse(code)+ v.visit(mod)+ assert not v.defs+ assert v.refs == set(["mo", "my_table", "lim"])