diff --git a/hermes_state_registry.py b/hermes_state_registry.py index f00969c721..5abfed8094 100644 --- a/hermes_state_registry.py +++ b/hermes_state_registry.py @@ -2,7 +2,8 @@ ``acquire`` opens a ``hermes_state.SessionDB`` on the storage interface and ``release`` closes it; nothing is shared between callers. The first ``acquire`` in a process emits the ``hermes_state`` stand-in's one deprecation warning, -unless another stand-in name has already emitted it. +unless another stand-in name has already emitted it. A process that loaded an older release's ``hermes_state`` before +an update (a long-running server importing this module lazily) gets that release's ``SessionDB``, without the warning. """ import warnings as _warnings @@ -12,7 +13,10 @@ import hermes_state as _hermes_state def acquire(db_path=None): """A ``hermes_state.SessionDB`` on the store of ``db_path`` (a ``state.db``), or of the current profile.""" - if _hermes_state._first_warning(): + first_warning = getattr(_hermes_state, "_first_warning", None) + if first_warning is None: + return _hermes_state.SessionDB(db_path) + if first_warning(): _warnings.warn( f"hermes_state_registry.acquire is deprecated and is removed in {_hermes_state._REMOVAL_RELEASE}; use " f"persistence_assembly.open_persistence(profile_scope()) instead ({_hermes_state._MIGRATION_GUIDE}).", diff --git a/tests/test_hermes_state_registry_old_release.py b/tests/test_hermes_state_registry_old_release.py new file mode 100644 index 0000000000..a33b5887e8 --- /dev/null +++ b/tests/test_hermes_state_registry_old_release.py @@ -0,0 +1,25 @@ +"""A process that loaded an older release's ``hermes_state`` before ``hermes update`` still gets a store from the +registry module it imports lazily afterwards.""" + +import importlib +import sys +import types + + +class _OldReleaseSessionDB: + def __init__(self, db_path=None): + self.db_path = db_path + + +def test_acquire_under_an_older_loaded_hermes_state_opens_that_releases_session_db(monkeypatch, tmp_path): + old = types.ModuleType("hermes_state") + old.SessionDB = _OldReleaseSessionDB # type: ignore[attr-defined] + monkeypatch.setitem(sys.modules, "hermes_state", old) + monkeypatch.delitem(sys.modules, "hermes_state_registry", raising=False) + registry = importlib.import_module("hermes_state_registry") + try: + db = registry.acquire(tmp_path / "state.db") + assert isinstance(db, _OldReleaseSessionDB) + assert db.db_path == tmp_path / "state.db" + finally: + sys.modules.pop("hermes_state_registry", None)