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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: allow other "globals" to be caught for builtins (#4662)
Dylan Madisettic46fb721f660
diff --git a/marimo/_ast/app.py b/marimo/_ast/app.pyindex 44d15a2615..ba7109506c 100644--- a/marimo/_ast/app.py+++ b/marimo/_ast/app.py@@ -3,7 +3,6 @@ from __future__ import annotationsimport astimport base64-import builtinsimport inspectimport sysimport threading@@ -25,6 +24,7 @@ from typing import (from uuid import uuid4from marimo._ast.app_config import _AppConfig+from marimo._ast.variables import BUILTINSfrom marimo._types.ids import CellId_tif 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.pyindex f81d926e59..7996f6a30c 100644--- a/marimo/_ast/codegen.py+++ b/marimo/_ast/codegen.py@@ -2,7 +2,6 @@from __future__ import annotationsimport ast-import builtinsimport jsonimport osimport re@@ -16,6 +15,7 @@ from marimo._ast.cell import CellConfig, CellImplfrom marimo._ast.compiler import compile_cellfrom marimo._ast.names import DEFAULT_CELL_NAME, SETUP_CELL_NAMEfrom marimo._ast.toplevel import TopLevelExtraction, TopLevelStatus+from marimo._ast.variables import BUILTINSfrom marimo._ast.visitor import Namefrom 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 = BUILTINSrefs = 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 = BUILTINStoplevel_var = cell.toplevel_variableassert toplevel_var, "Cell is not a top-level function"diff --git a/marimo/_ast/toplevel.py b/marimo/_ast/toplevel.pyindex 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 builtinsimport token as token_typesfrom enum import Enumfrom io import BytesIO@@ -16,6 +15,7 @@ from marimo._ast.names import (SETUP_CELL_NAME,TOPLEVEL_CELL_PREFIX,)+from marimo._ast.variables import BUILTINSfrom marimo._ast.visitor import Namefrom marimo._runtime.dataflow import DirectedGraphfrom 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 - defsself.allowed_refs.update(self.unshadowed)self.used_refs = refsdiff --git a/marimo/_ast/variables.py b/marimo/_ast/variables.pyindex 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 builtinsimport refrom collections import namedtuplefrom 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.pyindex 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 Pathfrom marimo._cli.print import bold, echo, greendiff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 12e1eb419d..279e44046d 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -2,7 +2,6 @@from __future__ import annotationsimport asyncio-import builtinsimport contextlibimport dataclassesimport io@@ -26,7 +25,7 @@ from marimo._ast.cell import CellConfig, CellImplfrom marimo._ast.compiler import compile_cellfrom marimo._ast.errors import ImportStarErrorfrom marimo._ast.names import SETUP_CELL_NAME-from marimo._ast.variables import is_local+from marimo._ast.variables import BUILTINS, is_localfrom marimo._ast.visitor import ImportData, Name, VariableDatafrom marimo._config.config import ExecutionType, MarimoConfig, OnCellChangeTypefrom 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.pyindex 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.functiondef fun_that_uses_another():return fun_that_uses_mo()diff --git a/tests/_ast/test_load.py b/tests/_ast/test_load.pyindex 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.pyindex 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","_",