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: more aggressive static parsing (#6176)

Dylan Madisettiaedf71a7749b
diff --git a/marimo/_ast/parse.py b/marimo/_ast/parse.py
index fb0ad48333..27345de274 100644
--- a/marimo/_ast/parse.py
+++ b/marimo/_ast/parse.py
@@ -45,6 +45,26 @@ V = TypeVar("V")
U = TypeVar("U")
+def fixed_dedent(text: str) -> str:
+ """Manually edited code, can dedent"""
+ # Added robustness for AI generated code
+ lines = text.splitlines()
+ for line in lines:
+ if content := line.lstrip():
+ indent = line[: len(line) - len(content)]
+ break
+ else:
+ # Quit early, no clear leading spaces
+ return dedent(text)
+
+ def refill(line: str) -> str:
+ if not line.startswith(indent):
+ return indent + line
+ return line
+
+ return dedent("\n".join(map(refill, lines)))
+
+
class MarimoFileError(Exception):
pass
@@ -112,7 +132,7 @@ class Extractor:
_none_to_0(node.end_lineno) - 1,
_none_to_0(node.end_col_offset),
)
- return dedent(code)
+ return fixed_dedent(code)
def to_cell_def(self, node: FnNode, kwargs: dict[str, Any]) -> CellDef:
# A general note on the apparent brittleness of this code:
@@ -223,7 +243,7 @@ class Extractor:
# Line positioning here is still consequential for correct stack tracing
# produced in _ast.compiler.
return CellDef(
- code=dedent(cell_code),
+ code=fixed_dedent(cell_code),
options=kwargs,
lineno=start_lineno - 1,
col_offset=node.col_offset + col_offset,
@@ -235,7 +255,7 @@ class Extractor:
def to_setup_cell(self, node: Node) -> SetupCell:
kwargs, _violations = _maybe_kwargs(node.items[0].context_expr) # type: ignore
code = self.extract_from_code(node)
- code = dedent(code)
+ code = fixed_dedent(code)
if code.endswith("\npass"):
code = code[: -len("\npass")]
return SetupCell(
@@ -385,7 +405,14 @@ class Parser:
# Attempt to find import statement
node = body.last
while node:
- if is_marimo_import(node):
+ if isinstance(node, ast.Import) and is_marimo_import(node):
+ if node.names[0].asname:
+ violations.append(
+ Violation(
+ "`marimo` is typically not imported with an alias. ",
+ node.lineno,
+ )
+ )
return ParseResult(node, violations=violations)
violations.append(
Violation(
@@ -399,7 +426,8 @@ class Parser:
def parse_version(self, body: PeekStack[Node]) -> ParseResult[str]:
# __generated_with not being correctly set should not break marimo.
violations: list[Violation] = []
- node = body.peek()
+ next(body)
+ node = body.last
version = _maybe_version(node) if node else None
if not version:
lineno = node.lineno if node else 0
@@ -415,20 +443,21 @@ class Parser:
return ParseResult(version, violations=violations)
def parse_app(
- self, body: PeekStack[Node]
+ self, body: PeekStack[Node], import_alias: str = "marimo"
) -> ParseResult[AppInstantiation]:
# app = import marimo + __generated_with + App(kwargs*)
violations: list[Violation] = []
node = body.last
while node:
- if is_app_def(node):
+ if is_app_def(node, import_alias=import_alias):
# type caught by is_app_def
_kwargs, _violations = _eval_kwargs(node.value.keywords) # type: ignore
violations.extend(_violations)
return ParseResult(
AppInstantiation(
options=_kwargs,
- )
+ ),
+ violations=violations,
)
violations.append(
Violation(
@@ -709,8 +738,8 @@ def get_valid_decorator(
return None
-def is_marimo_import(node: Node) -> bool:
- return isinstance(node, ast.Import) and node.names[0].name == "marimo"
+def is_marimo_import(node: ast.Import) -> bool:
+ return node.names[0].name == "marimo"
def is_string(node: Node) -> bool:
@@ -721,7 +750,7 @@ def is_string(node: Node) -> bool:
)
-def is_app_def(node: Node) -> bool:
+def is_app_def(node: Node, import_alias: str = "marimo") -> bool:
# Expected Ast:
#
# Assign(
@@ -740,7 +769,6 @@ def is_app_def(node: Node) -> bool:
# ]
# )
# )
-
# A bit obnoxious as a huge conditional, but also better for line coverage.
return (
isinstance(node, ast.Assign)
@@ -750,7 +778,7 @@ def is_app_def(node: Node) -> bool:
and isinstance(node.value, ast.Call)
and isinstance(node.value.func, ast.Attribute)
and isinstance(node.value.func.value, ast.Name)
- and node.value.func.value.id == "marimo"
+ and node.value.func.value.id == import_alias
and node.value.func.attr == "App"
)
@@ -864,13 +892,22 @@ def parse_notebook(contents: str) -> Optional[NotebookSerialization]:
valid=False,
)
violations.extend(import_result.violations)
+ # Extract import alias for the reference
+ # Import(names=[alias(name='marimo', asname='mo')])
+ import_alias = "marimo"
+ if import_result:
+ imported_node = import_result.unwrap()
+ if isinstance(imported_node, ast.Import):
+ import_alias = (
+ imported_node.names[0].asname or imported_node.names[0].name
+ )
version = None
if version_result := parser.parse_version(body):
version = version_result.unwrap()
violations.extend(version_result.violations)
- if not (app_result := parser.parse_app(body)):
+ if not (app_result := parser.parse_app(body, import_alias=import_alias)):
raise MarimoFileError("`marimo.App` definition expected.")
app = app_result.unwrap()
violations.extend(app_result.violations)
diff --git a/tests/_ast/codegen_data/test_get_alias_import.py b/tests/_ast/codegen_data/test_get_alias_import.py
new file mode 100644
index 0000000000..0ea4a74cb8
--- /dev/null
+++ b/tests/_ast/codegen_data/test_get_alias_import.py
@@ -0,0 +1,14 @@
+
+import marimo as mo
+
+__generated_with = "0.14.11"
+app = mo.App()
+
+
+def one():
+ x: int = 0
+ return (x,)
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/codegen_data/test_get_codes_multiline_string.py b/tests/_ast/codegen_data/test_get_codes_multiline_string.py
index d3d9b05193..099a088b0d 100644
--- a/tests/_ast/codegen_data/test_get_codes_multiline_string.py
+++ b/tests/_ast/codegen_data/test_get_codes_multiline_string.py
@@ -9,5 +9,12 @@ def one(): c = """
a, b"""; return (c,)
+def two():
+ d = """
+a, b"""
+ # comment
+ return (d,)
+
if __name__ == "__main__":
app.run()
diff --git a/tests/_ast/test_load.py b/tests/_ast/test_load.py
index 2a5013fe4f..a76f035bfe 100644
--- a/tests/_ast/test_load.py
+++ b/tests/_ast/test_load.py
@@ -178,8 +178,11 @@ class TestGetCodes:
app = load_app(get_filepath("test_get_codes_multiline_string"))
assert app is not None
cell_manager = app._cell_manager
- assert list(cell_manager.names()) == ["one"]
- assert list(cell_manager.codes()) == ['c = """\n a, b"""; ']
+ assert list(cell_manager.names()) == ["one", "two"]
+ assert list(cell_manager.codes()) == [
+ 'c = """\n a, b"""; ',
+ 'd = """\na, b"""\n# comment',
+ ]
@staticmethod
def test_get_codes_comment_after_sig(load_app) -> None:
@@ -224,6 +227,13 @@ class TestGetCodes:
get_filepath("test_get_codes_non_marimo_python_script")
)
+ @staticmethod
+ def test_import_alias(static_load) -> None:
+ app = static_load(get_filepath("test_get_alias_import"))
+ assert app is not None
+ cell_manager = app._cell_manager
+ assert list(cell_manager.names()) == ["one"]
+
@staticmethod
def test_get_codes_app_with_no_cells(load_app) -> None:
app = load_app(get_filepath("test_app_with_no_cells"))
diff --git a/tests/_server/export/snapshots/notebook_with_outputs.ipynb.txt b/tests/_server/export/snapshots/notebook_with_outputs.ipynb.txt
index 725dfc2d36..9cb1400d7a 100644
--- a/tests/_server/export/snapshots/notebook_with_outputs.ipynb.txt
+++ b/tests/_server/export/snapshots/notebook_with_outputs.ipynb.txt
@@ -87,7 +87,7 @@
},
"outputs": [],
"source": [
- "import marimo as mo\n"
+ "import marimo as mo"
]
},
{