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: handle nested msgspec parsing (#6382)
Myles Scolnick377a0c555946
diff --git a/marimo/_utils/parse_dataclass.py b/marimo/_utils/parse_dataclass.pyindex dd3058648e..8d4918de98 100644--- a/marimo/_utils/parse_dataclass.py+++ b/marimo/_utils/parse_dataclass.py@@ -38,7 +38,7 @@ def to_snake(string: str) -> str:).lstrip("_")-class DataclassParser:+class _DataclassParser:def __init__(self, allow_unknown_keys: bool = False):self.allow_unknown_keys = allow_unknown_keys@@ -153,6 +153,11 @@ class DataclassParser:elif dataclasses.is_dataclass(cls):return self.build_dataclass(value, cls) # type: ignore[return-value]+ if issubclass(cls, msgspec.Struct):+ return _parse_msgspec(+ value, strict=not self.allow_unknown_keys, cls=cls+ ) # type: ignore[return-value]+try:if isinstance(value, cls):return value # type: ignore[no-any-return]@@ -194,6 +199,16 @@ class DataclassParser:return cls(**transformed)+def _parse_msgspec(+ value: Union[bytes, str, dict[Any, Any]], *, strict: bool, cls: type[T]+) -> T:+ # If it is a dict, it is already parsed and we can just build the dataclass.+ if isinstance(value, dict):+ return msgspec.convert(value, strict=strict, type=cls)++ return msgspec.json.decode(value, strict=strict, type=cls)++def parse_raw(message: Union[bytes, str, dict[Any, Any]],cls: type[T],@@ -222,23 +237,13 @@ def parse_raw(# union types. We could move off if we changed the over-the-wire types to have# a tag, but that would require updating the front end.if dataclasses.is_dataclass(cls):- if isinstance(message, dict):- return cast(- "T",- DataclassParser(allow_unknown_keys).build_dataclass(- message, cls- ),- )- parsed = json.loads(message)+ as_dict = (+ json.loads(message) if not isinstance(message, dict) else message+ )+ parser = _DataclassParser(allow_unknown_keys)return cast("T",- DataclassParser(allow_unknown_keys).build_dataclass(parsed, cls),+ parser.build_dataclass(as_dict, cls),)- # If it is a dict, it is already parsed and we can just build the dataclass.- if isinstance(message, dict):- return msgspec.convert(message, type=cls)-- return msgspec.json.decode(- message, strict=not allow_unknown_keys, type=cls- )+ return _parse_msgspec(message, strict=not allow_unknown_keys, cls=cls)diff --git a/tests/_utils/test_parse_dataclass.py b/tests/_utils/test_parse_dataclass.pyindex 98295935d3..a50aab3ce0 100644--- a/tests/_utils/test_parse_dataclass.py+++ b/tests/_utils/test_parse_dataclass.py@@ -17,6 +17,7 @@ from typing import (Union,)+import msgspecimport pytestfrom marimo._config.config import ExperimentalConfigType@@ -899,3 +900,29 @@ def test_date_and_datetime_types() -> None:assert nested_parsed.datetimes["evening"] == dt.datetime(2023, 1, 15, 18, 0, 0)+++class StructClass(msgspec.Struct):+ limit: int+++@dataclass+class Nested:+ config: StructClass+++def test_dataclass_with_nested_msgspec_struct() -> None:+ data = {"config": {"limit": 10}}++ # Test as bytes+ serialized: bytes = serialize(data)+ parsed = parse_raw(serialized, Nested)+ assert parsed == Nested(config=StructClass(limit=10))++ # Test as str+ parsed = parse_raw(json.dumps(data), Nested)+ assert parsed == Nested(config=StructClass(limit=10))++ # Test as dict+ parsed = parse_raw(data, Nested, allow_unknown_keys=True)+ assert parsed == Nested(config=StructClass(limit=10))