Skip to content

Commit 360f113

Browse files
committed
fix(tests): capture real __import__ before patching builtins to avoid recursion
fake_import closures that called __import__ directly would resolve to themselves after patch("builtins.__import__") was applied, causing RecursionError instead of the expected ImportError path. Capture builtins.__import__ into _real_import before the patch context so the fallthrough call uses the original import machinery. Fixes test_import_error_raises_embeddings_unavailable and the same pattern in test_debug_log_fires_before_raise_on_import_error.
1 parent 41e4bd8 commit 360f113

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

tests/test_coverage_iter235.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,15 @@ def test_is_exception_subclass(self):
356356
assert isinstance(exc, Exception)
357357

358358
def test_import_error_raises_embeddings_unavailable(self):
359-
# Clear model cache to force a fresh load attempt.
359+
import builtins as _b
360+
360361
emb_mod._MODEL_CACHE.clear()
362+
_real_import = _b.__import__
361363

362364
def fake_import(name, *args, **kwargs):
363365
if name == "fastembed":
364366
raise ImportError("no fastembed")
365-
return __import__(name, *args, **kwargs)
367+
return _real_import(name, *args, **kwargs)
366368

367369
with patch("builtins.__import__", side_effect=fake_import), pytest.raises(
368370
EmbeddingsUnavailable, match="fastembed not installed"
@@ -387,12 +389,15 @@ def fake_find_spec(name, *args, **kwargs):
387389
assert result is False
388390

389391
def test_debug_log_fires_before_raise_on_import_error(self):
392+
import builtins as _b
393+
390394
emb_mod._MODEL_CACHE.clear()
395+
_real_import = _b.__import__
391396

392397
def fake_import(name, *args, **kwargs):
393398
if name == "fastembed":
394399
raise ImportError("no fastembed")
395-
return __import__(name, *args, **kwargs)
400+
return _real_import(name, *args, **kwargs)
396401

397402
# Confirm the exception is EmbeddingsUnavailable — the debug log
398403
# may or may not fire depending on cache state, but the raise must happen.

0 commit comments

Comments
 (0)