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.

3.1 KB
Raw Blame
"""Import-boundary checks for the external arXiv package."""

import ast
from pathlib import Path
from typing import Final, override

_REPO_ROOT = Path(__file__).resolve().parents[2]
SHARED_UTILITY_PATH: Final = "src/local_deep_research/utilities/arxiv_api.py"
_SCAN_ROOTS: Final = ("src", "scripts")
_RUNNABLE_TEST_PATHS: Final = (
    "tests/download_stuff_for_local.py",
    "tests/performance/relevance_filter/eval_models.py",
    "tests/performance/relevance_filter/eval_prompt.py",
    "tests/performance/relevance_filter/test_live.py",
    "tests/benchmarks/evaluators/arxiv_optimization/benchmark_arxiv_query_optimization.py",
)


def _is_external_arxiv(module_name: str) -> bool:
    return module_name.partition(".")[0] == "arxiv"


class _ArxivImportVisitor(ast.NodeVisitor):
    def __init__(self) -> None:
        self.violation_lines: set[int] = set()

    @override
    def visit_Import(self, node: ast.Import) -> None:
        if any(_is_external_arxiv(alias.name) for alias in node.names):
            self.violation_lines.add(node.lineno)

    @override
    def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
        if (
            node.level == 0
            and node.module is not None
            and _is_external_arxiv(node.module)
        ):
            self.violation_lines.add(node.lineno)

    @override
    def visit_Call(self, node: ast.Call) -> None:
        if node.args and isinstance(node.args[0], ast.Constant):
            module_name = node.args[0].value
            if isinstance(module_name, str) and _is_external_arxiv(module_name):
                if (
                    isinstance(node.func, ast.Name)
                    and node.func.id in {"__import__", "import_module"}
                ) or (
                    isinstance(node.func, ast.Attribute)
                    and node.func.attr == "import_module"
                ):
                    self.violation_lines.add(node.lineno)
        self.generic_visit(node)


def arxiv_policy_violation_lines(source: str) -> tuple[int, ...]:
    """Return lines that load the external ``arxiv`` package."""
    visitor = _ArxivImportVisitor()
    visitor.visit(ast.parse(source))
    return tuple(sorted(visitor.violation_lines))


def _iter_scanned_paths() -> list[Path]:
    paths: list[Path] = []
    for root_name in _SCAN_ROOTS:
        root = _REPO_ROOT / root_name
        if root.is_dir():
            paths.extend(root.rglob("*.py"))
    paths.extend(
        path
        for relative in _RUNNABLE_TEST_PATHS
        if (path := _REPO_ROOT / relative).is_file()
    )
    return sorted(paths)


def scan_repo_for_arxiv_policy_violations() -> list[str]:
    """Return sorted ``path:line`` external-arXiv import violations."""
    violations: list[str] = []
    for path in _iter_scanned_paths():
        relative = path.relative_to(_REPO_ROOT).as_posix()
        if relative == SHARED_UTILITY_PATH:
            continue
        source = path.read_text(encoding="utf-8")
        violations.extend(
            f"{relative}:{line}"
            for line in arxiv_policy_violation_lines(source)
        )
    return sorted(violations)