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: disallow `import *` in marimo notebooks (#387)

Akshay Agrawaleeccbcd4d416
diff --git a/marimo/_ast/test_visitor.py b/marimo/_ast/test_visitor.py
index 9451e76c5c..3a499037d9 100644
--- a/marimo/_ast/test_visitor.py
+++ b/marimo/_ast/test_visitor.py
@@ -553,6 +553,8 @@ def test_from_import_star() -> None:
expr = "from a.b.c import *"
v = visitor.ScopedVisitor()
mod = ast.parse(expr)
- v.visit(mod)
+ with pytest.raises(SyntaxError) as e:
+ v.visit(mod)
+ assert "`import *` is not allowed in marimo." in str(e)
assert v.defs == set()
assert v.refs == set()
diff --git a/marimo/_ast/visitor.py b/marimo/_ast/visitor.py
index 9f01e5e08b..1aa32cdced 100644
--- a/marimo/_ast/visitor.py
+++ b/marimo/_ast/visitor.py
@@ -293,6 +293,11 @@ class ScopedVisitor(ast.NodeVisitor):
# that needs to be tracked?
# Import and ImportFrom statements have symbol names in alias nodes
def visit_alias(self, node: ast.alias) -> None:
+ """Visiting names in import statements
+
+ NB: We disallow `import *` because Python only allows
+ star imports at module-level, but we store cells as functions.
+ """
if node.asname is None:
# imported name, no "as" clause; examples:
# import [a.b.c] - we define a
@@ -302,8 +307,16 @@ class ScopedVisitor(ast.NodeVisitor):
# Note:
# Don't mangle - user has no control over package name
basename = node.name.split(".")[0]
- if basename != "*":
- self._define(basename)
+ if basename == "*":
+ line = (
+ f"line {node.lineno}"
+ if hasattr(node, "lineno")
+ else "line ..."
+ )
+ raise SyntaxError(
+ f"{line} SyntaxError: `import *` is not allowed in marimo."
+ )
+ self._define(basename)
else:
node.asname = self._if_local_then_mangle(node.asname)
self._define(node.asname)