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: treat special variable "__" as local variable (#1764)

ontowhee6082e861c5da
diff --git a/marimo/_utils/variables.py b/marimo/_utils/variables.py
index c3a2f5ec35..cae340b3cb 100644
--- a/marimo/_utils/variables.py
+++ b/marimo/_utils/variables.py
@@ -12,4 +12,4 @@ def is_mangled_local(name: str, cell_id: CellId_t) -> bool:
def is_local(name: str) -> bool:
- return name.startswith("_") and not name.startswith("__")
+ return name == "__" or (name.startswith("_") and not name.startswith("__"))
diff --git a/tests/_ast/test_app.py b/tests/_ast/test_app.py
index c58b8bd7b2..19d8ac966c 100644
--- a/tests/_ast/test_app.py
+++ b/tests/_ast/test_app.py
@@ -275,6 +275,25 @@ class TestApp:
app.run()
+ @staticmethod
+ def test_dunder_rewritten_as_local() -> None:
+ app = App()
+
+ @app.cell
+ def _() -> None:
+ __ = 1 # noqa: F841
+ return
+
+ @app.cell
+ def _() -> None:
+ __ # type: ignore
+ return
+
+ with pytest.raises(NameError) as e:
+ app.run()
+
+ assert "'__' is not defined" in str(e.value)
+
@staticmethod
def test_app_width_config() -> None:
app = App(width="full")
diff --git a/tests/_ast/test_compiler.py b/tests/_ast/test_compiler.py
index 03dcfaf6de..b30d2cb99a 100644
--- a/tests/_ast/test_compiler.py
+++ b/tests/_ast/test_compiler.py
@@ -21,7 +21,7 @@ class TestParseCell:
@staticmethod
def test_local_variables() -> None:
- code = "_, y = f(x)\ndef _foo():\n _bar = 0\nimport _secret_module as module" # noqa: E501
+ code = "__ = 10\n_, y = f(x)\ndef _foo():\n _bar = 0\nimport _secret_module as module" # noqa: E501
cell = compile_cell(code)
assert cell.defs == {"module", "y"}
assert cell.refs == {"f", "x"}
@@ -29,9 +29,9 @@ class TestParseCell:
@staticmethod
def test_dunder_dunder_excluded() -> None:
- code = "__ = 10; __name__ = 20"
+ code = "__name__ = 20"
cell = compile_cell(code)
- assert cell.defs == {"__", "__name__"}
+ assert cell.defs == {"__name__"}
assert cell.refs == set()
@staticmethod