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: couple more edge cases for Fix duplicate `import marimo as mo` (#5022)

Myles Scolnick2ad12fcf9ee2
diff --git a/marimo/_convert/ipynb.py b/marimo/_convert/ipynb.py
index 23e2cfa867..247f862368 100644
--- a/marimo/_convert/ipynb.py
+++ b/marimo/_convert/ipynb.py
@@ -77,15 +77,31 @@ def transform_add_marimo_import(sources: list[str]) -> list[str]:
def contains_mo(cell: str) -> bool:
return cell.startswith("mo.md(") or "mo.sql(" in cell
- def imports_mo(cell: str) -> bool:
- # TODO this is a quick heuristic and will fail if mo is defined in other ways
- # however detecting arbitrary definitions would require an AST parse
- # which isn't being performed during this pass
- return "import marimo as mo" in cell
-
- if any(contains_mo(cell) for cell in sources) and not any(
- imports_mo(cell) for cell in sources
- ):
+ def has_marimo_import(cell: str) -> bool:
+ # Quick check
+ if "import marimo as mo" not in cell:
+ return False
+
+ def is_in_import_line(line: str) -> bool:
+ if line.startswith("import marimo as mo"):
+ return True
+ if line.startswith("import ") or line.startswith("from "):
+ return "import marimo as mo" in line
+ return False
+
+ # Slow check
+ lines = cell.strip().split("\n")
+ if any(is_in_import_line(line) for line in lines):
+ return True
+ return False
+
+ already_has_marimo_import = any(
+ has_marimo_import(cell) for cell in sources
+ )
+ if already_has_marimo_import:
+ return sources
+
+ if any(contains_mo(cell) for cell in sources):
return sources + ["import marimo as mo"]
return sources
diff --git a/tests/_convert/test_ipynb.py b/tests/_convert/test_ipynb.py
index 785eb3f540..cc27a31625 100644
--- a/tests/_convert/test_ipynb.py
+++ b/tests/_convert/test_ipynb.py
@@ -122,23 +122,26 @@ def test_transform_fixup_multiple_definitions_when_not_encapsulated():
def test_transform_add_marimo_import():
+ # mo.md
sources = [
"mo.md('# Hello')",
"print('World')",
- "mo.sql('SELECT * FROM table')",
]
- expected = [
- "mo.md('# Hello')",
- "print('World')",
+ result = transform_add_marimo_import(sources)
+ assert "import marimo as mo" in result
+
+ # mo.sql
+ sources = [
"mo.sql('SELECT * FROM table')",
- "import marimo as mo",
+ "print('World')",
]
result = transform_add_marimo_import(sources)
- assert result == expected
+ assert "import marimo as mo" in result
# if `import marimo as mo` is already present
# it should not be added again
- assert transform_add_marimo_import(expected) == expected
+ existing = sources + ["import marimo as mo"]
+ assert transform_add_marimo_import(existing) == existing
existing = [
# slight support for different import orders
@@ -149,6 +152,34 @@ def test_transform_add_marimo_import():
assert transform_add_marimo_import(existing) == existing
+def test_transform_add_marimo_import_already_imported():
+ sources = [
+ "import marimo as mo",
+ "mo.md('# Hello')",
+ "print('World')",
+ ]
+ result = transform_add_marimo_import(sources)
+ assert result == sources
+
+
+def test_transform_add_marimo_import_already_but_in_comment_or_definition():
+ # Comment
+ sources = [
+ "mo.md('# Hello')",
+ "# import marimo as mo",
+ ]
+ result = transform_add_marimo_import(sources)
+ assert result == sources + ["import marimo as mo"]
+
+ # Definition
+ sources = [
+ "mo.md('# Hello')",
+ "def foo():\n import marimo as mo",
+ ]
+ result = transform_add_marimo_import(sources)
+ assert result == sources + ["import marimo as mo"]
+
+
def test_transform_magic_commands():
sources = [
"%%sql\nSELECT * FROM table",