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.

1.8 KB
Raw Blame
from __future__ import annotations

from typing import Any

from marimo._messaging.mimetypes import ConsoleMimeType
from marimo._messaging.notification import (
    CellNotification,
    NotificationMessage,
)
from marimo._messaging.serde import deserialize_kernel_message
from marimo._messaging.types import KernelMessage, Stderr, Stream


class MockStream(Stream):
    def __init__(self, stream: Stream | None = None) -> None:
        self.messages: list[KernelMessage] = []

        if stream is not None and hasattr(stream, "messages"):
            self.messages = stream.messages

    def write(self, data: KernelMessage) -> None:
        self.messages.append(data)
        # Attempt to deserialize the message to ensure it is valid
        deserialize_kernel_message(data)

    @property
    def operations(self) -> list[dict[str, Any]]:
        import json

        return [json.loads(op_data) for op_data in self.messages]

    @property
    def parsed_operations(self) -> list[NotificationMessage]:
        return [
            deserialize_kernel_message(op_data) for op_data in self.messages
        ]

    @property
    def cell_notifications(self) -> list[CellNotification]:
        return [
            op
            for op in self.parsed_operations
            if isinstance(op, CellNotification)
        ]


class MockStderr(Stderr):
    def __init__(self, stream: Stderr | None = None) -> None:
        self.messages: list[str] = []

        if stream is not None and hasattr(stream, "messages"):
            self.messages = stream.messages

    def _write_with_mimetype(
        self,
        data: str,
        mimetype: ConsoleMimeType,
        *,
        code: str | None = None,
    ) -> int:
        del mimetype, code
        self.messages.append(data)
        return len(data)