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.

tidy: executor cleanup (#5025)

Dylan Madisetti8cab36b56667
diff --git a/marimo/_runtime/executor.py b/marimo/_runtime/executor.py
index 197d4879a7..96ce453453 100644
--- a/marimo/_runtime/executor.py
+++ b/marimo/_runtime/executor.py
@@ -10,6 +10,7 @@ from typing import TYPE_CHECKING, Any, Optional
from marimo._ast.cell import CellImpl, _is_coroutine
from marimo._ast.variables import is_mangled_local, unmangle_local
+from marimo._entrypoints.registry import EntryPointRegistry
from marimo._runtime.copy import (
CloneError,
ShallowCopy,
@@ -31,6 +32,26 @@ from marimo._runtime.primitives import (
if TYPE_CHECKING:
from marimo._runtime.dataflow import DirectedGraph
+_EXECUTOR_REGISTRY = EntryPointRegistry[type["Executor"]](
+ "marimo.cell.executor",
+)
+
+
+def get_executor(
+ config: ExecutionConfig,
+ registry: EntryPointRegistry[type[Executor]] = _EXECUTOR_REGISTRY,
+) -> Executor:
+ """Get a code executor based on the execution configuration."""
+ executors = registry.get_all()
+
+ base: Executor = DefaultExecutor()
+ if config.is_strict:
+ base = StrictExecutor(base)
+
+ for executor in executors:
+ base = executor(base)
+ return base
+
@dataclass
class ExecutionConfig:
@@ -53,15 +74,10 @@ def _raise_name_error(
raise MarimoRuntimeException from name_error
-def get_executor(config: ExecutionConfig) -> Executor:
- """Get a code executor based on the execution configuration."""
- base = DefaultExecutor()
- if config.is_strict:
- return StrictExecutor(base)
- return base
-
-
class Executor(ABC):
+ def __init__(self, base: Optional[Executor] = None) -> None:
+ self.base = base
+
@abstractmethod
def execute_cell(
self,
@@ -128,15 +144,14 @@ class DefaultExecutor(Executor):
class StrictExecutor(Executor):
- def __init__(self, base: Executor):
- self.base = base
-
async def execute_cell_async(
self,
cell: CellImpl,
glbls: dict[str, Any],
graph: DirectedGraph,
) -> Any:
+ assert self.base is not None, "Invalid executor composition."
+
# Manage globals and references, but refers to the default beyond that.
refs = graph.get_transitive_references(
cell.refs,
@@ -158,6 +173,8 @@ class StrictExecutor(Executor):
glbls: dict[str, Any],
graph: DirectedGraph,
) -> Any:
+ assert self.base is not None, "Invalid executor composition."
+
refs = graph.get_transitive_references(
cell.refs,
predicate=build_ref_predicate_for_primitives(
diff --git a/tests/test_entrypoints.py b/tests/test_entrypoints.py
index 4c8cfabed9..d3d0946c82 100644
--- a/tests/test_entrypoints.py
+++ b/tests/test_entrypoints.py
@@ -5,6 +5,7 @@ import pytest
from marimo._entrypoints.ids import KnownEntryPoint
from marimo._entrypoints.registry import EntryPointRegistry, get_entry_points
+from marimo._runtime.executor import ExecutionConfig, Executor, get_executor
class TestEntryPointRegistry:
@@ -87,3 +88,37 @@ class TestEntryPointRegistry:
# Should include both registered and entry point plugins
assert set(result) == {"value1", "ep_value1", "ep_value2"}
+
+
+class CustomExecutor(Executor):
+ def execute_cell(
+ self,
+ cell: str,
+ glbls: dict[str, str],
+ graph: str,
+ ) -> str:
+ return f"Executed {cell} with {glbls} in {graph}"
+
+ async def execute_cell_async(
+ self,
+ cell: str,
+ glbls: dict[str, str],
+ graph: str,
+ ) -> str:
+ return f"Executed {cell} with {glbls} in {graph}"
+
+
+class TestExecutorEntryPoint:
+ @pytest.fixture
+ def registry(self) -> EntryPointRegistry[Executor]:
+ reg = EntryPointRegistry[Executor]("marimo.cell.executor")
+ reg.register("custom", CustomExecutor)
+ return reg
+
+ def test_get_entry_points_modern(
+ self, registry: EntryPointRegistry[Executor]
+ ) -> None:
+ executor = get_executor(
+ ExecutionConfig(is_strict=False), registry=registry
+ )
+ assert isinstance(executor, CustomExecutor)