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: preserve function return type in live docs panel (#8210) (#8263)

Myles Scolnick3828e5f9c110
diff --git a/marimo/_utils/format_signature.py b/marimo/_utils/format_signature.py
index a3ca25e080..1a3dc3b525 100644
--- a/marimo/_utils/format_signature.py
+++ b/marimo/_utils/format_signature.py
@@ -13,10 +13,8 @@ def format_signature(prefix: str, signature_text: str, width: int = 39) -> str:
except ModuleNotFoundError:
pass
- if (
- black_installed
- and prefix.startswith("class")
- or prefix.startswith("def")
+ if black_installed and (
+ prefix.startswith("class") or prefix.startswith("def")
):
# Coarse try-except because we're using internal black APIs;
# many other well-established projects use these APIs, which
@@ -30,9 +28,18 @@ def format_signature(prefix: str, signature_text: str, width: int = 39) -> str:
)
# replace "def " with actual prefix
formatted = prefix + formatted[4:]
- # remove ":\n"...\n", which was inserted to make signature have
- # valid syntax
- return ("\n".join(formatted.split("\n")[:-2]))[:-1]
+ # remove ": ..." body that was added to make valid syntax.
+ # Black may keep ": ..." on the same line as the closing
+ # paren (e.g. ") -> Html: ...") or put "..." on its own
+ # indented line.
+ formatted = formatted.rstrip()
+ if formatted.endswith(": ..."):
+ formatted = formatted[:-5]
+ elif formatted.endswith("..."):
+ formatted = formatted.rsplit("\n", 1)[0].rstrip()
+ if formatted.endswith(":"):
+ formatted = formatted[:-1]
+ return formatted
except Exception:
pass
diff --git a/tests/_server/api/endpoints/test_resume_session.py b/tests/_server/api/endpoints/test_resume_session.py
index c9714a4304..b487801df1 100644
--- a/tests/_server/api/endpoints/test_resume_session.py
+++ b/tests/_server/api/endpoints/test_resume_session.py
@@ -5,6 +5,8 @@ import time
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, Optional
+import pytest
+
from marimo._config.manager import UserConfigManager
from marimo._messaging.msgspec_encoder import asdict
from marimo._messaging.notification import (
@@ -295,6 +297,7 @@ def test_restart_session(client: TestClient) -> None:
# Shutdown the kernel
def test_resume_session_with_watch(client: TestClient) -> None:
session_manager = get_session_manager(client)
session_manager.watch = True
diff --git a/tests/_utils/test_format_signature.py b/tests/_utils/test_format_signature.py
new file mode 100644
index 0000000000..50ab4e530c
--- /dev/null
+++ b/tests/_utils/test_format_signature.py
@@ -0,0 +1,55 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+import pytest
+
+from marimo._utils.format_signature import format_signature
+
+
+class TestFormatSignature:
+ @pytest.mark.parametrize(
+ ("sig", "expected_return"),
+ [
+ # Short (fits on 1 line)
+ ("f() -> int", "int"),
+ ("f() -> str", "str"),
+ ("f() -> list[int]", "list[int]"),
+ # Medium (multiline)
+ ("f(x: int) -> Optional[str]", "Optional[str]"),
+ ("f(x: int, y: str) -> dict[str, Any]", "dict[str, Any]"),
+ ("medium_func(x: int, y: str) -> float", "float"),
+ ("func(x: int, y: str = None) -> bool", "bool"),
+ # Long (like hstack)
+ (
+ "hstack(items: Sequence[object], *, "
+ 'justify: str = "space-between", '
+ "align: str = None, "
+ "wrap: bool = False, "
+ "gap: float = 0.5, "
+ "widths: str = None) -> Html",
+ "Html",
+ ),
+ ],
+ )
+ def test_return_type_preserved(
+ self, sig: str, expected_return: str
+ ) -> None:
+ result = format_signature("def ", sig)
+ assert "->" in result
+ assert result.strip().endswith(expected_return)
+
+ def test_no_return_type(self) -> None:
+ result = format_signature("def ", "func(x: int, y: str)")
+ assert "->" not in result
+ assert "func" in result
+
+ def test_class_prefix(self) -> None:
+ result = format_signature("class ", "MyClass(x: int, y: str)")
+ assert result.startswith("class ")
+ assert "MyClass" in result
+
+ def test_default_values_not_truncated(self) -> None:
+ result = format_signature(
+ "def ", "func(x: int, y: str = None) -> bool"
+ )
+ assert "None" in result