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: append .py extension to markdown notebooks for correct ruff formatting (#8786)

Daizu4625abdb641f
diff --git a/marimo/_server/api/endpoints/editing.py b/marimo/_server/api/endpoints/editing.py
index b52147cf00..79ebac6473 100644
--- a/marimo/_server/api/endpoints/editing.py
+++ b/marimo/_server/api/endpoints/editing.py
@@ -178,14 +178,13 @@ async def format_cell(request: Request) -> FormatResponse:
app_state = AppState(request)
body = await parse_request(request, cls=FormatCellsRequest)
formatter = DefaultFormatter(line_length=body.line_length)
+ filename = app_state.require_current_session().app_file_manager.path
+ if filename and filename.endswith((".md", ".qmd")):
+ filename = f"{filename}.py"
try:
- return FormatResponse(
- codes=await formatter.format(
- body.codes,
- stdin_filename=app_state.require_current_session().app_file_manager.path,
- )
- )
+ codes = await formatter.format(body.codes, filename)
+ return FormatResponse(codes)
except ModuleNotFoundError:
# In multi-sandbox mode each kernel has its own venv, so installing
# ruff into the server wouldn't help the kernel. Just surface the
diff --git a/tests/_server/api/endpoints/test_editing.py b/tests/_server/api/endpoints/test_editing.py
index fe52f97138..2b133fa56f 100644
--- a/tests/_server/api/endpoints/test_editing.py
+++ b/tests/_server/api/endpoints/test_editing.py
@@ -90,27 +90,36 @@ def test_format_cell(client: TestClient) -> None:
def test_format_cell_passes_notebook_path_to_formatter(
client: TestClient,
) -> None:
- with patch(
- "marimo._server.api.endpoints.editing.DefaultFormatter.format",
- new=AsyncMock(return_value={"cell-123": "x = 1"}),
- ) as mock_format:
- response = client.post(
- "/api/kernel/format",
- headers=HEADERS,
- json={
- "codes": {"cell-123": "x=1"},
- "lineLength": 80,
- },
- )
-
- assert response.status_code == 200, response.text
- assert response.json() == {"codes": {"cell-123": "x = 1"}}
- assert mock_format.await_count == 1
-
- args, kwargs = mock_format.await_args
- assert {"cell-123": "x=1"} in args
- assert isinstance(kwargs["stdin_filename"], str)
- assert kwargs["stdin_filename"].endswith(".py")
+ # pytest.mark.parametrize doesn't work with @with_session
+ for suffix, filename in (
+ (".py", "notebook.py"),
+ (".md", "notebook.md.py"),
+ (".qmd", "notebook.qmd.py"),
+ ):
+ with (
+ patch(
+ "marimo._server.api.endpoints.editing.DefaultFormatter.format",
+ new=AsyncMock(return_value={"cell-123": "x = 1"}),
+ ) as mock_format,
+ patch(
+ "marimo._server.api.endpoints.editing.AppState.require_current_session"
+ ) as mock_require_session,
+ ):
+ mock_session = MagicMock()
+ mock_session.app_file_manager.path = f"notebook{suffix}"
+ mock_require_session.return_value = mock_session
+ response = client.post(
+ "/api/kernel/format",
+ headers=HEADERS,
+ json={
+ "codes": {"cell-123": "x=1"},
+ "lineLength": 80,
+ },
+ )
+
+ assert response.status_code == 200, response.text
+ assert response.json() == {"codes": {"cell-123": "x = 1"}}
+ mock_format.assert_awaited_once_with({"cell-123": "x=1"}, filename)
@with_session(SESSION_ID)