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: handle by mro types from upstream libs (#6539)

Myles Scolnick004c2d58e825
diff --git a/marimo/_output/formatting.py b/marimo/_output/formatting.py
index e24bc8ab4c..447bd915ca 100644
--- a/marimo/_output/formatting.py
+++ b/marimo/_output/formatting.py
@@ -67,7 +67,16 @@ class FormatterRegistry:
return None
# Search for formatters in the object's type hierarchy
- for t in top_level_type.mro():
+ try:
+ mro_list = top_level_type.mro()
+ except BaseException as e: # noqa: E722
+ # Some exotic metaclasses or broken types may raise when calling mro
+ LOGGER.warning(
+ "Failed to read MRO for type %s: %s", top_level_type, str(e)
+ )
+ return None
+
+ for t in mro_list:
if t in self.formatters:
formatter = self.formatters[t]
# Add to formatters dict to avoid re-searching
diff --git a/tests/_output/test_formatter_registry.py b/tests/_output/test_formatter_registry.py
index 8476b89c50..696409dc03 100644
--- a/tests/_output/test_formatter_registry.py
+++ b/tests/_output/test_formatter_registry.py
@@ -281,3 +281,24 @@ class TestFormatterRegistry:
assert registry.get_formatter(type(value)) is None
assert registry.get_formatter(int) is None
assert registry.get_formatter(type(int)) is None
+
+ def test_get_formatter_handles_broken_mro(self):
+ """Ensure registry gracefully handles types with broken mro()."""
+ registry = FormatterRegistry()
+
+ class Broken:
+ pass
+
+ # Shadow mro with a plain function so calling it raises a TypeError
+ # because no implicit self/cls is passed for functions on classes
+ def bad_mro(_self):
+ del _self
+ raise TypeError("broken mro")
+
+ Broken.mro = bad_mro # type: ignore[attr-defined]
+
+ obj = Broken()
+
+ # Should not raise, and should return None when MRO cannot be read
+ formatter = registry.get_formatter(obj)
+ assert formatter is None