Skip to content

[BUG] skip _try_credentials check for to_gbq #207

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
merged 3 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Changelog
(:issue:`128`)
- Reduced verbosity of logging from ``read_gbq``, particularly for short
queries. (:issue:`201`)
- Avoid ``SELECT 1`` query when running ``to_gbq``. (:issue:`202`)

.. _changelog-0.6.0:

Expand Down
27 changes: 20 additions & 7 deletions pandas_gbq/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@


def get_credentials(
private_key=None, project_id=None, reauth=False, auth_local_webserver=False
private_key=None,
project_id=None,
reauth=False,
auth_local_webserver=False,
try_credentials=None,
):
if try_credentials is None:
try_credentials = _try_credentials

if private_key:
return get_service_account_credentials(private_key)

# Try to retrieve Application Default Credentials
credentials, default_project = get_application_default_credentials(
project_id=project_id
try_credentials, project_id=project_id
)

if credentials:
return credentials, default_project

credentials = get_user_account_credentials(
try_credentials,
project_id=project_id,
reauth=reauth,
auth_local_webserver=auth_local_webserver,
Expand Down Expand Up @@ -79,7 +87,7 @@ def get_service_account_credentials(private_key):
)


def get_application_default_credentials(project_id=None):
def get_application_default_credentials(try_credentials, project_id=None):
"""
This method tries to retrieve the "default application credentials".
This could be useful for running code on Google Cloud Platform.
Expand Down Expand Up @@ -111,10 +119,11 @@ def get_application_default_credentials(project_id=None):
# used with BigQuery. For example, we could be running on a GCE instance
# that does not allow the BigQuery scopes.
billing_project = project_id or default_project
return _try_credentials(billing_project, credentials), billing_project
return try_credentials(billing_project, credentials), billing_project


def get_user_account_credentials(
try_credentials,
project_id=None,
reauth=False,
auth_local_webserver=False,
Expand Down Expand Up @@ -151,7 +160,9 @@ def get_user_account_credentials(
os.rename("bigquery_credentials.dat", credentials_path)

credentials = load_user_account_credentials(
project_id=project_id, credentials_path=credentials_path
try_credentials,
project_id=project_id,
credentials_path=credentials_path,
)

client_config = {
Expand Down Expand Up @@ -187,7 +198,9 @@ def get_user_account_credentials(
return credentials


def load_user_account_credentials(project_id=None, credentials_path=None):
def load_user_account_credentials(
try_credentials, project_id=None, credentials_path=None
):
"""
Loads user account credentials from a local file.

Expand Down Expand Up @@ -230,7 +243,7 @@ def load_user_account_credentials(project_id=None, credentials_path=None):
request = google.auth.transport.requests.Request()
credentials.refresh(request)

return _try_credentials(project_id, credentials)
return try_credentials(project_id, credentials)


def get_default_credentials_path():
Expand Down
5 changes: 5 additions & 0 deletions pandas_gbq/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def __init__(
auth_local_webserver=False,
dialect="legacy",
location=None,
try_credentials=None,
):
from google.api_core.exceptions import GoogleAPIError
from google.api_core.exceptions import ClientError
Expand All @@ -189,6 +190,7 @@ def __init__(
project_id=project_id,
reauth=reauth,
auth_local_webserver=auth_local_webserver,
try_credentials=try_credentials,
)

if self.project_id is None:
Expand Down Expand Up @@ -804,6 +806,9 @@ def to_gbq(
private_key=private_key,
auth_local_webserver=auth_local_webserver,
location=location,
# Avoid reads when writing tables.
# https://github.com/pydata/pandas-gbq/issues/202
try_credentials=lambda project, creds: creds,
)
dataset_id, table_id = destination_table.rsplit(".", 1)

Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def mock_default_credentials(scopes=None, request=None):
google.auth.credentials.Credentials
)

def mock_load_credentials(project_id=None, credentials_path=None):
def mock_load_credentials(
try_credentials, project_id=None, credentials_path=None
):
return mock_user_credentials

monkeypatch.setattr(
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ def test_to_gbq_with_verbose_old_pandas_no_warnings(recwarn, min_bq_version):
assert len(recwarn) == 0


def test_to_gbq_doesnt_run_query(recwarn, min_bq_version):
try:
gbq.to_gbq(
DataFrame([[1]]), "dataset.tablename", project_id="my-project"
)
except gbq.TableCreationError:
pass

gbq.GbqConnector.get_client(None).query.assert_not_called()


def test_read_gbq_with_no_project_id_given_should_fail(monkeypatch):
from pandas_gbq import auth

Expand Down