test: drop the __file__-relative sys.path.insert calls from the unit tests

Follows #37802, which removed the cwd-relative form and left the __file__-relative
and variable-based call sites for later. Under tests/test_litellm those 42 sites
all add the repo root, or the test's own directory, to sys.path. pytest already
puts the repo root on sys.path for this tree (tests/ is a package, so the rootdir
basedir is inserted) and litellm itself is installed in the environment, so none
of them changes what is importable.

41 call sites removed together with the os, sys and Path imports that nothing
else used. The one in test_check_licenses.py is kept and marked test-quality-ok:
it imports check_licenses out of tests/code_coverage_tests, which is not a
package, the same way test_check_py310_typing_imports.py already does.

TQ003 across tests/ goes from 62 to 20 and the budget is ratcheted to match.
This commit is contained in:
Ranjan G 2026-09-05 15:33:02 +05:30
parent 95b438013a
commit 16c9d6d5c9
43 changed files with 6 additions and 240 deletions

View file

@ -6,7 +6,7 @@
"limit": 737
},
"TQ003": {
"limit": 62
"limit": 20
},
"TQ004": {
"limit": 469

View file

@ -6,13 +6,8 @@ at the package default. This is what lets request_timeout act as an independent
per-attempt timeout instead of being indistinguishable from "nobody set it".
"""
import os
import sys
import pytest
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../..")))
import litellm
from litellm.constants import DEFAULT_REQUEST_TIMEOUT_SECONDS
from litellm.litellm_core_utils.request_timeout_resolver import (

View file

@ -25,18 +25,11 @@ Tests cover (consolidating PRs #23706 and #22727):
the fallback inference path from being exercised).
"""
import os
import sys
from unittest.mock import MagicMock, patch
import pytest
# Anchor sys.path to this file's location — not the working-directory-relative
# pattern Greptile flagged on PR #23706. Resolves correctly regardless of
# where pytest is invoked from.
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../../.."))
)
from litellm.llms.anthropic.experimental_pass_through.adapters.handler import (
ANTHROPIC_ONLY_REQUEST_KEYS,

View file

@ -1,11 +1,7 @@
import json
import os
import sys
import pytest
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../../..")))
from litellm.llms.anthropic.experimental_pass_through.adapters.handler import (
LiteLLMMessagesToCompletionTransformationHandler,
)

View file

@ -1,14 +1,10 @@
import datetime
import json
import os
import sys
from unittest.mock import AsyncMock, patch
import pytest
import respx
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../../..")))
import litellm
from litellm.llms.anthropic.experimental_pass_through.responses_adapters.handler import (
LiteLLMMessagesToResponsesAPIHandler,

View file

@ -4,12 +4,8 @@ Tests for AnthropicResponsesStreamWrapper
"""
import asyncio
import os
import sys
from types import SimpleNamespace
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../../..")))
from litellm.litellm_core_utils.prompt_templates.common_utils import (
encrypted_reasoning_signature,
)

View file

@ -11,15 +11,11 @@ Verifies that:
"""
import json
import os
import sys
from types import SimpleNamespace
from unittest.mock import patch
import pytest
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../..")))
# Fake tokens for testing (not real secrets)
FAKE_OAUTH_TOKEN = "sk-ant-oat01-fake-token-for-testing-123456789abcdef"
FAKE_REGULAR_KEY = "sk-ant-api03-regular-key-for-testing-123456789"

View file

@ -7,12 +7,6 @@ Verifies that get_required_headers() correctly handles OAuth tokens
Regression test for https://github.com/BerriAI/litellm/issues/22040
"""
import os
import sys
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
from litellm.llms.anthropic.count_tokens.transformation import (
AnthropicCountTokensConfig,

View file

@ -8,13 +8,6 @@ C. Empty text content
"""
import pytest
import sys
import os
# Add the parent directory to the path so we can import litellm
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
)
import litellm
from litellm.litellm_core_utils.prompt_templates.factory import (

View file

@ -1,14 +1,8 @@
import os
import sys
from typing import Final
import pytest
from pydantic import TypeAdapter
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
import litellm
from litellm.litellm_core_utils.prompt_templates.common_utils import TOOL_RESULT_IMAGE_BOUNDARY
from litellm.llms.azure.chat.gpt_5_transformation import AzureOpenAIGPT5Config

View file

@ -1,13 +1,7 @@
import os
import sys
from unittest.mock import MagicMock, patch
import pytest
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
from litellm.llms.azure.azure import AzureChatCompletion
from litellm.types.utils import EmbeddingResponse, Usage

View file

@ -1,21 +1,9 @@
import json
import os
import sys
from typing import Any, Dict, Optional
from unittest.mock import MagicMock, patch, Mock, mock_open
from unittest.mock import MagicMock, patch, mock_open
import pytest
import httpx
# Add the parent directory to the system path
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../../.."))
)
import litellm
from litellm.llms.azure.videos.transformation import AzureVideoConfig
from litellm.types.videos.main import (
VideoObject,
VideoResponse,
VideoCreateOptionalRequestParams,
)
from litellm.types.router import GenericLiteLLMParams

View file

@ -1,15 +1,7 @@
import os
import sys
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
import json
from unittest.mock import MagicMock, patch
import pytest
from litellm.llms.azure_ai.anthropic.handler import AzureAnthropicChatCompletion
from litellm.types.utils import ModelResponse

View file

@ -1,12 +1,6 @@
import copy
import json
import os
import sys
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
from unittest.mock import patch
import pytest

View file

@ -1,11 +1,4 @@
import os
import sys
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
import pytest
from litellm.litellm_core_utils.get_llm_provider_logic import (
_is_azure_claude_model,

View file

@ -1,11 +1,5 @@
import os
import sys
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
from unittest.mock import MagicMock, patch
from unittest.mock import patch
import pytest

View file

@ -2,14 +2,8 @@
Ensure litellm.completion() forwards timeout to Azure Anthropic handler (main.py dispatch).
"""
import os
import sys
from unittest.mock import MagicMock, patch
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
from litellm import completion
from litellm.types.utils import ModelResponse

View file

@ -11,13 +11,9 @@ Validates that:
import asyncio
import gc
import sys
from pathlib import Path
import pytest
# Add litellm to path
sys.path.insert(0, str(Path(__file__).parent))
async def test_aiohttp_handler_cleanup():
"""Test BaseLLMAIOHTTPHandler session cleanup via __del__"""

View file

@ -72,11 +72,6 @@ pytestmark = pytest.mark.skip(
"python tests/test_litellm/llms/databricks/test_databricks_e2e.py"
)
# Add the litellm package to path
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
)
# Config file path - can be overridden with DATABRICKS_TEST_CONFIG env var
DEFAULT_CONFIG_PATH = os.path.expanduser("~/.databricks_litellm_config.txt")
CONFIG_FILE = os.environ.get("DATABRICKS_TEST_CONFIG", DEFAULT_CONFIG_PATH)

View file

@ -1,13 +1,7 @@
import os
import sys
from unittest.mock import patch
from pydantic import BaseModel
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
from litellm.llms.lm_studio.chat.transformation import LMStudioChatConfig
from litellm.utils import get_optional_params

View file

@ -1,15 +1,7 @@
import inspect
import os
import sys
from typing import cast
import pytest
from pydantic import BaseModel
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
from litellm.llms.ollama.chat.transformation import (
OllamaChatConfig,
OllamaChatCompletionResponseIterator,

View file

@ -2,16 +2,8 @@
Tests for OpenAI-like Responses API support in the JSON provider system.
"""
import os
import sys
from unittest.mock import patch
import pytest
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../../.."))
)
class TestSimpleProviderConfigSupportedEndpoints:
"""Test the supported_endpoints field on SimpleProviderConfig."""

View file

@ -2,12 +2,6 @@
Unit tests for the Abliteration OpenAI-like provider.
"""
import os
import sys
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
)
from litellm.llms.openai_like.dynamic_config import create_config_class
from litellm.llms.openai_like.json_loader import JSONProviderRegistry

View file

@ -2,12 +2,6 @@
Unit tests for the AssemblyAI LLM Gateway OpenAI-like provider.
"""
import os
import sys
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
)
from litellm.llms.openai_like.dynamic_config import create_config_class
from litellm.llms.openai_like.json_loader import JSONProviderRegistry

View file

@ -2,18 +2,12 @@
Tests for Charity Engine provider configuration and integration.
"""
import os
import sys
try:
import pytest
except ImportError:
pytest = None
# Add workspace to path
workspace_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
sys.path.insert(0, workspace_path)
import litellm

View file

@ -2,12 +2,6 @@
Unit tests for the EmpirioLabs OpenAI-like provider.
"""
import os
import sys
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
)
from litellm.llms.openai_like.dynamic_config import create_config_class
from litellm.llms.openai_like.json_loader import JSONProviderRegistry

View file

@ -3,7 +3,6 @@ Tests for JSON-based provider configuration system.
"""
import os
import sys
from unittest.mock import patch
try:
@ -12,9 +11,8 @@ except ImportError:
# pytest not available, will run as standalone script
pytest = None
# Add workspace to path
# Repo root, used to locate model_prices_and_context_window.json.
workspace_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
sys.path.insert(0, workspace_path)
import litellm

View file

@ -4,18 +4,12 @@ Related to issue #18794
"""
import os
import sys
from unittest.mock import MagicMock, patch
try:
import pytest
except ImportError:
pytest = None
# Add workspace to path
workspace_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
sys.path.insert(0, workspace_path)
import litellm

View file

@ -5,13 +5,7 @@ Tests that LiteLLM properly constructs URLs when using custom api_base
for PSC endpoints.
"""
import os
import sys
import pytest
# Add the litellm package to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../../.."))
from litellm.llms.vertex_ai.vertex_llm_base import VertexBase

View file

@ -5,15 +5,9 @@ descending into it, the banned-param boundary check is bypassed by
nesting the same fields under ``extra_body``.
"""
import os
import sys
import pytest
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
)
from litellm.proxy.auth.auth_utils import is_request_body_safe # noqa: E402

