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
fix(output): render Enum members as str in JSON serializer (#9594)
Kiran Gadhave9b1f94ac6e8c
diff --git a/marimo/_messaging/msgspec_encoder.py b/marimo/_messaging/msgspec_encoder.pyindex 3eb2da5a01..d80e948c1d 100644--- a/marimo/_messaging/msgspec_encoder.py+++ b/marimo/_messaging/msgspec_encoder.py@@ -6,6 +6,7 @@ from __future__ import annotationsimport collectionsimport datetimeimport decimal+import enumimport fractionsimport uuidfrom math import isnan@@ -182,6 +183,12 @@ def enc_hook(obj: Any) -> Any:exc_info=True,)+ # Encode Enum members as their str form (e.g. "TestEnum.ONE"). Without+ # this, plain Enum members reach the __dict__ fallback and leak internals+ # (_value_, _name_, ...).+ if isinstance(obj, enum.Enum):+ return str(obj)+# Handle objects with __slots__# Check on type(obj) to avoid triggering __getattr__ on objects that# implement itdiff --git a/tests/_plugins/core/test_json_encoder.py b/tests/_plugins/core/test_json_encoder.pyindex 62b14fa627..60f33ee1a8 100644--- a/tests/_plugins/core/test_json_encoder.py+++ b/tests/_plugins/core/test_json_encoder.py@@ -441,6 +441,26 @@ def test_enum_encoding() -> None:assert encoded == "2"+def test_enum_via_sanitize_json_bigint() -> None:+ """The stdlib-json path used by table serializers must render Enum+ members as their str form, not leak __dict__ internals."""+ from enum import Enum, auto++ from marimo._output.data.data import sanitize_json_bigint++ class TestEnum(Enum):+ ONE = auto()+ TWO = auto()++ encoded = sanitize_json_bigint(+ [{"col": TestEnum.ONE}, {"col": TestEnum.TWO}]+ )+ assert encoded == ('[{"col":"TestEnum.ONE"},{"col":"TestEnum.TWO"}]')+ assert "_value_" not in encoded+ assert "_name_" not in encoded+ assert "__objclass__" not in encoded++@pytest.mark.skipif(sys.version_info < (3, 11), reason="StrEnum not supported in Python 3.10")