-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[WIP]fix: validate transport types in ConfigClient and ConfigAsyncClient #15495
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
Draft
chalmerlowe
wants to merge
1
commit into
main
Choose a base branch
from
fix/config-client-transport-validation-2567066881583631619
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -779,6 +779,7 @@ def __init__( | |
| ] = None, | ||
| client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None, | ||
| client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, | ||
| _allow_async_transport: bool = False, | ||
| ) -> None: | ||
| """Instantiates the config client. | ||
|
|
||
|
|
@@ -870,6 +871,18 @@ def __init__( | |
| # Ordinarily, we provide the transport, but allowing a custom transport | ||
| # instance provides an extensibility point for unusual situations. | ||
| transport_provided = isinstance(transport, ConfigTransport) | ||
|
|
||
| # Raise an exception if the async transport is provided. | ||
| if ( | ||
| not _allow_async_transport | ||
| and transport_provided | ||
| and isinstance(transport, ConfigGrpcAsyncIOTransport) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ): | ||
| raise ValueError( | ||
| "The `ConfigClient` does not support async transports. " | ||
| "Please use `ConfigAsyncClient` instead." | ||
| ) | ||
|
|
||
| if transport_provided: | ||
| # transport is a ConfigTransport instance. | ||
| if credentials or self._client_options.credentials_file or api_key_value: | ||
|
|
@@ -909,6 +922,18 @@ def __init__( | |
| if isinstance(transport, str) or transport is None | ||
| else cast(Callable[..., ConfigTransport], transport) | ||
| ) | ||
|
|
||
| # Raise an exception if the async transport is provided. | ||
| if ( | ||
| not _allow_async_transport | ||
| and isinstance(transport, str) | ||
| and transport == "grpc_asyncio" | ||
| ): | ||
| raise ValueError( | ||
| "The `ConfigClient` does not support async transports. " | ||
| "Please use `ConfigAsyncClient` instead." | ||
| ) | ||
|
Comment on lines
+927
to
+935
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| # initialize with the provided callable or the passed in class | ||
| self._transport = transport_init( | ||
| credentials=credentials, | ||
|
|
||
79 changes: 79 additions & 0 deletions
79
packages/google-cloud-config/tests/unit/gapic/config_v1/test_client_validation.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,79 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| import pytest | ||
| from unittest import mock | ||
| from google.cloud.config_v1 import ConfigClient, ConfigAsyncClient | ||
| from google.cloud.config_v1.services.config import transports | ||
| from google.auth import credentials as ga_credentials | ||
|
|
||
| def test_config_client_validates_grpc_asyncio_string(): | ||
| creds = ga_credentials.AnonymousCredentials() | ||
| with pytest.raises(ValueError) as excinfo: | ||
| ConfigClient(credentials=creds, transport="grpc_asyncio") | ||
| assert "ConfigClient" in str(excinfo.value) | ||
| assert "async transports" in str(excinfo.value) | ||
|
|
||
| def test_config_client_validates_grpc_asyncio_object(): | ||
| creds = ga_credentials.AnonymousCredentials() | ||
| transport = transports.ConfigGrpcAsyncIOTransport(credentials=creds) | ||
| with pytest.raises(ValueError) as excinfo: | ||
| ConfigClient(transport=transport) | ||
| assert "ConfigClient" in str(excinfo.value) | ||
| assert "async transports" in str(excinfo.value) | ||
|
|
||
| def test_config_client_allows_custom_transport(): | ||
| creds = ga_credentials.AnonymousCredentials() | ||
| class CustomTransport(transports.ConfigTransport): | ||
| pass | ||
| transport = CustomTransport(credentials=creds) | ||
| # Should not raise | ||
| ConfigClient(transport=transport) | ||
|
|
||
| def test_config_async_client_validates_grpc_string(): | ||
| creds = ga_credentials.AnonymousCredentials() | ||
| with pytest.raises(ValueError) as excinfo: | ||
| ConfigAsyncClient(credentials=creds, transport="grpc") | ||
| assert "ConfigAsyncClient" in str(excinfo.value) | ||
| assert "sync transports" in str(excinfo.value) | ||
|
|
||
| def test_config_async_client_validates_rest_string(): | ||
| creds = ga_credentials.AnonymousCredentials() | ||
| with pytest.raises(ValueError) as excinfo: | ||
| ConfigAsyncClient(credentials=creds, transport="rest") | ||
| assert "ConfigAsyncClient" in str(excinfo.value) | ||
| assert "sync transports" in str(excinfo.value) | ||
|
|
||
| def test_config_async_client_validates_grpc_object(): | ||
| creds = ga_credentials.AnonymousCredentials() | ||
| transport = transports.ConfigGrpcTransport(credentials=creds) | ||
| with pytest.raises(ValueError) as excinfo: | ||
| ConfigAsyncClient(transport=transport) | ||
| assert "ConfigAsyncClient" in str(excinfo.value) | ||
| assert "sync transports" in str(excinfo.value) | ||
|
|
||
| def test_config_async_client_allows_custom_transport(): | ||
| creds = ga_credentials.AnonymousCredentials() | ||
| class CustomTransport(transports.ConfigTransport): | ||
| pass | ||
| transport = CustomTransport(credentials=creds) | ||
| # Should not raise | ||
| ConfigAsyncClient(transport=transport) | ||
|
|
||
| def test_config_async_client_initialization_happy_path(): | ||
| creds = ga_credentials.AnonymousCredentials() | ||
| # Should not raise | ||
| client = ConfigAsyncClient(credentials=creds) | ||
| assert isinstance(client._client, ConfigClient) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
transport_providedvariable is redundant here. The condition can be simplified by removing this variable and its usage, asisinstance(transport, (ConfigGrpcTransport, ConfigRestTransport))already handles cases wheretransportis not a transport object (like a string).