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(formatter): wrap cells in function scope before ruff formatting (#9889)
Vishak Baddur7ae012e48b99
diff --git a/marimo/_ast/parse.py b/marimo/_ast/parse.pyindex df5c68e1f3..5eff55ea55 100644--- a/marimo/_ast/parse.py+++ b/marimo/_ast/parse.py@@ -84,6 +84,28 @@ def fixed_dedent(text: str) -> str:return dedent("\n".join(map(refill, lines)))+def unwrap_cell_body(formatted: str) -> str:+ """Extract the body of a wrapped `def _():` cell after ruff formatting.++ Used by RuffFormatter to unwrap cells that were temporarily wrapped in a+ dummy function so ruff applies function-scope blank-line rules (E301)+ instead of file-scope rules (E302).+ """+ tree = ast_parse(formatted)+ fn = tree.body[0]+ if not isinstance(fn, ast.FunctionDef):+ raise ValueError(+ f"Expected a FunctionDef node, got {type(fn).__name__}"+ )+ if fn.end_lineno is None:+ raise ValueError("FunctionDef node has no end_lineno")+ extractor = Extractor(formatted)+ raw = extractor.extract_from_offsets(+ fn.body[0].lineno - 1, 0, fn.end_lineno - 1, fn.end_col_offset+ )+ return fixed_dedent(raw).strip()++def extract_lineno(node: Node) -> int:if not isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef, ast.ClassDef)diff --git a/marimo/_utils/formatter.py b/marimo/_utils/formatter.pyindex 649568a35d..411895dc6d 100644--- a/marimo/_utils/formatter.py+++ b/marimo/_utils/formatter.py@@ -4,8 +4,10 @@ from __future__ import annotationsimport asyncioimport subprocessimport sys+import textwrapfrom marimo import _loggers+from marimo._ast.parse import unwrap_cell_bodyfrom marimo._dependencies.dependencies import DependencyManagerfrom marimo._types.ids import CellId_t@@ -142,14 +144,47 @@ class RuffFormatter(Formatter):async def format(self, codes: CellCodes, stdin_filename: str | None = None) -> CellCodes:- return await ruff(- codes,+ # Wrap each cell in a dummy function so ruff applies function-scope+ # blank-line rules (1 blank line around nested defs) instead of+ # file-scope rules (2 blank lines). Cells with no executable+ # statements (whitespace-only or comment-only) are left unwrapped+ # since an empty or comment-only function body is invalid Python.+ skip_wrap = {+ key+ for key, code in codes.items()+ if all(+ not line.strip() or line.strip().startswith("#")+ for line in code.splitlines()+ )+ }+ wrapped: CellCodes = {}+ for key, code in codes.items():+ if key not in skip_wrap:+ wrapped[key] = "\n".join(+ ["def _():", textwrap.indent(code, " ")]+ )+ else:+ wrapped[key] = code++ wrapped_result = await ruff(+ wrapped,"format","--line-length",str(self.line_length),stdin_filename=stdin_filename,)+ # Unwrap: use the AST to extract the function body, mirroring the+ # parse mechanism in marimo/_ast/parse.py (Extractor + fixed_dedent).+ result: CellCodes = {}+ for key, code in codes.items():+ if key in skip_wrap:+ result[key] = wrapped_result.get(key, code)+ elif key in wrapped_result:+ result[key] = unwrap_cell_body(wrapped_result[key])++ return result+class BlackFormatter(Formatter):async def format(diff --git a/tests/_utils/test_formatter.py b/tests/_utils/test_formatter.pyindex 92565a3901..b8a51d8118 100644--- a/tests/_utils/test_formatter.py+++ b/tests/_utils/test_formatter.py@@ -127,30 +127,35 @@ class TestRuffFormatter:async def test_ruff_formatter_calls_ruff_function(self, mock_ruff: MagicMock) -> None:- """Test RuffFormatter calls the ruff function with correct arguments."""- mock_ruff.return_value = {"cell1": "formatted_code"}+ """Test RuffFormatter wraps cell code before formatting."""+ import textwrap+ mock_ruff.return_value = {"cell1": "def _():\n x = 1"}formatter = RuffFormatter(line_length=100)codes: CellCodes = {"cell1": "x=1"}result = await formatter.format(codes)+ wrapped_codes: CellCodes = {+ "cell1": "def _():\n" + textwrap.indent("x=1", " ")+ }mock_ruff.assert_called_once_with(- codes,+ wrapped_codes,"format","--line-length","100",stdin_filename=None,)- assert result == {"cell1": "formatted_code"}+ assert result == {"cell1": "x = 1"}@patch("marimo._utils.formatter.ruff")async def test_ruff_formatter_passes_stdin_filename(self, mock_ruff: MagicMock) -> None:"""Test RuffFormatter forwards notebook filename for config discovery."""- mock_ruff.return_value = {"cell1": "formatted_code"}+ import textwrap+ mock_ruff.return_value = {"cell1": "def _():\n x = 1"}formatter = RuffFormatter(line_length=100)codes: CellCodes = {"cell1": "x=1"}@@ -158,14 +163,80 @@ class TestRuffFormatter:codes, stdin_filename="/tmp/notebook.py")+ wrapped_codes: CellCodes = {+ "cell1": "def _():\n" + textwrap.indent("x=1", " ")+ }mock_ruff.assert_called_once_with(- codes,+ wrapped_codes,"format","--line-length","100",stdin_filename="/tmp/notebook.py",)- assert result == {"cell1": "formatted_code"}+ assert result == {"cell1": "x = 1"}++ @patch("marimo._utils.formatter.ruff")+ async def test_ruff_formatter_nested_function_single_blank_line(+ self, mock_ruff: MagicMock+ ) -> None:+ """Regression test for #9848: nested functions get 1 blank line (function+ scope), not 2 (file scope). The wrap/unwrap logic ensures ruff sees cell+ code as a function body, not a module."""+ cell_code = "x = 3\ndef _foo():\n return x + 1\nprint(_foo())"+ # Ruff formats the wrapped cell with 1 blank line around the nested def+ wrapped_formatted = "def _():\n x = 3\n\n def _foo():\n return x + 1\n\n print(_foo())"+ mock_ruff.return_value = {"cell1": wrapped_formatted}++ formatter = RuffFormatter(line_length=88)+ result = await formatter.format({"cell1": cell_code})++ # Unwrapped result must have exactly 1 blank line before and after _foo,+ # not 2 (which file-scope ruff would produce without the wrap).+ output = result["cell1"]+ assert (+ output == "x = 3\n\ndef _foo():\n return x + 1\n\nprint(_foo())"+ )+ assert "\n\n\n" not in output++ @patch("marimo._utils.formatter.ruff")+ async def test_ruff_formatter_app_function_cell(+ self, mock_ruff: MagicMock+ ) -> None:+ """Regression test: @app.function cells (top-level function definitions)+ are wrapped before formatting so ruff applies E301 (nested defs get+ 1 blank line), not E302 (top-level defs get 2 blank lines)."""+ cell_code = 'def foo():\n """Something here"""'+ wrapped_formatted = (+ 'def _():\n def foo():\n """Something here"""'+ )+ mock_ruff.return_value = {"cell1": wrapped_formatted}++ formatter = RuffFormatter(line_length=88)+ result = await formatter.format({"cell1": cell_code})++ assert result == {"cell1": 'def foo():\n """Something here"""'}++ @patch("marimo._utils.formatter.ruff")+ async def test_ruff_formatter_comment_only_cell(+ self, mock_ruff: MagicMock+ ) -> None:+ """Comment-only cells are left unwrapped (def _(): # comment is a+ syntax error) and passed through ruff unchanged."""+ cell_code = "# just a comment"+ mock_ruff.return_value = {"cell1": cell_code}++ formatter = RuffFormatter(line_length=88)+ result = await formatter.format({"cell1": cell_code})++ # ruff receives the comment unwrapped, not inside def _():+ mock_ruff.assert_called_once_with(+ {"cell1": cell_code},+ "format",+ "--line-length",+ "88",+ stdin_filename=None,+ )+ assert result == {"cell1": cell_code}@patch("marimo._utils.formatter.ruff")async def test_ruff_formatter_propagates_exceptions(