View file

@ -9,8 +9,6 @@ from __future__ import annotations
import contextlib
import os
import sys
from pathlib import Path
from typing import Any, AsyncIterator, Callable, Dict, Iterator, List, Optional
from unittest.mock import AsyncMock, MagicMock
@ -20,7 +18,6 @@ import pytest
# matter where pytest is invoked from. With the project installed via
# uv this is defensive — `litellm` already resolves through site-packages
# — but it lets the harness work in editable-source layouts too.
sys.path.insert(0, str(Path(__file__).resolve().parents[4]))
# ---------------------------------------------------------------------------

View file

@ -24,7 +24,6 @@ RDS IAM token when ``IAM_TOKEN_DB_AUTH`` is set).
"""
import os
import sys
# Importing ``litellm.proxy.proxy_server`` runs its module-level setup, which
# reads ``DATABASE_URL`` (Prisma) and ``LITELLM_MASTER_KEY``. Tier-zero CI
@ -45,10 +44,6 @@ from fastapi.routing import Mount
from prometheus_client import make_asgi_app
# gateway/ and backend/ live at the repo root, not inside litellm/.
_REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
if _REPO_ROOT not in sys.path:
sys.path.insert(0, _REPO_ROOT)
from backend.routes.allowlist import (
BACKEND_EXACT_PATHS,
BACKEND_MOUNT_PATHS,

View file

@ -6,17 +6,11 @@ deployment are merged into request metadata and trigger execution for both
streaming and non-streaming post_call hooks.
"""
import os
import sys
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../..")))
from litellm.proxy.utils import (
_check_and_merge_model_level_guardrails,
_merge_guardrails_with_existing,
)
# ---------------------------------------------------------------------------

