-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Fix pylint complaining about too many if-else branch in get_openai_cl… #46536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
howieleung
merged 3 commits into
feature/azure-ai-projects/2.2.0
from
howie/get-openai-client-pylint
Apr 28, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
sdk/ai/azure-ai-projects/tests/responses/openai_test_helpers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # pylint: disable=line-too-long,useless-suppression | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| """Shared helpers for unit-testing AIProjectClient.get_openai_client (sync and async). | ||
|
|
||
| These helpers build lightweight client stubs that bypass the real ``__init__`` so unit | ||
| tests can target individual branches of ``get_openai_client`` without making any | ||
| network calls. | ||
| """ | ||
|
|
||
| from typing import Optional | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from azure.ai.projects import AIProjectClient | ||
| from azure.ai.projects.aio import AIProjectClient as AsyncAIProjectClient | ||
|
|
||
| ENDPOINT = "https://myaccount.services.ai.azure.com/api/projects/myproject" | ||
| API_VERSION = "2025-01-01" | ||
|
|
||
| # Patch targets used by tests to swap in mocked OpenAI/AsyncOpenAI constructors | ||
| # and bearer-token providers. | ||
| SYNC_OPENAI_PATCH = "azure.ai.projects._patch.OpenAI" | ||
| ASYNC_OPENAI_PATCH = "azure.ai.projects.aio._patch.AsyncOpenAI" | ||
| SYNC_TOKEN_PROVIDER_PATCH = "azure.ai.projects._patch.get_bearer_token_provider" | ||
| ASYNC_TOKEN_PROVIDER_PATCH = "azure.ai.projects.aio._patch.get_bearer_token_provider" | ||
|
|
||
|
|
||
| def make_sync_client( | ||
| allow_preview: bool = True, | ||
| console_logging: bool = False, | ||
| custom_user_agent: Optional[str] = None, | ||
| ) -> AIProjectClient: | ||
| """Return a minimal sync AIProjectClient stub suitable for unit-testing get_openai_client.""" | ||
| client = AIProjectClient.__new__(AIProjectClient) | ||
| client._config = MagicMock() | ||
| client._config.endpoint = ENDPOINT | ||
| client._config.allow_preview = allow_preview | ||
| client._config.api_version = API_VERSION | ||
| client._config.credential = MagicMock() | ||
| client._console_logging_enabled = console_logging | ||
| client._custom_user_agent = custom_user_agent | ||
| return client | ||
|
|
||
|
|
||
| def make_async_client( | ||
| allow_preview: bool = True, | ||
| console_logging: bool = False, | ||
| custom_user_agent: Optional[str] = None, | ||
| ) -> AsyncAIProjectClient: | ||
| """Return a minimal async AIProjectClient stub suitable for unit-testing get_openai_client.""" | ||
| client = AsyncAIProjectClient.__new__(AsyncAIProjectClient) | ||
| client._config = MagicMock() | ||
| client._config.endpoint = ENDPOINT | ||
| client._config.allow_preview = allow_preview | ||
| client._config.api_version = API_VERSION | ||
| client._config.credential = MagicMock() | ||
| client._console_logging_enabled = console_logging | ||
| client._custom_user_agent = custom_user_agent | ||
| return client | ||
|
|
||
|
|
||
| def mock_openai(user_agent: str = "openai/1.0"): | ||
| """Return ``(mock_class, mock_instance)`` where ``mock_class`` acts as the OpenAI constructor.""" | ||
| instance = MagicMock() | ||
| instance.user_agent = user_agent | ||
| mock_cls = MagicMock(return_value=instance) | ||
| return mock_cls, instance |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.