Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions tests/unit/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,39 @@ def _reset_live_caches():
_live_caches.clear()


def _reload_fleche_state():
"""Reload ``fleche.state`` in place, preserving the ``BoundWrapper`` class identity.

``importlib.reload`` re-executes the module inside its *existing*
``__dict__``, which is what picks up a monkeypatched config into the
module-level ``_CACHE``/``_METADATA`` ContextVars. But it also rebuilds
every class defined in the module, including ``BoundWrapper`` — and
plenty of code (this test file, ``tests/unit/test_executor.py``, any
user code) holds a reference to the *original* class via
``from fleche import BoundWrapper`` or ``from fleche.state import
BoundWrapper``, captured once at import time. A second, distinct
``BoundWrapper`` class breaks ``isinstance`` checks (e.g.
``executor.py``'s ``isinstance(func, state.BoundWrapper)``) for every
test that runs afterwards, for any reference taken before the reload.

So: reload, then restore the original class object. Since the module
dict is reused in place, the restored class's ``bind`` classmethod still
resolves ``_CACHE``/``_METADATA`` to the freshly reloaded ContextVars —
only the class *identity* is preserved, not its stale state.
"""
import importlib
import fleche.state

original_bound_wrapper = fleche.state.BoundWrapper
importlib.reload(fleche.state)
fleche.state.BoundWrapper = original_bound_wrapper


@pytest.fixture
def restore_fleche_state():
"""Reload fleche.state after each test to undo any importlib.reload side effects."""
yield
import importlib
import fleche.state
importlib.reload(fleche.state)
_reload_fleche_state()


def test_load_cache_config_default(monkeypatch, config_file):
Expand Down Expand Up @@ -220,10 +246,9 @@ def test_cache_default_activates_default_cache(monkeypatch, config_file):
def test_load_default_metadata(restore_fleche_state, monkeypatch, config_file):
monkeypatch.setenv("XDG_CONFIG_HOME", config_file)

import importlib
import fleche.state

importlib.reload(fleche.state)
_reload_fleche_state()

meta = fleche.state._METADATA.get()
assert len(meta) == 1
Expand All @@ -233,10 +258,9 @@ def test_load_default_metadata(restore_fleche_state, monkeypatch, config_file):
def test_load_default_cache(restore_fleche_state, monkeypatch, config_file):
monkeypatch.setenv("XDG_CONFIG_HOME", config_file)

import importlib
import fleche.state

importlib.reload(fleche.state)
_reload_fleche_state()

cache_obj = fleche.state._CACHE.get()
assert isinstance(cache_obj, BaseCache)
Expand Down
Loading
Loading