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: typing with default cells (#5047)
Dylan Madisetticb25bd5cbe68
diff --git a/marimo/_ast/codegen.py b/marimo/_ast/codegen.pyindex 27b8c7b3f9..7068b11df2 100644--- a/marimo/_ast/codegen.py+++ b/marimo/_ast/codegen.py@@ -32,13 +32,6 @@ Cls: TypeAlias = typeINDENT = " "MAX_LINE_LENGTH = 80-NOTICE = "\n".join(- [- "# Imports used by your notebook.",- "# This section is automatically generated by marimo.",- ]-)-BRACES: dict[Literal["(", "["], tuple[str, str]] = {"(": ("(", ")"),"[": ("[", "]"),diff --git a/marimo/_ast/toplevel.py b/marimo/_ast/toplevel.pyindex 10781ace41..8b100ca692 100644--- a/marimo/_ast/toplevel.py+++ b/marimo/_ast/toplevel.py@@ -239,6 +239,7 @@ class TopLevelExtraction:TopLevelType.UNRESOLVED: self.unresolved,TopLevelType.CELL: self.cells,}+ self.collection: set[TopLevelStatus] = set({})# Track definitions and referencesdefs: set[Name] = set()@@ -259,6 +260,7 @@ class TopLevelExtraction:if not status.is_unparsable:defs.update(status.defs)refs.update(status.refs)+ self.collection.add(status)# Refresh namespotential_refs = set(@@ -356,7 +358,8 @@ class TopLevelExtraction:if self._variables is not None:return self._variablesvariables = {}- for status in self.cells.values():++ for status in self.collection:if status._cell:variables.update(status._cell.init_variable_data)for var, status in self.toplevel.items():diff --git a/tests/_ast/codegen_data/test_app_with_annotation_typing.py b/tests/_ast/codegen_data/test_app_with_annotation_typing.pynew file mode 100644index 0000000000..b909f7c152--- /dev/null+++ b/tests/_ast/codegen_data/test_app_with_annotation_typing.py@@ -0,0 +1,30 @@+import marimo++__generated_with = "0.0.0"+app = marimo.App(width="medium")++with app.setup:+ CONSTANT: int = 42+++def _():+ z: "int" = 0+ return (z,)+++def _():+ x: int = CONSTANT + 2+ y: float = 2.0+ return x, y+++def _(x: int, y: float, z: "int"):+ _ = x + y + z+ return+++if __name__ == "__main__":+ app.run()diff --git a/tests/_ast/test_codegen.py b/tests/_ast/test_codegen.pyindex 7f5e362539..ca2672274e 100644--- a/tests/_ast/test_codegen.py+++ b/tests/_ast/test_codegen.py@@ -431,6 +431,13 @@ class TestGeneration:)assert "import marimo" in source+ @staticmethod+ def test_generate_filecontents_with_annotation_typing() -> None:+ source = get_idempotent_marimo_source(+ "test_app_with_annotation_typing"+ )+ assert "import marimo" in source+@pytest.fixturedef marimo_app() -> App:diff --git a/tests/_ast/test_toplevel.py b/tests/_ast/test_toplevel.pyindex b3e5e677b0..59caee1585 100644--- a/tests/_ast/test_toplevel.py+++ b/tests/_ast/test_toplevel.py@@ -367,6 +367,67 @@ class TestTopLevelExtraction:s.hint for s in extraction]+ @staticmethod+ def test_variables_extracted(app) -> None:+ with app.setup:+ CONSTANT: int = 42++ @app.cell+ def _():+ x: int = 2+ # No typing+ z = CONSTANT+ return (x, z)++ @app.cell+ def _():+ y: float = 2.0+ return (y,)++ extraction = TopLevelExtraction.from_app(InternalApp(app))++ from marimo._ast.visitor import AnnotationData, VariableData++ assert extraction.variables == {+ "x": VariableData(+ kind="variable",+ required_refs={+ "int",+ },+ unbounded_refs=set(),+ annotation_data=AnnotationData(+ repr="int",+ refs={+ "int",+ },+ ),+ import_data=None,+ ),+ "y": VariableData(+ kind="variable",+ required_refs={+ "float",+ },+ unbounded_refs=set(),+ annotation_data=AnnotationData(+ repr="float",+ refs={+ "float",+ },+ ),+ import_data=None,+ ),+ "z": VariableData(+ kind="variable",+ required_refs={+ "CONSTANT",+ },+ unbounded_refs=set(),+ annotation_data=None,+ import_data=None,+ ),+ }+class TestTopLevelClasses:@staticmethoddiff --git a/tests/_ast/test_visitor.py b/tests/_ast/test_visitor.pyindex eb4bd99280..8379cce2c8 100644--- a/tests/_ast/test_visitor.py+++ b/tests/_ast/test_visitor.py@@ -10,6 +10,7 @@ import pytestfrom marimo._ast import visitorfrom marimo._ast.errors import ImportStarErrorfrom marimo._ast.visitor import (+ AnnotationData,ImportData,VariableData,normalize_sql_f_string,@@ -419,6 +420,28 @@ def test_function_with_args() -> None:}+def test_annotations_captured() -> None:+ code = cleandoc(+ """+ x: int = CONSTANT + 2+ """+ )+ v = visitor.ScopedVisitor()+ mod = ast.parse(code)+ v.visit(mod)+ assert v.defs == set(["x"])+ assert v.refs == set(["int", "CONSTANT"])+ assert v.variable_data == {+ "x": [+ VariableData(+ kind="variable",+ required_refs={"CONSTANT", "int"},+ annotation_data=AnnotationData(repr="int", refs={"int"}),+ )+ ],+ }++def test_function_with_defaults() -> None:code = cleandoc("""