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.

tests: add tests for try-format (#3643)

Myles Scolnickdf67b3e21cf7
diff --git a/tests/_output/test_try_format.py b/tests/_output/test_try_format.py
new file mode 100644
index 0000000000..9e1a5c192f
--- /dev/null
+++ b/tests/_output/test_try_format.py
@@ -0,0 +1,256 @@
+from __future__ import annotations
+
+from dataclasses import dataclass
+
+import pytest
+
+from marimo._dependencies.dependencies import DependencyManager
+from marimo._messaging.mimetypes import KnownMimeType
+from marimo._output.formatting import (
+ FormattedOutput,
+ Plain,
+ formatter,
+ opinionated_formatter,
+ try_format,
+)
+
+
+# Test class with _mime_ protocol
+class MimeObject:
+ def _mime_(self):
+ return ("text/html", "<p>mime test</p>")
+
+
+# Test class with _display_ protocol
+class DisplayObject:
+ def _display_(self):
+ return MimeObject()
+
+
+# Test class for basic formatting
+@dataclass
+class BasicObject:
+ value: str
+
+
+# Register a formatter for BasicObject
+@formatter(BasicObject)
+def format_basic(obj: BasicObject) -> tuple[KnownMimeType, str]:
+ return ("text/html", f"<span>{obj.value}</span>")
+
+
+# Test class for opinionated formatting
+@dataclass
+class OpinionatedObject:
+ value: str
+
+
+# Register an opinionated formatter
+@opinionated_formatter(OpinionatedObject)
+def format_opinionated(obj: OpinionatedObject) -> tuple[KnownMimeType, str]:
+ return ("text/html", f"<div>{obj.value}</div>")
+
+
+# Test class that raises error during formatting
+@dataclass
+class ErrorObject:
+ value: str
+
+
+@formatter(ErrorObject)
+def format_error(_: ErrorObject) -> tuple[KnownMimeType, str]:
+ raise ValueError("Formatting error")
+
+
+def is_plain_text(output: FormattedOutput) -> bool:
+ assert output.mimetype == "text/plain"
+ assert output.traceback is None
+ assert output.exception is None
+ return True
+
+
+def is_html(output: FormattedOutput) -> bool:
+ assert output.mimetype == "text/html"
+ assert output.traceback is None
+ assert output.exception is None
+ return True
+
+
+def test_primitives():
+ # 1 and "1" are printed the same
+ assert try_format(1).data == "<pre style='font-size: 12px'>1</pre>"
+ assert try_format("1").data == "<pre style='font-size: 12px'>1</pre>"
+ assert (
+ try_format("hello").data == "<pre style='font-size: 12px'>hello</pre>"
+ )
+ assert try_format(None).data == ""
+ # True and 'True' are printed the same
+ assert try_format(True).data == "<pre style='font-size: 12px'>True</pre>"
+ assert try_format("True").data == "<pre style='font-size: 12px'>True</pre>"
+ assert try_format(False).data == "<pre style='font-size: 12px'>False</pre>"
+ assert try_format(1.0).data == "<pre style='font-size: 12px'>1.0</pre>"
+ assert (
+ try_format(1.0 + 1.0j).data
+ == "<pre style='font-size: 12px'>(1+1j)</pre>"
+ )
+ assert try_format([1, 2, 3]).data == "[1, 2, 3]"
+ assert try_format({"a": 1, "b": 2}).data == '{"a": 1, "b": 2}'
+ assert (
+ try_format(set([1, 2, 3])).data
+ == "<pre style='font-size: 12px'>{1, 2, 3}</pre>"
+ )
+
+
+def test_none_value():
+ """Test formatting of None value."""
+ result = try_format(None)
+ assert is_plain_text(result)
+ assert result.data == ""
+
+
+def test_basic_formatter():
+ """Test basic formatter registration and usage."""
+ obj = BasicObject("test")
+ result = try_format(obj)
+ assert is_html(result)
+ assert result.data == "<span>test</span>"
+
+
+def test_mime_protocol():
+ """Test object implementing _mime_ protocol."""
+ obj = MimeObject()
+ result = try_format(obj)
+ assert is_html(result)
+ assert result.data == "<p>mime test</p>"
+
+
+def test_display_protocol():
+ """Test object implementing _display_ protocol."""
+ obj = DisplayObject()
+ result = try_format(obj)
+ assert is_html(result)
+ assert result.data == "<p>mime test</p>"
+
+
+def test_error_handling():
+ """Test error handling during formatting."""
+ obj = ErrorObject("test")
+ result = try_format(obj)
+ assert result.mimetype == "text/plain"
+ assert result.data == ""
+ assert result.traceback is not None
+ assert isinstance(result.exception, ValueError)
+ assert str(result.exception) == "Formatting error"
+
+
+def test_plain_object():
+ """Test Plain wrapper to opt out of opinionated formatting."""
+ obj = Plain(OpinionatedObject("test"))
+ result = try_format(obj)
+ # Should fall back to string representation since we opted out of opinionated formatter
+ assert is_html(result)
+ assert result.data.startswith(
+ "<pre style='font-size: 12px'>&lt;marimo._output.formatting.Plain object"
+ )
+
+
+def test_opinionated_formatter():
+ """Test opinionated formatter with and without include_opinionated flag."""
+ obj = OpinionatedObject("test")
+
+ # With opinionated formatting (default)
+ result = try_format(obj)
+ assert is_html(result)
+ assert result.data == "<div>test</div>"
+
+ # Without opinionated formatting
+ result = try_format(obj, include_opinionated=False)
+ assert is_html(result)
+ assert "test" in result.data.lower()
+
+
+def test_fallback_string_representation():
+ """Test fallback to string representation for objects without formatters."""
+
+ class NoFormatter:
+ def __str__(self):
+ return "no formatter"
+
+ obj = NoFormatter()
+ result = try_format(obj)
+ assert is_html(result)
+ assert "no formatter" in result.data.lower()
+
+
+def test_string_eval_error():
+ """Test handling of string evaluation errors."""
+
+ class BadString:
+ def __str__(self):
+ raise ValueError("Bad string")
+
+ obj = BadString()
+ result = try_format(obj)
+ assert result.mimetype == "text/plain"
+ assert result.data == ""
+ assert result.traceback is not None
+
+
+def test_str_vs_repr():
+ """Test that str is preferred over repr."""
+
+ class StrReprTest:
+ def __str__(self):
+ return "str_value"
+
+ def __repr__(self):
+ return "repr_value"
+
+ obj = StrReprTest()
+ result = try_format(obj)
+ assert is_html(result)
+ assert "str_value" in result.data.lower()
+ assert "repr_value" not in result.data.lower()
+
+
+def test_repr_fallback():
+ """Test that repr is used when str not available."""
+
+ class ReprOnlyTest:
+ def __repr__(self):
+ return "repr_value"
+
+ obj = ReprOnlyTest()
+ result = try_format(obj)
+ assert is_html(result)
+ assert "repr_value" in result.data.lower()
+
+
+def test_str_error_repr_fallback():
+ """Test that repr is used when str raises error."""
+
+ class StrErrorTest:
+ def __str__(self):
+ raise ValueError("str error")
+
+ def __repr__(self):
+ return "repr_value"
+
+ obj = StrErrorTest()
+ result = try_format(obj)
+ assert result.mimetype == "text/plain"
+ assert result.data == ""
+ assert result.traceback is not None
+
+
+ not DependencyManager.numpy.has(), reason="numpy is not installed"
+)
+def test_numpy_array():
+ """Test formatting of numpy array."""
+ import numpy as np
+
+ obj = np.array([1, 2, 3])
+ result = try_format(obj)
+ assert is_html(result)
+ assert result.data == "<pre style='font-size: 12px'>[1 2 3]</pre>"