From 77890478a1fd6b0ea433177163f027b26880d21f Mon Sep 17 00:00:00 2001 From: "ZOU Yi (BD/SWD-WDE1)" Date: Mon, 7 Sep 2026 13:58:16 +0800 Subject: [PATCH] refactor(sap): inject http client into deployment_url to drop internal patch Replace the test's patch of litellm.module_level_client with dependency injection via a new optional _http_client attribute, clearing the TQ008 test-quality violation. Production still falls back to the module-level client when none is injected. --- litellm/llms/sap/chat/transformation.py | 6 +++--- .../llms/sap/chat/test_sap_stream_chunk_validation.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/litellm/llms/sap/chat/transformation.py b/litellm/llms/sap/chat/transformation.py index affb6193287..f41a4c3f336 100755 --- a/litellm/llms/sap/chat/transformation.py +++ b/litellm/llms/sap/chat/transformation.py @@ -18,6 +18,7 @@ if TYPE_CHECKING: import tiktoken from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj + from litellm.llms.custom_httpx.http_handler import HTTPHandler LiteLLMLoggingObj = _LiteLLMLoggingObj else: @@ -136,6 +137,7 @@ class GenAIHubOrchestrationConfig(OpenAIGPTConfig): self.token_creator = None self._base_url = None self._resource_group = None + self._http_client: HTTPHandler | None = None def run_env_setup(self, service_key: str | None = None) -> None: try: @@ -169,9 +171,7 @@ class GenAIHubOrchestrationConfig(OpenAIGPTConfig): @cached_property def deployment_url(self) -> str: - # Keep a short, tight client lifecycle here to avoid fd leaks - client: Final = litellm.module_level_client - # with httpx.Client(timeout=30) as client: + client: Final = self._http_client if self._http_client is not None else litellm.module_level_client deployments: Final = client.get(f"{self.base_url}/lm/deployments", headers=self.headers).json() valid: Final[list[tuple[str, str]]] = [] for dep in deployments.get("resources", []): diff --git a/tests/test_litellm/llms/sap/chat/test_sap_stream_chunk_validation.py b/tests/test_litellm/llms/sap/chat/test_sap_stream_chunk_validation.py index b2dc73cf846..632130c23ea 100644 --- a/tests/test_litellm/llms/sap/chat/test_sap_stream_chunk_validation.py +++ b/tests/test_litellm/llms/sap/chat/test_sap_stream_chunk_validation.py @@ -10,7 +10,7 @@ Regression tests for: configured resource group contains no orchestration deployment. """ -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock import pytest @@ -128,10 +128,10 @@ def test_deployment_url_raises_404_when_no_orchestration_deployment(): mock_client = MagicMock() mock_client.get.return_value.json.return_value = {"resources": []} + config._http_client = mock_client - with patch("litellm.module_level_client", mock_client): - with pytest.raises(GenAIHubOrchestrationError) as exc_info: - _ = config.deployment_url + with pytest.raises(GenAIHubOrchestrationError) as exc_info: + _ = config.deployment_url assert exc_info.value.status_code == 404 assert "fake-group" in exc_info.value.message