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 other "globals" to be caught for builtins (#4662)

Dylan Madisettic46fb721f660
diff --git a/marimo/_ast/app.py b/marimo/_ast/app.py
index 44d15a2615..ba7109506c 100644
--- a/marimo/_ast/app.py
+++ b/marimo/_ast/app.py
@@ -3,7 +3,6 @@ from __future__ import annotations
import ast
import base64
-import builtins
import inspect
import sys
import threading
@@ -25,6 +24,7 @@ from typing import (
from uuid import uuid4
from marimo._ast.app_config import _AppConfig
+from marimo._ast.variables import BUILTINS
from marimo._types.ids import CellId_t
if sys.version_info < (3, 10):
@@ -113,7 +113,7 @@ class _SetupContext:
def __enter__(self) -> None:
with_frame = sys._getframe(1)
- if refs := self._cell.refs - set(builtins.__dict__.keys()):
+ if refs := self._cell.refs - BUILTINS:
# Otherwise fail in say a script context.
raise SetupRootError(
f"The setup cell cannot reference any additional variables: {refs}"
diff --git a/marimo/_ast/codegen.py b/marimo/_ast/codegen.py
index f81d926e59..7996f6a30c 100644
--- a/marimo/_ast/codegen.py
+++ b/marimo/_ast/codegen.py
@@ -2,7 +2,6 @@
from __future__ import annotations
import ast
-import builtins
import json
import os
import re
@@ -16,6 +15,7 @@ from marimo._ast.cell import CellConfig, CellImpl
from marimo._ast.compiler import compile_cell
from marimo._ast.names import DEFAULT_CELL_NAME, SETUP_CELL_NAME
from marimo._ast.toplevel import TopLevelExtraction, TopLevelStatus
+from marimo._ast.variables import BUILTINS
from marimo._ast.visitor import Name
from marimo._types.ids import CellId_t
@@ -177,7 +177,7 @@ def to_functiondef(
# should not be taken as args by a cell's functiondef (since they are
# already in globals)
if allowed_refs is None:
- allowed_refs = set(builtins.__dict__.keys())
+ allowed_refs = BUILTINS
refs = tuple(ref for ref in sorted(cell.refs) if ref not in allowed_refs)
decorator = to_decorator(cell.config, fn=fn)
@@ -218,7 +218,7 @@ def to_top_functiondef(
# For the top-level function criteria to be satisfied,
# the cell, it must pass basic checks in the cell impl.
if allowed_refs is None:
- allowed_refs = set(builtins.__dict__.keys())
+ allowed_refs = BUILTINS
toplevel_var = cell.toplevel_variable
assert toplevel_var, "Cell is not a top-level function"
diff --git a/marimo/_ast/toplevel.py b/marimo/_ast/toplevel.py
index d8fe1c3953..fa457c2387 100644
--- a/marimo/_ast/toplevel.py
+++ b/marimo/_ast/toplevel.py
@@ -1,7 +1,6 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-import builtins
import token as token_types
from enum import Enum
from io import BytesIO
@@ -16,6 +15,7 @@ from marimo._ast.names import (
SETUP_CELL_NAME,
TOPLEVEL_CELL_PREFIX,
)
+from marimo._ast.variables import BUILTINS
from marimo._ast.visitor import Name
from marimo._runtime.dataflow import DirectedGraph
from marimo._types.ids import CellId_t
@@ -264,7 +264,7 @@ class TopLevelExtraction:
[status.name for status in self.statuses if not status.is_toplevel]
)
- self.unshadowed = set(builtins.__dict__.keys()) - defs
+ self.unshadowed = BUILTINS - defs
self.allowed_refs.update(self.unshadowed)
self.used_refs = refs
diff --git a/marimo/_ast/variables.py b/marimo/_ast/variables.py
index df95ad2dbe..db70142195 100644
--- a/marimo/_ast/variables.py
+++ b/marimo/_ast/variables.py
@@ -1,6 +1,7 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
+import builtins
import re
from collections import namedtuple
from typing import Optional
@@ -11,6 +12,17 @@ UnmagledLocal = namedtuple("UnmagledLocal", "name cell")
_EMPTY_CELL_ID = CellId_t("")
+BUILTINS = set(
+ {
+ *set(builtins.__dict__.keys()),
+ # resolved from:
+ # set(globals().keys()) - set(builtins.__dict__.keys())
+ "__builtin__",
+ "__file__",
+ "__builtins__",
+ }
+)
+
def if_local_then_mangle(ref: str, cell_id: CellId_t) -> str:
if is_local(ref):
diff --git a/marimo/_cli/export/cloudflare.py b/marimo/_cli/export/cloudflare.py
index 71e59f8575..43624805a8 100644
--- a/marimo/_cli/export/cloudflare.py
+++ b/marimo/_cli/export/cloudflare.py
@@ -1,3 +1,4 @@
+# Copyright 2025 Marimo. All rights reserved.
from pathlib import Path
from marimo._cli.print import bold, echo, green
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 12e1eb419d..279e44046d 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -2,7 +2,6 @@
from __future__ import annotations
import asyncio
-import builtins
import contextlib
import dataclasses
import io
@@ -26,7 +25,7 @@ from marimo._ast.cell import CellConfig, CellImpl
from marimo._ast.compiler import compile_cell
from marimo._ast.errors import ImportStarError
from marimo._ast.names import SETUP_CELL_NAME
-from marimo._ast.variables import is_local
+from marimo._ast.variables import BUILTINS, is_local
from marimo._ast.visitor import ImportData, Name, VariableData
from marimo._config.config import ExecutionType, MarimoConfig, OnCellChangeType
from marimo._config.settings import GLOBAL_SETTINGS
@@ -209,7 +208,7 @@ def refs() -> tuple[str, ...]:
return tuple()
# builtins that have not been shadowed by the user
- unshadowed_builtins = set(builtins.__dict__.keys()).difference(
+ unshadowed_builtins = BUILTINS.difference(
set(ctx.graph.definitions.keys())
)
diff --git a/tests/_ast/codegen_data/test_generate_filecontents_toplevel.py b/tests/_ast/codegen_data/test_generate_filecontents_toplevel.py
index 72618a349b..ccf2f2373b 100644
--- a/tests/_ast/codegen_data/test_generate_filecontents_toplevel.py
+++ b/tests/_ast/codegen_data/test_generate_filecontents_toplevel.py
@@ -68,6 +68,12 @@ def fun_that_uses_another_but_out_of_order():
return fun_that_uses_another()
+def fun_uses_file():
+ # file is in globals but not builtins
+ return __file__
+
+
@app.function
def fun_that_uses_another():
return fun_that_uses_mo()
diff --git a/tests/_ast/test_load.py b/tests/_ast/test_load.py
index 4d5c0a6eb3..3f2343a12b 100644
--- a/tests/_ast/test_load.py
+++ b/tests/_ast/test_load.py
@@ -238,6 +238,7 @@ class TestGetCodes:
"_",
"*fun_that_uses_mo",
"*fun_that_uses_another_but_out_of_order",
+ "*fun_uses_file",
"*fun_that_uses_another",
"cell_with_ref_and_def",
"_",
diff --git a/tests/_ast/test_parse.py b/tests/_ast/test_parse.py
index 2d5bff1fb9..d61acfb27f 100644
--- a/tests/_ast/test_parse.py
+++ b/tests/_ast/test_parse.py
@@ -63,6 +63,7 @@ class TestParser:
"_",
"fun_that_uses_mo",
"fun_that_uses_another_but_out_of_order",
+ "fun_uses_file",
"fun_that_uses_another",
"cell_with_ref_and_def",
"_",