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: recognize class variables used within class scope as non-external dependencies (#7265) (#7273)

Myles Scolnick3bc4d8dd68bd
diff --git a/marimo/_ast/visitor.py b/marimo/_ast/visitor.py
index 135441b09e..f7e851eebe 100644
--- a/marimo/_ast/visitor.py
+++ b/marimo/_ast/visitor.py
@@ -531,7 +531,12 @@ class ScopedVisitor(ast.NodeVisitor):
# Thus, if it has been declared, it's not "unbounded"
class_def.add(var)
else:
- unbounded_refs |= data.required_refs
+ # For non-function/non-class variables (e.g., class attributes),
+ # exclude references to variables already defined in class scope
+ unbounded_refs |= data.required_refs - class_def
+ # Add the variable to class_def so that later references
+ # to it don't create unbounded refs
+ class_def.add(var)
unbounded_refs |= mock_visitor.refs - ignore_refs
# Handle function/class refs that are evaluated in the outer scope
diff --git a/marimo/_smoke_tests/parse/classes.py b/marimo/_smoke_tests/parse/classes.py
new file mode 100644
index 0000000000..a623764853
--- /dev/null
+++ b/marimo/_smoke_tests/parse/classes.py
@@ -0,0 +1,73 @@
+import marimo
+
+__generated_with = "0.18.0"
+app = marimo.App(width="medium")
+
+
+# This should be reusable
+class One:
+ A: int = 1
+
+ def run(a: A = 1) -> int:
+ return a
+
+
+# This should be a pure-class
+class Two:
+ A: int = 1
+
+ def run(a: int = 1) -> int:
+ return a
+
+
+def _():
+ C = int
+ return (C,)
+
+
+def _():
+ value = 1
+ return (value,)
+
+
+def _(C):
+ # This should NOT be reusable (depends on C)
+ class Three:
+ A: int = 1
+ B: int = 1
+
+ def run(a: C = 1) -> int:
+ return a
+ return
+
+
+def _(C):
+ # This should NOT be reusable (depends on C)
+ class Four:
+ A: int = 1
+ B: C = 1
+
+ def run(a: B = 1) -> int:
+ return a
+ return
+
+
+def _(value):
+ # This should NOT be reusable (depends on value)
+ class Five:
+ A: int = 1
+
+ def run(a: A = value) -> int:
+ return a
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_ast/test_visitor.py b/tests/_ast/test_visitor.py
index 004157836d..aaf179cd01 100644
--- a/tests/_ast/test_visitor.py
+++ b/tests/_ast/test_visitor.py
@@ -1593,3 +1593,48 @@ def test_sql_pivot_unpivot_commands(
assert v.defs == {"df"}, f"Failed for: {description}"
assert v.refs == expected_refs, f"Failed for: {description}"
+
+
+def test_class_with_class_var_in_method_default() -> None:
+ """Test that class variables used in method defaults don't create unbounded refs (issue #7265)."""
+ code = cleandoc(
+ """
+ class K:
+ A: int = 1
+ def b(a: int = A) -> int:
+ return a
+ """
+ )
+ v = visitor.ScopedVisitor()
+ mod = ast.parse(code)
+ v.visit(mod)
+
+ # K should be a def
+ assert v.defs == set(["K"])
+ # A should NOT be a ref (it's defined in class scope), but int is (type annotation)
+ assert v.refs == set(["int"])
+ # K should have unbounded_refs only for int, not A
+ assert v.variable_data["K"][0].unbounded_refs == set(["int"])
+
+
+def test_class_with_class_var_referencing_another() -> None:
+ """Test that class variables used in other class variable definitions don't create unbounded refs (issue #7265)."""
+ code = cleandoc(
+ """
+ class K:
+ A: int = 1
+ B: int = A
+ def b(a: int = B) -> int:
+ return a
+ """
+ )
+ v = visitor.ScopedVisitor()
+ mod = ast.parse(code)
+ v.visit(mod)
+
+ # K should be a def
+ assert v.defs == set(["K"])
+ # A should NOT be a ref (it's defined in class scope), but int is (type annotation)
+ assert v.refs == set(["int"])
+ # K should have unbounded_refs only for int, not A
+ assert v.variable_data["K"][0].unbounded_refs == set(["int"])