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: allow syntax errors (#6371)

Dylan Madisetti111b24fb1b83
diff --git a/marimo/_ast/cell_manager.py b/marimo/_ast/cell_manager.py
index 0911d71c8b..ddb2c7c862 100644
--- a/marimo/_ast/cell_manager.py
+++ b/marimo/_ast/cell_manager.py
@@ -228,10 +228,24 @@ class CellManager:
else:
cell_id = self.create_cell_id()
filename = app.filename if app is not None else None
- cell = ir_cell_factory(cell_def, cell_id=cell_id, filename=filename)
cell_config = CellConfig.from_dict(
cell_def.options,
)
+
+ try:
+ cell = ir_cell_factory(
+ cell_def, cell_id=cell_id, filename=filename
+ )
+ except SyntaxError:
+ self.unparsable = True
+ self.register_cell(
+ cell_id=cell_id,
+ code=cell_def.code,
+ config=cell_config,
+ name=cell_def.name,
+ cell=None,
+ )
+ return
cell._cell.configure(cell_config)
self._register_cell(cell, app=app)
diff --git a/tests/_ast/codegen_data/test_syntax_errors.py b/tests/_ast/codegen_data/test_syntax_errors.py
new file mode 100644
index 0000000000..5b5312251e
--- /dev/null
+++ b/tests/_ast/codegen_data/test_syntax_errors.py
@@ -0,0 +1,21 @@
+import marimo
+
+__generated_with = "0.15.5"
+app = marimo.App(width="medium")
+
+
+def global_error():
+ tickers = ["AAPL", "GOOGL"]
+ global tickers
+
+def return_error(tickers):
+ if tickers is not None:
+ return tickers
+ return
+
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/test_parse.py b/tests/_ast/test_parse.py
index 3cece74494..3b0149d1da 100644
--- a/tests/_ast/test_parse.py
+++ b/tests/_ast/test_parse.py
@@ -109,6 +109,20 @@ class TestParser:
assert "statement" in notebook.violations[1].description
assert "run guard" in notebook.violations[2].description
+ @staticmethod
+ def test_parse_syntax_errors() -> None:
+ notebook = parse_notebook(
+ get_filepath("test_syntax_errors").read_text()
+ )
+ assert notebook
+ # Valid currently
+ # TODO: Propagate decorators violations.
+ assert len(notebook.violations) == 0
+ assert [cell.name for cell in notebook.cells] == [
+ "global_error",
+ "return_error",
+ ]
+
@staticmethod
def test_parse_decorator_permutations() -> None:
notebook = parse_notebook(get_filepath("test_decorators").read_text())