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: prevent SQL cells from auto-converting when mo.sql() is in conditional expressions (#7390)

Myles Scolnick47d29a7b3662
diff --git a/.github/workflows/test_fe.yaml b/.github/workflows/test_fe.yaml
index 5c147237d1..8570f7fa12 100644
--- a/.github/workflows/test_fe.yaml
+++ b/.github/workflows/test_fe.yaml
@@ -35,7 +35,6 @@ jobs:
timeout-minutes: 10
defaults:
run:
- working-directory: ./frontend
shell: bash
steps:
@@ -84,7 +83,7 @@ jobs:
run: pnpm turbo typecheck
- name: 🧪 Test
- run: pnpm test
+ run: pnpm turbo test
build_frontend:
needs: changes
@@ -128,8 +127,8 @@ jobs:
- name: 📦 Build islands frontend
env:
NODE_ENV: production
- VITE_MARIMO_ISLANDS: 'true'
- VITE_MARIMO_VERSION: '0.0.0'
+ VITE_MARIMO_ISLANDS: "true"
+ VITE_MARIMO_VERSION: "0.0.0"
run: |
npm version 0.0.0 --no-git-tag-version
pnpm turbo build:islands
diff --git a/packages/smart-cells/src/__tests__/markdown-parser.test.ts b/packages/smart-cells/src/__tests__/markdown-parser.test.ts
index 0af4807338..a637f07e36 100644
--- a/packages/smart-cells/src/__tests__/markdown-parser.test.ts
+++ b/packages/smart-cells/src/__tests__/markdown-parser.test.ts
@@ -109,8 +109,8 @@ Count: {str(data["items"][0]["count"])}`.trim(),
const { code: pythonCode, offset } = parser.transformOut(code, {
quotePrefix: "",
});
- expect(pythonCode).toBe(`mo.md("""Hello world""")`);
- expect(offset).toBe(9);
+ expect(pythonCode).toBe(`mo.md("""\nHello world\n""")`);
+ expect(offset).toBe(11);
});
it("should wrap multiline markdown", () => {
@@ -134,7 +134,7 @@ Count: {str(data["items"][0]["count"])}`.trim(),
quotePrefix: "r",
});
expect(pythonCode).toBe(
- `mo.md(r"""$\\nu = \\mathllap{}\\cdot\\mathllap{\\alpha}$""")`,
+ `mo.md(r"""\n$\\nu = \\mathllap{}\\cdot\\mathllap{\\alpha}$\n""")`,
);
});
@@ -157,7 +157,7 @@ Count: {str(data["items"][0]["count"])}`.trim(),
quotePrefix: "",
});
expect(pythonCode).toBe(
- `mo.md("""Markdown with an escaped \\"""quote\\"""!!""")`,
+ `mo.md("""\nMarkdown with an escaped "\\""quote"\\""!!\n""")`,
);
});
@@ -165,7 +165,7 @@ Count: {str(data["items"][0]["count"])}`.trim(),
const { code: pythonCode } = parser.transformOut("", {
quotePrefix: "r",
});
- expect(pythonCode).toBe(`mo.md(r""" """)`);
+ expect(pythonCode).toBe(`mo.md(r"""\n \n""")`);
});
});
diff --git a/packages/smart-cells/src/__tests__/sql-parser.test.ts b/packages/smart-cells/src/__tests__/sql-parser.test.ts
index 009d5aa8f9..41f85c7e17 100644
--- a/packages/smart-cells/src/__tests__/sql-parser.test.ts
+++ b/packages/smart-cells/src/__tests__/sql-parser.test.ts
@@ -262,6 +262,13 @@ WHERE id IN ({str(data["items"][0]["id"])})`.trim(),
'df := mo.sql("")', // Wrong assignment operator
// Multiple SQL calls
'df = mo.sql("""SELECT 1""")\ndf2 = mo.sql("""SELECT 2""")',
+ // Conditional expressions - issue #7386
+ 'df = mo.sql("""SELECT 1""") if condition else None',
+ 'df = mo.sql(f"""SELECT * FROM table""") if x.value else None',
+ 'test_df = (mo.sql(f"""SELECT * FROM table""", output=False) if true_false_widget.value else None)',
+ // Binary operations
+ 'df = mo.sql("""SELECT 1""") or default_value',
+ 'df = mo.sql("""SELECT 1""") and process_it()',
];
for (const pythonCode of invalidCases) {
@@ -272,6 +279,18 @@ WHERE id IN ({str(data["items"][0]["id"])})`.trim(),
it("should return true for empty string", () => {
expect(parser.isSupported("")).toBe(true);
});
+
+ it("should not support mo.sql() wrapped in parentheses", () => {
+ // We could change this in the future if needed
+ const shouldBeSupportedCases = [
+ 'df = (mo.sql("""SELECT 1"""))',
+ 'df = (\n mo.sql("""SELECT 1""")\n)',
+ ];
+
+ for (const pythonCode of shouldBeSupportedCases) {
+ expect(parser.isSupported(pythonCode)).toBe(true);
+ }
+ });
});
describe("roundtrip", () => {
diff --git a/packages/smart-cells/src/parsers/sql-parser.ts b/packages/smart-cells/src/parsers/sql-parser.ts
index d331eac947..9ff6ac0927 100644
--- a/packages/smart-cells/src/parsers/sql-parser.ts
+++ b/packages/smart-cells/src/parsers/sql-parser.ts
@@ -209,23 +209,63 @@ function parseSQLStatement(code: string): SQLParseInfo | null {
}
// Move to the expression part (after the =)
- while (assignCursor.next()) {
- if (assignCursor.name === "CallExpression") {
- // Check if it's mo.sql call
- const callCursor = assignCursor.node.cursor();
- let isMoSql = false;
-
- callCursor.firstChild(); // Move to first child of call
- if (callCursor.name === "MemberExpression") {
- const memberText = code.slice(callCursor.from, callCursor.to);
- isMoSql = memberText === "mo.sql";
- }
+ // We need to find the value being assigned (after AssignOp)
+ let foundAssignOp = false;
+ let rightHandSide: SyntaxNode | null = null;
+
+ while (assignCursor.nextSibling()) {
+ if (assignCursor.name === "AssignOp") {
+ foundAssignOp = true;
+ } else if (foundAssignOp && !rightHandSide) {
+ // This is the right-hand side of the assignment
+ rightHandSide = assignCursor.node;
+ break;
+ }
+ }
+
+ if (!rightHandSide) {
+ return null;
+ }
+
+ // Check if the right-hand side is wrapped in a conditional or binary expression
+ // These should NOT be treated as SQL cells (issue #7386)
+ if (
+ rightHandSide.name === "ConditionalExpression" ||
+ rightHandSide.name === "BinaryExpression" ||
+ rightHandSide.name === "UnaryExpression"
+ ) {
+ return null;
+ }
- if (!isMoSql) {
- // Skip non-mo.sql calls (e.g., embedded expressions in f-strings)
- continue;
+ // Now look for the CallExpression - it should be the right-hand side or direct child
+ const rhsCursor = rightHandSide.cursor();
+ let callExprNode: SyntaxNode | null = null;
+
+ if (rhsCursor.name === "CallExpression") {
+ callExprNode = rhsCursor.node;
+ } else {
+ // Move to first child to handle potential wrapping
+ rhsCursor.firstChild();
+ do {
+ if (rhsCursor.name === "CallExpression") {
+ callExprNode = rhsCursor.node;
+ break;
}
+ } while (rhsCursor.nextSibling());
+ }
+
+ if (callExprNode) {
+ // Check if it's mo.sql call
+ const callCursor = callExprNode.cursor();
+ let isMoSql = false;
+ callCursor.firstChild(); // Move to first child of call
+ if (callCursor.name === "MemberExpression") {
+ const memberText = code.slice(callCursor.from, callCursor.to);
+ isMoSql = memberText === "mo.sql";
+ }
+
+ if (isMoSql) {
// Move to arguments
while (callCursor.next()) {
if (callCursor.name === "ArgList") {
@@ -261,10 +301,6 @@ function parseSQLStatement(code: string): SQLParseInfo | null {
break;
}
}
-
- // Break after processing the mo.sql call to avoid processing
- // embedded expressions in f-strings
- break;
}
}