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.

make `_format_plan` respect format_on_save; format enabled/disabled unit tests (#9380)

ouatu-rob3fa14cea241
diff --git a/marimo/_code_mode/_context.py b/marimo/_code_mode/_context.py
index 98bb8e5f83..d710f36de7 100644
--- a/marimo/_code_mode/_context.py
+++ b/marimo/_code_mode/_context.py
@@ -1609,7 +1609,10 @@ class AsyncCodeModeContext:
# ------------------------------------------------------------------
async def _format_plan(self, plan: list[_PlanEntry]) -> list[_PlanEntry]:
- """Format new/changed code in the plan with the default formatter."""
+ """Format new/changed code when save-time formatting is enabled."""
+ if not self._kernel.user_config["save"]["format_on_save"]:
+ return plan
+
existing_code = {cell.id: cell.code for cell in self._document.cells}
to_format: dict[CellId_t, str] = {}
diff --git a/tests/_code_mode/test_context.py b/tests/_code_mode/test_context.py
index f414ab04ee..5cc0a03ef2 100644
--- a/tests/_code_mode/test_context.py
+++ b/tests/_code_mode/test_context.py
@@ -327,6 +327,42 @@ class TestUpdateCell:
]
)
+ async def test_update_code_skips_formatting_when_disabled(
+ self, k: Kernel
+ ) -> None:
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
+
+ with (
+ patch.dict(k.user_config["save"], {"format_on_save": False}),
+ patch(
+ "marimo._code_mode._context.DefaultFormatter.format",
+ new_callable=AsyncMock,
+ ) as mock_format,
+ ):
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ nb.edit_cell("0", code="x= 42")
+
+ mock_format.assert_not_awaited()
+ assert _graph_codes(k) == {"0": "x= 42"}
+
+ async def test_update_code_formats_when_enabled(self, k: Kernel) -> None:
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
+
+ with (
+ patch.dict(k.user_config["save"], {"format_on_save": True}),
+ patch(
+ "marimo._code_mode._context.DefaultFormatter.format",
+ new=AsyncMock(return_value={"0": "x = 42"}),
+ ) as mock_format,
+ ):
+ with _ctx(k) as ctx:
+ async with ctx as nb:
+ nb.edit_cell("0", code="x= 42")
+
+ mock_format.assert_awaited_once()
+ assert _graph_codes(k) == {"0": "x = 42"}
+
class TestCombined:
async def test_delete_and_add(self, k: Kernel) -> None: