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: don't compile code with annotations future flag (#3918)

Akshay Agrawala17e08a3ded2
diff --git a/marimo/_ast/compiler.py b/marimo/_ast/compiler.py
index 0e77cc7e5d..79a25f1421 100644
--- a/marimo/_ast/compiler.py
+++ b/marimo/_ast/compiler.py
@@ -147,6 +147,8 @@ def compile_cell(
code,
"<unknown>",
mode="exec",
+ # don't inherit compiler flags, in particular future annotations
+ dont_inherit=True,
flags=ast.PyCF_ONLY_AST | ast.PyCF_ALLOW_TOP_LEVEL_AWAIT,
)
if not module.body:
@@ -221,8 +223,12 @@ def compile_cell(
)
flags = ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
- body = compile(module, filename, mode="exec", flags=flags)
- last_expr = compile(expr, filename, mode="eval", flags=flags)
+ body = compile(
+ module, filename, mode="exec", dont_inherit=True, flags=flags
+ )
+ last_expr = compile(
+ expr, filename, mode="eval", dont_inherit=True, flags=flags
+ )
nonlocals = {name for name in v.defs if not is_local(name)}
temporaries = v.defs - nonlocals
diff --git a/tests/_ast/test_compiler.py b/tests/_ast/test_compiler.py
index c2be07b5ba..eced330db8 100644
--- a/tests/_ast/test_compiler.py
+++ b/tests/_ast/test_compiler.py
@@ -71,6 +71,14 @@ class TestParseCell:
assert not cell.imported_namespaces
+class TestCompilerFlags:
+ @staticmethod
+ def test_top_level_await_ok() -> None:
+ code = "await foo()"
+ cell = compile_cell(code)
+ assert cell.refs == {"foo"}
+
+
class TestImportWorkspace:
@staticmethod
def test_import() -> None:
diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.py
index 93964612aa..581ec515e0 100644
--- a/tests/_runtime/test_runtime.py
+++ b/tests/_runtime/test_runtime.py
@@ -2810,6 +2810,25 @@ def test_notebook_dir_in_non_notebook_mode() -> None:
assert notebook_location() == pathlib.Path().absolute()
+async def test_future_annotations_not_inherited(
+ k: Kernel, exec_req: ExecReqProvider
+) -> None:
+ await k.run(
+ [
+ exec_req.get(
+ """
+ class A: pass
+ def foo() -> A:
+ ...
+ anno = foo.__annotations__
+ """
+ )
+ ]
+ )
+ assert not k.errors
+ assert k.globals["A"] == k.globals["anno"]["return"]
+
+
def _parse_error_output(cell_op: CellOp) -> list[Error]:
error_output = cell_op.output
assert error_output is not None