From 79786880fe57490bddd50a42d06798cf40aa08fb Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Mon, 4 May 2026 17:24:32 -0300 Subject: [PATCH] feat(execution): add executor registry (register/get only) --- src/lfx/src/lfx/execution/registry.py | 27 ++++++++++++++++ src/lfx/tests/unit/execution/test_registry.py | 31 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/lfx/src/lfx/execution/registry.py create mode 100644 src/lfx/tests/unit/execution/test_registry.py diff --git a/src/lfx/src/lfx/execution/registry.py b/src/lfx/src/lfx/execution/registry.py new file mode 100644 index 0000000000..857d0fe1dc --- /dev/null +++ b/src/lfx/src/lfx/execution/registry.py @@ -0,0 +1,27 @@ +"""Executor registry.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from lfx.execution.executor import Executor + + +class ExecutorNotFoundError(LookupError): + pass + + +class ExecutorRegistry: + def __init__(self) -> None: + self._by_kind: dict[str, Executor] = {} + + def register(self, executor: Executor) -> None: + self._by_kind[executor.kind] = executor + + def get(self, kind: str) -> Executor: + try: + return self._by_kind[kind] + except KeyError as exc: + msg = f"No executor registered for kind={kind!r}" + raise ExecutorNotFoundError(msg) from exc diff --git a/src/lfx/tests/unit/execution/test_registry.py b/src/lfx/tests/unit/execution/test_registry.py new file mode 100644 index 0000000000..91c923e7f3 --- /dev/null +++ b/src/lfx/tests/unit/execution/test_registry.py @@ -0,0 +1,31 @@ +import pytest +from lfx.execution.executor import Executor +from lfx.execution.registry import ExecutorNotFoundError, ExecutorRegistry + + +class _Stub(Executor): + kind = "stub" + + async def execute(self, unit): # noqa: ARG002 + return + yield + + +def test_register_and_get_by_kind(): + registry = ExecutorRegistry() + registry.register(_Stub()) + assert registry.get("stub").kind == "stub" + + +def test_get_unknown_raises(): + with pytest.raises(ExecutorNotFoundError, match="nope"): + ExecutorRegistry().get("nope") + + +def test_register_replaces_same_kind(): + registry = ExecutorRegistry() + a = _Stub() + b = _Stub() + registry.register(a) + registry.register(b) + assert registry.get("stub") is b