Repository navigation
fix(pandas-gbq): reuse bigquery_client in GbqConnector.get_client and support client_options #19295
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/pandas-gbq-universe
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
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,124 @@ | ||
| # Copyright 2026 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. | ||
|
|
||
| """Unit tests for client_options, universe_domain, and bigquery_client reuse.""" | ||
|
|
||
| from types import MappingProxyType | ||
| from unittest import mock | ||
|
|
||
| import pandas as pd | ||
| import pytest | ||
| from google.api_core import client_options as client_options_lib | ||
| from google.auth import credentials as auth_credentials | ||
|
|
||
| import pandas_gbq | ||
| from pandas_gbq import gbq_connector | ||
|
|
||
| CUSTOM_UNIVERSE = "apis-tpclp.goog" | ||
|
|
||
|
|
||
| def test_gbq_connector_get_client_reuses_supplied_bigquery_client(): | ||
| supplied_client = mock.Mock( | ||
| project="tpc-proj", | ||
| _credentials=auth_credentials.AnonymousCredentials(), | ||
| ) | ||
|
|
||
| connector = gbq_connector.GbqConnector( | ||
| project_id=None, | ||
| bigquery_client=supplied_client, | ||
| ) | ||
|
|
||
| assert connector.client is supplied_client | ||
| assert connector.get_client() is supplied_client | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "opts", | ||
| [ | ||
| pytest.param( | ||
| client_options_lib.ClientOptions(universe_domain=CUSTOM_UNIVERSE), | ||
| id="client_options_instance", | ||
| ), | ||
| pytest.param({"universe_domain": CUSTOM_UNIVERSE}, id="dict"), | ||
| pytest.param( | ||
| MappingProxyType({"universe_domain": CUSTOM_UNIVERSE}), | ||
| id="mapping_proxy", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_gbq_connector_forwards_client_options(opts): | ||
| fake_creds = auth_credentials.AnonymousCredentials() | ||
| with mock.patch.object( | ||
| gbq_connector.FEATURES, "bigquery_try_import" | ||
| ) as mock_bq_import: | ||
| mock_bq = mock_bq_import.return_value | ||
| connector = gbq_connector.GbqConnector( | ||
| project_id="tpc-proj", | ||
| credentials=fake_creds, | ||
| client_options=opts, | ||
| ) | ||
|
|
||
| mock_bq.Client.assert_called_once() | ||
| assert mock_bq.Client.call_args.kwargs["client_options"] is opts | ||
| assert connector.get_client() is mock_bq.Client.return_value | ||
|
|
||
|
|
||
| def test_gbq_connector_get_client_fallback_forwards_client_options(): | ||
| opts = client_options_lib.ClientOptions(universe_domain=CUSTOM_UNIVERSE) | ||
| fake_creds = auth_credentials.AnonymousCredentials() | ||
| with mock.patch.object( | ||
| gbq_connector.FEATURES, "bigquery_try_import" | ||
| ) as mock_bq_import: | ||
| mock_bq = mock_bq_import.return_value | ||
| connector = gbq_connector.GbqConnector( | ||
| project_id="tpc-proj", | ||
| credentials=fake_creds, | ||
| client_options=opts, | ||
| ) | ||
| connector.client = None | ||
| connector.get_client() | ||
|
|
||
| assert mock_bq.Client.call_count == 2 | ||
| assert mock_bq.Client.call_args.kwargs["client_options"] is opts | ||
|
|
||
|
|
||
| def test_read_gbq_forwards_client_options(): | ||
| opts = {"universe_domain": CUSTOM_UNIVERSE} | ||
| with mock.patch("pandas_gbq.gbq.GbqConnector") as mock_connector_cls: | ||
| mock_connector_cls.return_value.run_query.return_value = pd.DataFrame() | ||
| pandas_gbq.read_gbq( | ||
| "SELECT 1", | ||
| project_id="tpc-proj", | ||
| client_options=opts, | ||
| ) | ||
|
|
||
| assert mock_connector_cls.call_args.kwargs["client_options"] is opts | ||
|
|
||
|
|
||
| def test_to_gbq_forwards_client_options(): | ||
| opts = client_options_lib.ClientOptions(universe_domain=CUSTOM_UNIVERSE) | ||
| df = pd.DataFrame({"a": [1]}) | ||
| with mock.patch("pandas_gbq.gbq.GbqConnector") as mock_connector_cls: | ||
| mock_connector = mock_connector_cls.return_value | ||
| mock_connector.project_id = "tpc-proj" | ||
| mock_connector.client.get_table.side_effect = Exception("stop") | ||
| with pytest.raises(Exception, match="stop"): | ||
| pandas_gbq.to_gbq( | ||
| df, | ||
| "ds.tbl", | ||
| project_id="tpc-proj", | ||
| client_options=opts, | ||
| ) | ||
|
|
||
| assert mock_connector_cls.call_args.kwargs["client_options"] is opts |
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.
Using
getattr(self, ...)forclientandclient_optionsis unnecessary and inconsistent with the direct attribute access used forself.user_agent,self.rfc9110_delimiter,self.project_id, andself.credentialsin the same method. Since bothself.clientandself.client_optionsare guaranteed to be initialized in__init__, they should be accessed directly.References
getattr(self, 'attribute', None)checks for attributes that are guaranteed to be initialized in the class's__init__method, as it adds unnecessary complexity.