Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

test: replace subprocess startup-import diff with static source scan

Michael Riad Zaky47b2832d6f57
diff --git a/tests/test_litellm/proxy/test_proxy_server.py b/tests/test_litellm/proxy/test_proxy_server.py
index 7a96f6cbd15..17b03adf0a2 100644
--- a/tests/test_litellm/proxy/test_proxy_server.py
+++ b/tests/test_litellm/proxy/test_proxy_server.py
@@ -5513,39 +5513,34 @@ class TestLazyFeaturesNotImportedAtStartup:
"""
def test_heavy_modules_absent_at_startup(self):
- # Force a fresh `proxy_server` import in a subprocess so other tests
- # in this run (which may have triggered lazy loads via the TestClient)
- # don't pollute the result.
- import subprocess
-
- check = (
- "import sys; "
- "from litellm.proxy.proxy_server import app; " # noqa: F401
- "heavy = ["
- "'litellm.proxy._experimental.mcp_server.rest_endpoints',"
- "'litellm.proxy._experimental.mcp_server.server',"
- "'litellm.proxy.management_endpoints.config_override_endpoints',"
- "'litellm.proxy.guardrails.guardrail_endpoints',"
- "'litellm.proxy.openai_evals_endpoints.endpoints',"
- "]; "
- "still_present = [m for m in heavy if m in sys.modules]; "
- "print('PRESENT_AT_STARTUP:', still_present)"
- )
- result = subprocess.run(
- [sys.executable, "-c", check],
- capture_output=True,
- text=True,
- timeout=120,
+ # Static scan of proxy_server.py source — catches any top-level
+ # `from <lazy_module> import` that would defeat lazy loading.
+ # Importing proxy_server in a subprocess and diffing sys.modules
+ # would also work, but takes 60-120 s and flakes on slow CI runners.
+ import re
+ from pathlib import Path
+
+ from litellm.proxy._lazy_features import LAZY_FEATURES
+
+ proxy_server_src = (
+ Path(__file__).resolve().parents[3] / "litellm/proxy/proxy_server.py"
+ ).read_text()
+
+ leaks = []
+ for feat in LAZY_FEATURES:
+ # Anchor at column 0 — indented imports inside function bodies
+ # are fine (deferred until the function runs).
+ pattern = (
+ rf"^(from\s+{re.escape(feat.module_path)}\s+import|"
+ rf"import\s+{re.escape(feat.module_path)})"
+ )
+ if re.search(pattern, proxy_server_src, re.MULTILINE):
+ leaks.append(feat.module_path)
+
+ assert not leaks, (
+ "proxy_server.py top-level imports a lazy feature module — these "
+ f"should be loaded via LazyFeatureMiddleware: {leaks}"
)
- # Last non-empty line of stdout (skip warnings printed before)
- out_lines = [
- line for line in result.stdout.strip().splitlines() if line.strip()
- ]
- report = next((line for line in out_lines if "PRESENT_AT_STARTUP" in line), "")
- assert report, f"no report emitted (stderr: {result.stderr[-500:]})"
- assert (
- "PRESENT_AT_STARTUP: []" in report
- ), f"expected no heavy modules at startup, got: {report}"
class TestLazyFeatureMiddleware: