Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(hooks): deduplicate deprecated-settings-wrapper errors (#3549)

LearningCircuit6ad30595165d
diff --git a/.pre-commit-hooks/check-deprecated-settings-wrapper.py b/.pre-commit-hooks/check-deprecated-settings-wrapper.py
index 20ba7beeda..64cf56099d 100755
--- a/.pre-commit-hooks/check-deprecated-settings-wrapper.py
+++ b/.pre-commit-hooks/check-deprecated-settings-wrapper.py
@@ -8,7 +8,6 @@ with proper session context management instead.
NOTE: This hook currently only warns about usage to allow gradual migration.
"""
-import ast
import sys
from pathlib import Path
from typing import List, Tuple
@@ -28,44 +27,38 @@ def check_file(filepath: Path) -> List[Tuple[int, str]]:
try:
content = filepath.read_text()
- # Check for imports
- if "get_setting_from_db_main_thread" in content:
- lines = content.split("\n")
- for i, line in enumerate(lines, 1):
- if "get_setting_from_db_main_thread" in line:
- if "from" in line and "import" in line:
- errors.append(
- (
- i,
- "Importing deprecated get_setting_from_db_main_thread - use SettingsManager with proper session context",
- )
- )
- elif not line.strip().startswith("#"):
- # Check if it's a function call (not in a comment)
- errors.append(
- (
- i,
- "Using deprecated get_setting_from_db_main_thread - use SettingsManager with get_user_db_session context manager",
- )
- )
-
- # Also parse the AST to catch any dynamic usage
- try:
- tree = ast.parse(content)
- for node in ast.walk(tree):
- if (
- isinstance(node, ast.Name)
- and node.id == "get_setting_from_db_main_thread"
- ):
- errors.append(
- (
- node.lineno,
- "Reference to deprecated get_setting_from_db_main_thread function",
- )
+ # Short-circuit: no mention at all → nothing to check.
+ if "get_setting_from_db_main_thread" not in content:
+ return errors
+
+ # Per-line scan. Previously this routine also walked the AST and
+ # appended a second error per call site, producing duplicate
+ # diagnostics on every hit. The AST walk (ast.Name match) is
+ # strictly weaker than this line scan — any bare identifier
+ # reference also appears on the line that contains it — so it
+ # is safe to remove.
+ for i, line in enumerate(content.split("\n"), 1):
+ # Skip comment and docstring lines — they can legitimately
+ # mention the deprecated name without invoking it.
+ stripped = line.strip()
+ if stripped.startswith(("#", '"""', "'''")):
+ continue
+ if "get_setting_from_db_main_thread" not in line:
+ continue
+ if "from" in line and "import" in line:
+ errors.append(
+ (
+ i,
+ "Importing deprecated get_setting_from_db_main_thread - use SettingsManager with proper session context",
)
- except SyntaxError:
- # File has syntax errors, skip AST check
- pass
+ )
+ else:
+ errors.append(
+ (
+ i,
+ "Using deprecated get_setting_from_db_main_thread - use SettingsManager with get_user_db_session context manager",
+ )
+ )
except Exception as e:
print(f"Error checking {filepath}: {e}", file=sys.stderr)
diff --git a/tests/hooks/test_check_deprecated_settings_wrapper.py b/tests/hooks/test_check_deprecated_settings_wrapper.py
new file mode 100644
index 0000000000..9eac952755
--- /dev/null
+++ b/tests/hooks/test_check_deprecated_settings_wrapper.py
@@ -0,0 +1,108 @@
+"""
+Tests for the check-deprecated-settings-wrapper pre-commit hook.
+
+Covers regressions:
+- Previously, every call site of the deprecated wrapper produced *two*
+ errors: one from the per-line string scan and another from the AST
+ walk. Verify a single call site now produces exactly one error.
+- Comments and docstrings mentioning the deprecated name must not fire.
+
+Note: DEP is built at runtime to avoid embedding the literal deprecated
+name in this test file (the hook under test would otherwise flag its
+own test file).
+"""
+
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+
+HOOK_SCRIPT = (
+ Path(__file__).parent.parent.parent
+ / ".pre-commit-hooks"
+ / "check-deprecated-settings-wrapper.py"
+)
+
+DEP = "get_setting_from" + "_db_main_thread"
+
+
+def _run_hook(
+ content: str, filename: str = "service.py"
+) -> subprocess.CompletedProcess:
+ with tempfile.TemporaryDirectory() as d:
+ path = Path(d) / filename
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(content)
+ return subprocess.run(
+ [sys.executable, str(HOOK_SCRIPT), str(path)],
+ capture_output=True,
+ text=True,
+ )
+
+
+def _count_line_errors(stdout: str) -> int:
+ """Count lines of the form ' Line N: <message>'."""
+ return sum(
+ 1 for line in stdout.splitlines() if line.lstrip().startswith("Line ")
+ )
+
+
+class TestSingleErrorPerCallSite:
+ """Regression: each call site must produce exactly one error, not two."""
+
+ def test_single_call_single_error(self):
+ result = _run_hook(f"value = {DEP}('key')\n")
+ assert result.returncode == 1
+ assert _count_line_errors(result.stdout) == 1
+
+ def test_single_import_single_error(self):
+ result = _run_hook(
+ f"from local_deep_research.utilities.db_utils import {DEP}\n"
+ )
+ assert result.returncode == 1
+ assert _count_line_errors(result.stdout) == 1
+
+ def test_two_call_sites_two_errors(self):
+ content = f"x = {DEP}('a')\ny = {DEP}('b')\n"
+ result = _run_hook(content)
+ assert result.returncode == 1
+ assert _count_line_errors(result.stdout) == 2
+
+
+class TestCommentAndDocstringSkip:
+ """Comments and docstring lines must not be flagged."""
+
+ def test_comment_not_flagged(self):
+ result = _run_hook(f"# {DEP} is deprecated\n")
+ assert result.returncode == 0
+
+ def test_docstring_line_not_flagged(self):
+ result = _run_hook(f'"""{DEP} is deprecated."""\n')
+ assert result.returncode == 0
+
+ def test_single_quote_docstring_not_flagged(self):
+ result = _run_hook(f"'''{DEP} is deprecated.'''\n")
+ assert result.returncode == 0
+
+
+class TestFilesExempted:
+ """db_utils.py and the hook itself must remain exempt."""
+
+ def test_db_utils_exempted(self):
+ result = _run_hook(f"value = {DEP}('key')\n", filename="db_utils.py")
+ assert result.returncode == 0
+
+ def test_test_file_exempted(self):
+ result = _run_hook(
+ f"value = {DEP}('key')\n", filename="test_db_utils.py"
+ )
+ assert result.returncode == 0
+
+
+class TestNoMentionShortCircuits:
+ """Files that never mention the name must pass cleanly."""
+
+ def test_clean_file_passes(self):
+ result = _run_hook("def hello(): return 'world'\n")
+ assert result.returncode == 0