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.

Switch to structural typing for `QueueType` (#6262)

Trevor Manzb8681bf1a2d9
diff --git a/marimo/_server/sessions.py b/marimo/_server/sessions.py
index 52ecf71f77..61866dce8b 100644
--- a/marimo/_server/sessions.py
+++ b/marimo/_server/sessions.py
@@ -70,7 +70,6 @@ from marimo._server.session.serialize import (
)
from marimo._server.session.session_view import SessionView
from marimo._server.tokens import AuthToken, SkewProtectionToken
-from marimo._server.types import QueueType
from marimo._server.utils import print_, print_tabbed
from marimo._types.ids import CellId_t, ConsumerId, SessionId
from marimo._utils.disposable import Disposable
@@ -93,23 +92,28 @@ class QueueManager:
# Control messages for the kernel (run, set UI element, set config, etc
# ) are sent through the control queue
- self.control_queue: QueueType[requests.ControlRequest] = (
- context.Queue() if context is not None else queue.Queue()
- )
+ self.control_queue: Union[
+ mp.Queue[requests.ControlRequest],
+ queue.Queue[requests.ControlRequest],
+ ] = context.Queue() if context is not None else queue.Queue()
# Set UI element queues are stored in both the control queue and
# this queue, so that the backend can merge/batch set-ui-element
# requests.
- self.set_ui_element_queue: QueueType[
- requests.SetUIElementValueRequest
+ self.set_ui_element_queue: Union[
+ mp.Queue[requests.SetUIElementValueRequest],
+ queue.Queue[requests.SetUIElementValueRequest],
] = context.Queue() if context is not None else queue.Queue()
# Code completion requests are sent through a separate queue
- self.completion_queue: QueueType[requests.CodeCompletionRequest] = (
- context.Queue() if context is not None else queue.Queue()
- )
+ self.completion_queue: Union[
+ mp.Queue[requests.CodeCompletionRequest],
+ queue.Queue[requests.CodeCompletionRequest],
+ ] = context.Queue() if context is not None else queue.Queue()
- self.win32_interrupt_queue: QueueType[bool] | None
+ self.win32_interrupt_queue: (
+ Union[mp.Queue[bool], queue.Queue[bool]] | None
+ )
if sys.platform == "win32":
self.win32_interrupt_queue = (
context.Queue() if context is not None else queue.Queue()
@@ -119,7 +123,7 @@ class QueueManager:
# Input messages for the user's Python code are sent through the
# input queue
- self.input_queue: QueueType[str] = (
+ self.input_queue: Union[mp.Queue[str], queue.Queue[str]] = (
context.Queue(maxsize=1)
if context is not None
else queue.Queue(maxsize=1)
diff --git a/marimo/_server/types.py b/marimo/_server/types.py
index 7e49020018..7cf7260ac2 100644
--- a/marimo/_server/types.py
+++ b/marimo/_server/types.py
@@ -1,12 +1,30 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from typing import TYPE_CHECKING, TypeVar, Union
-
-if TYPE_CHECKING:
- import multiprocessing as mp
- from queue import Queue
+from typing import Protocol, TypeVar, Union
T = TypeVar("T")
-# strings for python 3.8 compatibility
-QueueType = Union["mp.Queue[T]", "Queue[T]"]
+
+
+class QueueType(Protocol[T]):
+ """
+ Minimal queue protocol used by Marimo.
+
+ Defines the common subset of methods shared by `multiprocessing.Queue` and
+ `queue.Queue` that Marimo depends on. Keeping this protocol minimal makes it
+ straightforward to stub in `marimo-lsp` and to substitute alternative queue
+ implementations (e.g., wrapping a ZeroMQ socket).
+
+ If additional queue behavior becomes necessary, expand this protocol so that
+ stubs and implementations stay aligned.
+ """
+
+ def get(
+ self, block: bool = True, timeout: Union[float, None] = None
+ ) -> T: ...
+ def put(
+ self, obj: T, /, block: bool = True, timeout: Union[float, None] = None
+ ) -> None: ...
+ def get_nowait(self) -> T: ...
+ def put_nowait(self, item: T, /) -> None: ...
+ def empty(self) -> bool: ...