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 @prop.setter in class definitions (#5004)

Dylan Madisetti782371c657c0
diff --git a/marimo/_ast/visitor.py b/marimo/_ast/visitor.py
index 7c9eb09589..5c5a075fd1 100644
--- a/marimo/_ast/visitor.py
+++ b/marimo/_ast/visitor.py
@@ -471,11 +471,25 @@ class ScopedVisitor(ast.NodeVisitor):
# The unbounded defs are carried from the recursive visitor
# excluding all references found in exclusively in scope.
ignore_refs = set()
+ class_def: set[Name] = set()
for var in mock_visitor.variable_data:
for data in mock_visitor.variable_data[var]:
if data.kind in ("function", "class"):
- unbounded_refs |= data.unbounded_refs
+ unbounded_refs |= data.unbounded_refs - class_def
ignore_refs |= data.required_refs
+ # class def is captured because the following is valid:
+ # >>> class C:
+ # >>> A = 1
+ # >>> B = A + 1
+ # >>> @property
+ # >>> def prop(self): ...
+ # >>> @prop.setter
+ # >>> def prop(self, value): ...
+ #
+ # - even though these variables only live in scope of
+ # the class definition.
+ # Thus, if it has been declared, it's not "unbounded"
+ class_def.add(var)
else:
unbounded_refs |= data.required_refs
unbounded_refs |= mock_visitor.refs - ignore_refs
diff --git a/tests/_ast/test_toplevel.py b/tests/_ast/test_toplevel.py
index 9aef615150..b3e5e677b0 100644
--- a/tests/_ast/test_toplevel.py
+++ b/tests/_ast/test_toplevel.py
@@ -348,6 +348,25 @@ class TestTopLevelExtraction:
s.type for s in extraction
], [s.hint for s in extraction]
+ @staticmethod
+ def test_name_is_not_propagated(app) -> None:
+ @app.cell
+ def cell():
+ value = 1
+
+ @app.function
+ def to_be_demoted():
+ return value + 1 # type: ignore # noqa: F821
+
+ extraction = TopLevelExtraction.from_app(InternalApp(app))
+ assert [
+ TopLevelType.CELL,
+ TopLevelType.CELL,
+ ] == [s.type for s in extraction], [s.hint for s in extraction]
+ assert ["cell", "_"] == [s.name for s in extraction], [
+ s.hint for s in extraction
+ ]
+
class TestTopLevelClasses:
@staticmethod
@@ -455,9 +474,16 @@ class TestTopLevelClasses:
] == [s.type for s in extraction], [s.hint for s in extraction]
@staticmethod
- def test_class_invocation(app) -> None:
+ def test_class_properties(app) -> None:
@app.class_definition
- class Example: ...
+ class Example:
+ @property
+ def prop(self) -> int:
+ return 1
+
+ @prop.setter
+ def prop(self, value: int) -> None:
+ pass
@app.function
def f():
@@ -470,23 +496,19 @@ class TestTopLevelClasses:
] == [s.type for s in extraction], [s.hint for s in extraction]
@staticmethod
- def test_name_is_not_propagated(app) -> None:
- @app.cell
- def cell():
- value = 1
+ def test_class_invocation(app) -> None:
+ @app.class_definition
+ class Example: ...
@app.function
- def to_be_demoted():
- return value + 1 # type: ignore # noqa: F821
+ def f():
+ return Example()
extraction = TopLevelExtraction.from_app(InternalApp(app))
assert [
- TopLevelType.CELL,
- TopLevelType.CELL,
+ TopLevelType.TOPLEVEL,
+ TopLevelType.TOPLEVEL,
] == [s.type for s in extraction], [s.hint for s in extraction]
- assert ["cell", "_"] == [s.name for s in extraction], [
- s.hint for s in extraction
- ]
class TestTopLevelHook: