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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
refactor: pull out variable logic from notification schemas (#7596)
Myles Scolnickc2aa7d469d58
diff --git a/marimo/_messaging/notification.py b/marimo/_messaging/notification.pyindex 98119317e3..6115d5a73b 100644--- a/marimo/_messaging/notification.py+++ b/marimo/_messaging/notification.py@@ -7,7 +7,6 @@ Messages that the kernel sends to the frontend.from __future__ import annotationsimport time-from types import ModuleTypefrom typing import (Any,ClassVar,@@ -31,12 +30,7 @@ from marimo._dependencies.dependencies import DependencyManagerfrom marimo._messaging.cell_output import CellOutputfrom marimo._messaging.completion_option import CompletionOptionfrom marimo._messaging.context import RUN_ID_CTX, RunId_t-from marimo._messaging.serde import serialize_kernel_message-from marimo._messaging.types import KernelMessage-from marimo._messaging.variables import get_variable_preview-from marimo._output.hypertext import Htmlfrom marimo._plugins.core.web_component import JSONType-from marimo._plugins.ui._core.ui_element import UIElementfrom marimo._runtime.layout.layout import LayoutConfigfrom marimo._secrets.models import SecretKeysWithProviderfrom marimo._sql.parse import SqlCatalogCheckResult, SqlParseResult@@ -118,27 +112,6 @@ class FunctionCallResultNotification(Notification, tag="function-call-result"):return_value: JSONTypestatus: HumanReadableStatus- def serialize(self) -> KernelMessage:- try:- return serialize_kernel_message(self)- except Exception as e:- LOGGER.exception(- "Error serializing function call result %s: %s",- self.__class__.__name__,- e,- )- return serialize_kernel_message(- FunctionCallResultNotification(- function_call_id=self.function_call_id,- return_value=None,- status=HumanReadableStatus(- code="error",- title="Error calling function",- message="Failed to serialize function call result",- ),- )- )-class RemoveUIElementsNotification(Notification, tag="remove-ui-elements"):"""Invalidate UI elements for a given cell."""@@ -285,62 +258,6 @@ class VariableValue(BaseStruct):value: Optional[str]datatype: Optional[str]- @staticmethod- def create(- name: str, value: object, datatype: Optional[str] = None- ) -> VariableValue:- """Factory method to create a VariableValue from an object."""- # Defensively try-catch attribute accesses, which could raise- # exceptions- # If datatype is already defined, don't try to infer it- if datatype is None:- try:- computed_datatype = (- type(value).__name__ if value is not None else None- )- except Exception:- computed_datatype = datatype- else:- computed_datatype = datatype-- try:- formatted_value = VariableValue._format_value_static(value)- except Exception:- formatted_value = None-- return VariableValue(- name=name, value=formatted_value, datatype=computed_datatype- )-- @staticmethod- def _stringify_static(value: object) -> str:- MAX_STR_LEN = 50-- if isinstance(value, str):- if len(value) > MAX_STR_LEN:- return value[:MAX_STR_LEN]- return value-- try:- # str(value) can be slow for large objects- # or lead to large memory spikes- return get_variable_preview(value, max_str_len=MAX_STR_LEN)- except BaseException:- # Catch-all: some libraries like Polars have bugs and raise- # BaseExceptions, which shouldn't crash the kernel- return "<UNKNOWN>"-- @staticmethod- def _format_value_static(value: object) -> str:- resolved = value- if isinstance(value, UIElement):- resolved = value.value- elif isinstance(value, Html):- resolved = value.text- elif isinstance(value, ModuleType):- resolved = value.__name__- return VariableValue._stringify_static(resolved)-class VariablesNotification(Notification, tag="variables"):"""List of variable declarations."""diff --git a/marimo/_messaging/variables.py b/marimo/_messaging/variables.pyindex c554793c44..86f2ffaf17 100644--- a/marimo/_messaging/variables.py+++ b/marimo/_messaging/variables.py@@ -1,8 +1,10 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations+from types import ModuleTypefrom typing import TYPE_CHECKING, Any, Union+from marimo._messaging.notification import VariableValuefrom marimo._plugins.ui._impl.tables.utils import get_table_manager_or_noneif TYPE_CHECKING:@@ -166,3 +168,79 @@ def get_variable_preview(except Exception as e:return f"<error previewing {type_name}: {str(e)}>"+++def _stringify_variable_value(value: object) -> str:+ """Convert a value to its string representation.++ Limits string length and handles objects that may have expensive __str__.+ """+ MAX_STR_LEN = 50++ if isinstance(value, str):+ if len(value) > MAX_STR_LEN:+ return value[:MAX_STR_LEN]+ return value++ try:+ # str(value) can be slow for large objects+ # or lead to large memory spikes+ return get_variable_preview(value, max_str_len=MAX_STR_LEN)+ except BaseException:+ # Catch-all: some libraries like Polars have bugs and raise+ # BaseExceptions, which shouldn't crash the kernel+ return "<UNKNOWN>"+++def _format_variable_value(value: object) -> str:+ """Format a variable value for display.++ Handles special types like UIElement, Html, and ModuleType.+ """++ from marimo._output.hypertext import Html+ from marimo._plugins.ui._core.ui_element import UIElement++ resolved = value+ if isinstance(value, UIElement):+ resolved = value.value+ elif isinstance(value, Html):+ resolved = value.text+ elif isinstance(value, ModuleType):+ resolved = value.__name__+ return _stringify_variable_value(resolved)+++def create_variable_value(+ name: str, value: object, datatype: str | None = None+) -> VariableValue:+ """Factory function to create a VariableValue from an object.++ Args:+ name: Variable name+ value: Variable value (any Python object)+ datatype: Optional datatype override. If None, will be inferred.++ Returns:+ VariableValue with formatted value and datatype+ """+ # Defensively try-catch attribute accesses, which could raise exceptions+ # If datatype is already defined, don't try to infer it+ if datatype is None:+ try:+ computed_datatype = (+ type(value).__name__ if value is not None else None+ )+ except Exception:+ computed_datatype = datatype+ else:+ computed_datatype = datatype++ try:+ formatted_value = _format_variable_value(value)+ except Exception:+ formatted_value = None++ return VariableValue(+ name=name, value=formatted_value, datatype=computed_datatype+ )diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.pyindex 388f9c6bf9..a8adb49550 100644--- a/marimo/_runtime/runner/hooks_post_execution.py+++ b/marimo/_runtime/runner/hooks_post_execution.py@@ -22,7 +22,6 @@ from marimo._messaging.errors import (from marimo._messaging.notification import (DatasetsNotification,DataSourceConnectionsNotification,- VariableValue,VariableValuesNotification,)from marimo._messaging.notification_utils import (@@ -30,6 +29,7 @@ from marimo._messaging.notification_utils import (broadcast_notification,)from marimo._messaging.tracebacks import write_traceback+from marimo._messaging.variables import create_variable_valuefrom marimo._output import formattingfrom marimo._plugins.ui._core.ui_element import UIElementfrom marimo._runtime.context.kernel_context import KernelRuntimeContext@@ -123,7 +123,7 @@ def _broadcast_variables(del run_resultvalues = [- VariableValue.create(+ create_variable_value(name=variable,value=(runner.glbls[variable] if variable in runner.glbls else Nonediff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 09a68cc401..119e0a9ba3 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -95,6 +95,7 @@ from marimo._messaging.types import (Stdout,Stream,)+from marimo._messaging.variables import create_variable_valuefrom marimo._output.rich_help import mddocfrom marimo._plugins.core.web_component import JSONTypefrom marimo._plugins.ui._core.ui_element import MarimoConvertValueException@@ -1915,7 +1916,7 @@ class Kernel:for name in bound_names:# TODO update variable values even for namespaces? lenses? etcvariable_values.append(- VariableValue.create(name=name, value=value)+ create_variable_value(name=name, value=value))try:# subtracting self.graph.definitions[name]: never rerun thediff --git a/tests/_messaging/test_notifications.py b/tests/_messaging/test_notifications.pyindex 5621f18c47..ece05c300b 100644--- a/tests/_messaging/test_notifications.py+++ b/tests/_messaging/test_notifications.py@@ -9,12 +9,12 @@ from marimo._messaging.notification import (InstallingPackageAlertNotification,StartupLogsNotification,UIElementMessageNotification,- VariableValue,)from marimo._messaging.notification_utils import (CellNotificationUtils,broadcast_notification,)+from marimo._messaging.variables import create_variable_valuefrom marimo._output.hypertext import Htmlfrom marimo._plugins.ui._impl.input import sliderfrom marimo._types.ids import CellId_t@@ -23,7 +23,7 @@ from tests._messaging.mocks import MockStreamdef test_value_ui_element() -> None:- variable_value = VariableValue.create(+ variable_value = create_variable_value(name="s", value=slider(1, 10, value=5))assert variable_value.datatype == "slider"@@ -32,7 +32,7 @@ def test_value_ui_element() -> None:def test_value_html() -> None:h = Html("<span></span>")- variable_value = VariableValue.create(name="h", value=h)+ variable_value = create_variable_value(name="h", value=h)assert variable_value.datatype == "Html"assert variable_value.value == h.text@@ -42,7 +42,7 @@ def test_variable_value_broken_str() -> None:def __str__(self) -> str:raise BaseException # noqa: TRY002- variable_value = VariableValue.create(name="o", value=Broken())+ variable_value = create_variable_value(name="o", value=Broken())assert variable_value.datatype == "Broken"assert variable_value.value is not Noneassert variable_value.value.startswith("<Broken object at")diff --git a/tests/_messaging/test_variables.py b/tests/_messaging/test_variables.pyindex 5cc9b2a533..9e00850ec9 100644--- a/tests/_messaging/test_variables.py+++ b/tests/_messaging/test_variables.py@@ -6,7 +6,14 @@ from typing import Anyimport pytestfrom marimo._dependencies.dependencies import DependencyManager-from marimo._messaging.variables import get_variable_preview+from marimo._messaging.variables import (+ _format_variable_value,+ _stringify_variable_value,+ create_variable_value,+ get_variable_preview,+)+from marimo._output.hypertext import Html+from marimo._plugins.ui._impl.input import sliderfrom tests._data.mocks import create_dataframes@@ -219,3 +226,224 @@ def test_get_variable_preview_dataframe(df: Any) -> None:f"Memory increased by {mem_diff_mb}MB during preview")assert "2 columns" in preview+++class TestStringifyVariableValue:+ """Test _stringify_variable_value function."""++ def test_short_string(self) -> None:+ result = _stringify_variable_value("hello")+ assert result == "hello"++ def test_long_string_truncated(self) -> None:+ long_str = "a" * 100+ result = _stringify_variable_value(long_str)+ assert len(result) == 50+ assert result == "a" * 50++ def test_integer(self) -> None:+ result = _stringify_variable_value(42)+ assert result == "42"++ def test_list(self) -> None:+ result = _stringify_variable_value([1, 2, 3])+ assert "[1, 2, 3]" in result++ def test_dict(self) -> None:+ result = _stringify_variable_value({"a": 1, "b": 2})+ assert "a" in result+ assert "1" in result++ def test_broken_str(self) -> None:+ """Test object that raises BaseException in __str__."""++ class Broken:+ def __str__(self) -> str:+ raise BaseException("boom") # noqa: TRY002++ result = _stringify_variable_value(Broken())+ # get_variable_preview falls back to repr() which returns "<Broken object at ...>"+ assert result.startswith("<Broken object at")++ def test_none(self) -> None:+ result = _stringify_variable_value(None)+ assert result == "None"++ def test_float(self) -> None:+ result = _stringify_variable_value(3.14159)+ assert "3.14" in result++ def test_bool(self) -> None:+ assert _stringify_variable_value(True) == "True"+ assert _stringify_variable_value(False) == "False"++ def test_tuple(self) -> None:+ result = _stringify_variable_value((1, 2, 3))+ assert "(1, 2, 3)" in result+++class TestFormatVariableValue:+ """Test _format_variable_value function."""++ def test_ui_element(self) -> None:+ """Test UIElement extracts .value attribute."""+ s = slider(1, 10, value=5)+ result = _format_variable_value(s)+ assert result == "5"++ def test_html(self) -> None:+ """Test Html extracts .text attribute."""+ h = Html("<span>hello</span>")+ result = _format_variable_value(h)+ # Html.text returns the raw HTML string+ assert result == h.text+ assert result == "<span>hello</span>"++ def test_module(self) -> None:+ """Test ModuleType extracts .__name__."""+ import sys++ result = _format_variable_value(sys)+ assert result == "sys"++ def test_regular_value(self) -> None:+ """Test regular value passes through to stringify."""+ result = _format_variable_value(42)+ assert result == "42"++ def test_string(self) -> None:+ """Test string value."""+ result = _format_variable_value("hello")+ assert result == "hello"+++class TestCreateVariableValue:+ """Test create_variable_value function."""++ def test_integer(self) -> None:+ vv = create_variable_value("x", 42)+ assert vv.name == "x"+ assert vv.value == "42"+ assert vv.datatype == "int"++ def test_string(self) -> None:+ vv = create_variable_value("s", "hello")+ assert vv.name == "s"+ assert vv.value == "hello"+ assert vv.datatype == "str"++ def test_none(self) -> None:+ vv = create_variable_value("n", None)+ assert vv.name == "n"+ assert vv.value == "None"+ assert vv.datatype is None++ def test_list(self) -> None:+ vv = create_variable_value("lst", [1, 2, 3])+ assert vv.name == "lst"+ assert "[1, 2, 3]" in vv.value+ assert vv.datatype == "list"++ def test_ui_element(self) -> None:+ """Test UIElement value extraction."""+ s = slider(1, 10, value=7)+ vv = create_variable_value("slider", s)+ assert vv.name == "slider"+ assert vv.value == "7"+ assert vv.datatype == "slider"++ def test_html(self) -> None:+ """Test Html text extraction."""+ h = Html("<span>content</span>")+ vv = create_variable_value("html", h)+ assert vv.name == "html"+ # Html.text returns the raw HTML string+ assert vv.value == h.text+ assert vv.value == "<span>content</span>"+ assert vv.datatype == "Html"++ def test_module_type(self) -> None:+ """Test ModuleType name extraction."""+ import os++ vv = create_variable_value("os_module", os)+ assert vv.name == "os_module"+ assert vv.value == "os"+ assert vv.datatype == "module"++ def test_custom_datatype(self) -> None:+ """Test overriding datatype."""+ vv = create_variable_value("x", 42, datatype="custom_int")+ assert vv.name == "x"+ assert vv.value == "42"+ assert vv.datatype == "custom_int"++ def test_exception_in_type_name(self) -> None:+ """Test handling exception when getting type name."""++ class BrokenType:+ @property+ def __class__(self):+ raise Exception("boom") # noqa: TRY002++ # type() builtin doesn't use the __class__ property, so this doesn't raise+ vv = create_variable_value("broken", BrokenType())+ assert vv.name == "broken"+ # type() still works and returns the actual type+ assert vv.datatype == "BrokenType"++ def test_exception_in_format(self) -> None:+ """Test handling exception during formatting."""++ class BrokenStr:+ def __str__(self) -> str:+ raise BaseException("boom") # noqa: TRY002++ vv = create_variable_value("broken", BrokenStr())+ assert vv.name == "broken"+ # get_variable_preview falls back to repr() which returns "<BrokenStr object at ...>"+ assert vv.value.startswith("<BrokenStr object at")++ def test_long_string_truncation(self) -> None:+ """Test that long strings are truncated."""+ long_str = "x" * 100+ vv = create_variable_value("long", long_str)+ assert vv.name == "long"+ assert len(vv.value) == 50+ assert vv.value == "x" * 50+ assert vv.datatype == "str"++ def test_large_collection(self) -> None:+ """Test handling of large collections."""+ large_list = list(range(1000))+ vv = create_variable_value("big", large_list)+ assert vv.name == "big"+ # Should have preview with ellipsis+ assert "..." in vv.value+ assert vv.datatype == "list"++ def test_dict_value(self) -> None:+ """Test dictionary value."""+ vv = create_variable_value("d", {"a": 1, "b": 2})+ assert vv.name == "d"+ assert "a" in vv.value+ assert vv.datatype == "dict"++ def test_float_value(self) -> None:+ """Test float value."""+ vv = create_variable_value("pi", 3.14159)+ assert vv.name == "pi"+ assert "3.14" in vv.value+ assert vv.datatype == "float"++ def test_bool_values(self) -> None:+ """Test boolean values."""+ vv_true = create_variable_value("t", True)+ assert vv_true.name == "t"+ assert vv_true.value == "True"+ assert vv_true.datatype == "bool"++ vv_false = create_variable_value("f", False)+ assert vv_false.name == "f"+ assert vv_false.value == "False"+ assert vv_false.datatype == "bool"diff --git a/tests/_server/session/test_session_view.py b/tests/_server/session/test_session_view.pyindex 5611acd938..b674be2b93 100644--- a/tests/_server/session/test_session_view.py+++ b/tests/_server/session/test_session_view.py@@ -30,8 +30,9 @@ from marimo._messaging.notification import (VariablesNotification,VariableValue,VariableValuesNotification,- serialize_kernel_message,)+from marimo._messaging.serde import serialize_kernel_message+from marimo._messaging.variables import create_variable_valuefrom marimo._runtime.commands import (CreateNotebookCommand,ExecuteCellCommand,@@ -124,8 +125,8 @@ def test_session_view_variable_values(session_view: SessionView) -> None:# Create VariableValues operationvariable_values_op = VariableValuesNotification(variables=[- VariableValue.create(name="var1", value=1),- VariableValue.create(name="var2", value="hello"),+ create_variable_value(name="var1", value=1),+ create_variable_value(name="var2", value="hello"),])session_view.add_notification(variable_values_op)@@ -259,7 +260,7 @@ def test_last_run_code(session_view: SessionView) -> None:def test_serialize_parse_variable_value() -> None:- original = VariableValue.create(name="var1", value=1)+ original = create_variable_value(name="var1", value=1)serialized = serialize(original)assert serialized == {"datatype": "int", "name": "var1", "value": "1"}parsed = parse_raw(serialized, VariableValue)@@ -285,8 +286,8 @@ def test_add_variables(session_view: SessionView) -> None:serialize_kernel_message(VariableValuesNotification(variables=[- VariableValue.create(name="var1", value=1),- VariableValue.create(name="var2", value="hello"),+ create_variable_value(name="var1", value=1),+ create_variable_value(name="var2", value="hello"),]))