Skip to content

Commit 25b411f

Browse files
n24q02mclaude
andauthored
fix: stop two test modules from mutating global state at import time (#971)
* fix: scope the MAX_INPUT_LENGTH override to this test module The module set qwen3_embed.common.utils.MAX_INPUT_LENGTH = 100 at import time. pytest imports every test module during collection, so the override survived into every other module in the session -- including the ones deselected by -m, which are imported all the same. Five integration tests failed with 'exceeds maximum allowed length of 100 characters' on inputs that are nowhere near any real limit. The comment justifying it was wrong: check_input_length reads the global at call time, so it does not need to be set before the modules under test are imported. tests/test_utils.py already patches it per-test with monkeypatch.setattr. An autouse fixture does the same here and lets monkeypatch restore it afterwards. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: stop injecting a fake llama_cpp into sys.modules for the whole session The module assigned sys.modules['llama_cpp'] = MagicMock() at import time. Because pytest imports every test module during collection, the fake outlived this file and was still in place when tests/test_integration_gguf.py ran its pytest.importorskip('llama_cpp'). The guard found the fake, declined to skip, and 18 integration tests ran against a mock and failed -- turning 'the optional backend is not installed' into 'the backend is broken', which is the opposite reading. qwen3_embed.text.gguf_embedding imports llama_cpp lazily inside functions, so importing it needs no mock at all. The tests that do need one already scope it with patch.dict(sys.modules, ...), the pattern tests/test_gguf_cross_encoder.py uses. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent d9dc0be commit 25b411f

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

tests/test_gguf_embedding.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,23 @@
1111
import numpy as np
1212
import pytest
1313

14-
# Mock llama_cpp before importing the module under test
15-
_mock_llama_module = MagicMock()
16-
sys.modules["llama_cpp"] = _mock_llama_module
17-
18-
from qwen3_embed.text.gguf_embedding import ( # noqa: E402
14+
from qwen3_embed.text.gguf_embedding import (
1915
DEFAULT_TASK,
2016
QUERY_INSTRUCTION_TEMPLATE,
2117
Qwen3TextEmbeddingGGUF,
2218
_check_llama_cpp,
2319
)
2420

21+
# Note: qwen3_embed.text.gguf_embedding imports llama_cpp lazily inside
22+
# functions (see _check_llama_cpp / Qwen3TextEmbeddingGGUF.__init__), not at
23+
# module import time, so no sys.modules mock is needed just to import it.
24+
# Each test below that needs llama_cpp to appear "installed" scopes its own
25+
# mock via patch.dict(sys.modules, {"llama_cpp": ...}) so nothing leaks into
26+
# other test modules collected in the same pytest session (in particular,
27+
# tests/test_integration_gguf.py relies on llama_cpp being genuinely absent
28+
# so its pytest.importorskip("llama_cpp") can skip when the optional
29+
# dependency is not installed).
30+
2531
# ---------------------------------------------------------------------------
2632
# Tests for _check_llama_cpp
2733
# ---------------------------------------------------------------------------

tests/test_security_limits.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import pytest
22

3-
# Needs to be set BEFORE modules are imported to affect MAX_INPUT_LENGTH
43
import qwen3_embed.common.utils
54
import qwen3_embed.rerank.cross_encoder.text_cross_encoder
65
import qwen3_embed.text.text_embedding
76

8-
qwen3_embed.common.utils.MAX_INPUT_LENGTH = 100
7+
8+
@pytest.fixture(autouse=True)
9+
def _low_max_input_length(monkeypatch: pytest.MonkeyPatch) -> None:
10+
"""Lower MAX_INPUT_LENGTH for every test in this module.
11+
12+
check_input_length reads the module global at call time, so patching it
13+
per-test (and letting monkeypatch restore it afterwards) is sufficient --
14+
it does not need to be set before the modules under test are imported.
15+
A low limit lets the tests below exercise the length guard without
16+
allocating a huge string, and the patch does not leak into other test
17+
modules collected in the same pytest session.
18+
"""
19+
monkeypatch.setattr(qwen3_embed.common.utils, "MAX_INPUT_LENGTH", 100)
920

1021

1122
def test_check_input_length():

0 commit comments

Comments
 (0)