-
Notifications
You must be signed in to change notification settings - Fork 2
feat: resolve third-party backends via entry points (makes ADR 0005 real) #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+117
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """Discover third-party LedgerBackend implementations via entry points. | ||
|
|
||
| Built-in backends (in-memory, SQLite, JSON, Snowflake, HTTP) are resolved | ||
| directly. This module fulfills the storage-agnostic extension contract from | ||
| ADR 0005: a downstream package can register its own backend without any change | ||
| to the core, by declaring an entry point — | ||
|
|
||
| # in the downstream package's pyproject.toml | ||
| [project.entry-points."model_ledger.backends"] | ||
| postgres = "my_package:PostgresBackend" | ||
|
|
||
| The registered target is called with the connection string if one is given | ||
| (``Backend(path)``), otherwise with no arguments (``Backend()``). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| ENTRY_POINT_GROUP = "model_ledger.backends" | ||
|
|
||
|
|
||
| def load_backend_class(name: str) -> Any: | ||
| """Return the backend target registered under ``name``, or ``None`` if absent. | ||
|
|
||
| Args: | ||
| name: The entry-point name to look up in the ``model_ledger.backends`` group. | ||
|
|
||
| Returns: | ||
| The loaded class or factory, or ``None`` if no entry point matches (or the | ||
| metadata is unavailable). | ||
| """ | ||
| try: | ||
| from importlib.metadata import entry_points | ||
|
|
||
| for ep in entry_points(group=ENTRY_POINT_GROUP): | ||
| if ep.name == name: | ||
| return ep.load() | ||
| except Exception: | ||
| return None | ||
| return None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """Third-party backends resolve via the model_ledger.backends entry-point group (ADR 0005).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import importlib.metadata | ||
|
|
||
| from model_ledger.backends import registry | ||
| from model_ledger.backends.ledger_memory import InMemoryLedgerBackend | ||
| from model_ledger.backends.ledger_protocol import LedgerBackend | ||
|
|
||
|
|
||
| class _StubBackend: | ||
| """Stand-in for a downstream package's backend target.""" | ||
|
|
||
| def __init__(self, path: str | None = None) -> None: | ||
| self.path = path | ||
|
|
||
|
|
||
| class _FakeEntryPoint: | ||
| name = "stub" | ||
|
|
||
| def load(self): | ||
| return _StubBackend | ||
|
|
||
|
|
||
| def _fake_entry_points(*, group: str | None = None): | ||
| return [_FakeEntryPoint()] if group == registry.ENTRY_POINT_GROUP else [] | ||
|
|
||
|
|
||
| def test_load_backend_class_resolves_entry_point(monkeypatch): | ||
| monkeypatch.setattr(importlib.metadata, "entry_points", _fake_entry_points) | ||
| assert registry.load_backend_class("stub") is _StubBackend | ||
|
|
||
|
|
||
| def test_load_backend_class_unknown_returns_none(monkeypatch): | ||
| monkeypatch.setattr(importlib.metadata, "entry_points", _fake_entry_points) | ||
| assert registry.load_backend_class("nope") is None | ||
|
|
||
|
|
||
| def test_resolve_backend_instantiates_entry_point(monkeypatch): | ||
| monkeypatch.setattr(importlib.metadata, "entry_points", _fake_entry_points) | ||
| from model_ledger.cli.app import _resolve_backend | ||
|
|
||
| backend = _resolve_backend("stub", "conn-str") | ||
| assert isinstance(backend, _StubBackend) | ||
| assert backend.path == "conn-str" | ||
|
|
||
|
|
||
| def test_inmemory_backend_satisfies_protocol(): | ||
| """Conformance: a real backend is recognized by the runtime-checkable protocol.""" | ||
| assert isinstance(InMemoryLedgerBackend(), LedgerBackend) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an installed third-party backend entry point matches the requested name but
ep.load()raises (for example because the plugin has a missing dependency or an invalid target), this broadexceptreturnsNone. In the inspected CLI path, bothmodel-ledger mcp --backend <name>andmodel-ledger serve --backend <name>pass thatNoneintocreate_server/create_app, which default to an in-memory backend, so a production service can silently start with transient empty storage instead of reporting the broken configured backend.Useful? React with 👍 / 👎.