From a6df01caecea29c1e6778d3b130c0beeef37788d Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Tue, 17 Feb 2026 20:31:10 -0300 Subject: [PATCH] fix: remove importlib.reload calls that cause cross-test class-reference staleness Two test files were reloading modules in setup_method/fixtures, which caused class-reference staleness for subsequent tests in the same worker: 1. test_huggingface_embedding_handler.py reloaded litellm.llms.custom_httpx.http_handler, creating a new HTTPHandler class. Subsequent tests (e.g. hosted_vllm embedding) created client = HTTPHandler() from the new class, but llm_http_handler.py still held the old class reference. isinstance(client, HTTPHandler) returned False, so a new unpatched client was used and client.post was never called. 2. test_vertex_ai_rerank_integration.py reloaded litellm.llms.vertex_ai.rerank.transformation in setup_method, creating a new VertexAIRerankConfig class. The transformation test file's module-level import still referenced the old class, so @patch('...VertexAIRerankConfig._ensure_access_token') patched the new class while self.config was an instance of the old class, leaving the mock unapplied and hitting real Google credentials. Fix: remove the reload calls. The module-level class references are stable across tests within a worker; the reloads were solving a problem that doesn't exist and actively created cross-test contamination. Co-Authored-By: Claude Sonnet 4.6 --- .../test_huggingface_embedding_handler.py | 20 ++----------------- .../test_vertex_ai_rerank_integration.py | 10 +--------- 2 files changed, 3 insertions(+), 27 deletions(-) diff --git a/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py b/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py index 090792d4f0..f6bc983df0 100644 --- a/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py +++ b/tests/test_litellm/llms/huggingface/embedding/test_huggingface_embedding_handler.py @@ -1,4 +1,3 @@ -import importlib import json import os import sys @@ -16,22 +15,7 @@ MOCK_EMBEDDING_RESPONSE = [[0.1, 0.2, 0.3, 0.4, 0.5]] @pytest.fixture -def reload_huggingface_modules(): - """ - Reload modules to ensure fresh references after conftest reloads litellm. - This ensures the HTTPHandler class being patched is the same one used by - the embedding handler during parallel test execution. - """ - import litellm.llms.custom_httpx.http_handler as http_handler_module - import litellm.llms.huggingface.embedding.handler as hf_embedding_handler_module - - importlib.reload(http_handler_module) - importlib.reload(hf_embedding_handler_module) - yield - - -@pytest.fixture -def mock_embedding_http_handler(reload_huggingface_modules): +def mock_embedding_http_handler(): """Fixture to mock the HTTP handler for embedding tests""" with patch("litellm.llms.custom_httpx.http_handler.HTTPHandler.post") as mock_post: mock_response = MagicMock() @@ -43,7 +27,7 @@ def mock_embedding_http_handler(reload_huggingface_modules): @pytest.fixture -def mock_embedding_async_http_handler(reload_huggingface_modules): +def mock_embedding_async_http_handler(): """Fixture to mock the async HTTP handler for embedding tests""" with patch("litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post", new_callable=AsyncMock) as mock_post: mock_response = MagicMock() diff --git a/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py b/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py index dd0a3e36e4..1acdadf541 100644 --- a/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py +++ b/tests/test_litellm/llms/vertex_ai/rerank/test_vertex_ai_rerank_integration.py @@ -2,7 +2,6 @@ Integration tests for Vertex AI rerank functionality. These tests demonstrate end-to-end usage of the Vertex AI rerank feature. """ -import importlib import os from unittest.mock import MagicMock, patch @@ -14,14 +13,7 @@ from litellm.llms.vertex_ai.rerank.transformation import VertexAIRerankConfig class TestVertexAIRerankIntegration: def setup_method(self): - # Reload modules to ensure fresh references after conftest reloads litellm. - # This ensures the class being patched is the same one used by the tests. - import litellm.llms.vertex_ai.rerank.transformation as rerank_transformation_module - importlib.reload(rerank_transformation_module) - - # Re-import after reload to get the fresh class - from litellm.llms.vertex_ai.rerank.transformation import VertexAIRerankConfig as FreshConfig - self.config = FreshConfig() + self.config = VertexAIRerankConfig() self.model = "semantic-ranker-default@latest" @patch('litellm.llms.vertex_ai.rerank.transformation.VertexAIRerankConfig._ensure_access_token')