View file

@ -13,15 +13,9 @@ pass and ``_load_instance_from_remote_storage`` would exec the
remote module.
"""
import os
import sys
import pytest
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
)
from litellm.proxy.proxy_server import ( # noqa: E402
_scrub_db_overlay_remote_module_loads,
)

View file

@ -7,16 +7,10 @@ unaffected — the documented ``litellm_settings.callbacks:
["s3://bucket/module.instance"]`` operator flow continues to work.
"""
import os
import sys
from unittest.mock import patch
import pytest
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../.."))
)
from litellm.proxy.types_utils.utils import get_instance_fn # noqa: E402

View file

@ -13,19 +13,14 @@ needing a generated Prisma client or a real database.
from __future__ import annotations
import asyncio
import sys
import threading
from dataclasses import dataclass, field
from email.message import EmailMessage
from pathlib import Path
from typing import Any, Callable, Dict, Iterator, List, Optional
from unittest.mock import AsyncMock, MagicMock
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[5]))
VOLATILE_KEYS = frozenset(
{
"created_at",
@ -188,6 +183,7 @@ def patched_prisma_import(monkeypatch: pytest.MonkeyPatch) -> Iterator[MagicMock
directly and restore in teardown.
"""
import prisma as _prisma_pkg
import litellm.proxy.utils as _utils_mod
fake_prisma = MagicMock(name="FakePrisma")

View file

@ -6,16 +6,11 @@ live here. Tests should not declare fixtures inline.
from __future__ import annotations
import sys
from pathlib import Path
from typing import Any, Dict, Optional
from unittest.mock import MagicMock
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[5]))
VOLATILE_KEYS = frozenset(
{
"created_at",

View file

@ -9,13 +9,8 @@ Verifies that:
import inspect
import json
import os
import sys
import unittest.mock as mock
# Use __file__ so the import path is correct regardless of the pytest working directory.
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))
import httpx
import pytest

View file

@ -17,7 +17,7 @@ import requests
_CODE_COVERAGE_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "..", "code_coverage_tests"
)
sys.path.insert(0, _CODE_COVERAGE_DIR)
sys.path.insert(0, _CODE_COVERAGE_DIR) # test-quality-ok: required to import checker from its source directory
import check_licenses # noqa: E402

View file

@ -1,12 +1,7 @@
"""Unit tests for litellm.litellm_core_utils.completion_timeout.CompletionTimeout."""
import os
import sys
import httpx
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../..")))
from litellm.litellm_core_utils.completion_timeout import CompletionTimeout
from litellm.utils import supports_httpx_timeout

View file

@ -4,13 +4,8 @@ Test nested path support in additional_drop_params.
This tests the new JSONPath-like syntax for removing nested fields.
"""
import os
import sys
# Add parent directory to path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
from litellm.litellm_core_utils.dot_notation_indexing import (
delete_nested_value,
is_nested_path,

View file

@ -5,15 +5,10 @@ These tests verify that ssl_verify parameters are correctly propagated
through the call stack without requiring live API credentials.
"""
import sys
from pathlib import Path
from unittest.mock import Mock, patch
import pytest
# Add litellm to path
sys.path.insert(0, str(Path(__file__).parent))
import litellm.proxy.guardrails.guardrail_hooks.aim.aim as _aim_module
import litellm.proxy.guardrails.guardrail_hooks.cato_networks.cato_networks as _cato_networks_module
from litellm.llms.bedrock.base_aws_llm import BaseAWSLLM

View file

@ -1,15 +1,9 @@
from __future__ import annotations
import os
import sys
from types import SimpleNamespace
import pytest
_REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
if _REPO_ROOT not in sys.path:
sys.path.insert(0, _REPO_ROOT)
from tests._vcr_conftest_common import ( # noqa: E402
KEY_FINGERPRINT_HEADER,
KEY_FINGERPRINT_MATCHER_NAME,