Skip to content

project_id as optional #127

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 25 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c85a685
project_id is optional
max-sixty Feb 20, 2018
8786e6d
Merge branch 'master' into default-project
max-sixty Mar 31, 2018
17a2ede
gitignore
max-sixty Mar 31, 2018
86b6fc5
docstring
max-sixty Mar 31, 2018
11c146e
don't skip if no project
max-sixty Mar 31, 2018
817c63a
formatting
max-sixty Mar 31, 2018
f0a8d36
add marks to allow test selecting
max-sixty Apr 1, 2018
4ecfcfc
assign mark to test
max-sixty Apr 1, 2018
c402952
explicitly chose with auth to do from travis
max-sixty Apr 1, 2018
476bcf1
too hasty to change the pandas installation
max-sixty Apr 1, 2018
ef59bca
do what needs doing
max-sixty Apr 1, 2018
2112acd
Fixing style errors.
stickler-ci Apr 1, 2018
6cc99f6
docstring & import order
max-sixty Apr 1, 2018
56436ff
correct mark expression
max-sixty Apr 1, 2018
5347858
project not required only if default creds available
max-sixty Apr 1, 2018
cd9b37d
remove any more branching in travis
max-sixty Apr 1, 2018
fa97b0f
Merge branch 'master' of https://github.com/pydata/pandas-gbq into de…
max-sixty Apr 2, 2018
a7f6c43
google import inline
max-sixty Apr 2, 2018
2d50519
Merge remote-tracking branch 'upstream/master' into pr-127-maxim-lian…
tswast Apr 25, 2018
7932c59
Use tuple for credentials & project for default project detection.
tswast Apr 25, 2018
31e001f
lint errors.
tswast Apr 25, 2018
08477cc
Remove extra project detection.
tswast Apr 25, 2018
1f1f2c4
Update bad_project_id test to query actual data.
tswast Apr 25, 2018
d920959
Skip credentials tests if key not present.
tswast Apr 25, 2018
a3e6d2f
DOC: add project_id optional to changelog
tswast Apr 25, 2018
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
.ipynb_checkpoints
.tags
.pytest_cache
.testmondata
.testmon*
.vscode/

# Docs #
########
Expand Down
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ sudo: false
language: python

env:
- PYTHON=2.7 PANDAS=0.19.2 COVERAGE='false' LINT='true'
- PYTHON=3.5 PANDAS=0.18.1 COVERAGE='true' LINT='false'
- PYTHON=3.6 PANDAS=0.20.1 COVERAGE='false' LINT='false'
- PYTHON=3.6 PANDAS=MASTER COVERAGE='false' LINT='true'
- PYTHON=2.7 PANDAS=0.19.2 COVERAGE='false' LINT='true' AUTH='s_path'
- PYTHON=3.5 PANDAS=0.18.1 COVERAGE='true' LINT='false' AUTH='s_path'
- PYTHON=3.6 PANDAS=0.20.1 COVERAGE='false' LINT='false' AUTH='s_cred'
- PYTHON=3.6 PANDAS=MASTER COVERAGE='false' LINT='true' AUTH='s_cred'

before_install:
- echo "before_install"
Expand Down Expand Up @@ -42,6 +42,7 @@ install:
- python setup.py install

script:
- pytest -v --cov=pandas_gbq --cov-report xml:/tmp/pytest-cov.xml pandas_gbq
- if [[ $AUTH == 's_path' ]]; then pytest -m 'not local_auth and not s_cred_auth' -v --cov=pandas_gbq --cov-report xml:/tmp/pytest-cov.xml pandas_gbq ; fi
- if [[ $AUTH == 's_cred' ]]; then pytest -m 'not local_auth and not s_path_auth' -v --cov=pandas_gbq --cov-report xml:/tmp/pytest-cov.xml pandas_gbq ; fi
- if [[ $COVERAGE == 'true' ]]; then codecov ; fi
- if [[ $LINT == 'true' ]]; then flake8 pandas_gbq -v ; fi
12 changes: 6 additions & 6 deletions pandas_gbq/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ def __init__(self, project_id, reauth=False,
from google.api_core.exceptions import GoogleAPIError
from google.api_core.exceptions import ClientError
self.http_error = (ClientError, GoogleAPIError)
if not project_id:
from google.auth import default
_, project_id = default()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, this could cause us to go through the whole "application default credentials" flow twice, which could be quite slow. (It checks for env var, checks for GCE, checks for App Engine, ...)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, and it's not cached?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah not great:


In [4]: %timeit google.auth.default()
573 ms ± 14.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about changing those methods to return 2-tuples?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tuples would be reasonable. The other (more disruptive, probably) option would be to pass in the project to the credentials methods and have them return a google.cloud.bigquery.Client.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this PR to tuples as we discussed.

self.project_id = project_id
self.reauth = reauth
self.private_key = private_key
Expand Down Expand Up @@ -752,7 +755,7 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
----------
query : str
SQL-Like Query to return data values
project_id : str
project_id : str (optional when available in environment)
Google BigQuery Account project ID.
index_col : str (optional)
Name of result column to use for index in results DataFrame
Expand Down Expand Up @@ -810,9 +813,6 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,

_test_google_api_imports()

if not project_id:
raise TypeError("Missing required parameter: project_id")

if dialect not in ('legacy', 'standard'):
raise ValueError("'{0}' is not valid for dialect".format(dialect))

Expand Down Expand Up @@ -860,7 +860,7 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
return final_df


def to_gbq(dataframe, destination_table, project_id, chunksize=None,
def to_gbq(dataframe, destination_table, project_id=None, chunksize=None,
verbose=None, reauth=False, if_exists='fail', private_key=None,
auth_local_webserver=False, table_schema=None):
"""Write a DataFrame to a Google BigQuery table.
Expand Down Expand Up @@ -892,7 +892,7 @@ def to_gbq(dataframe, destination_table, project_id, chunksize=None,
DataFrame to be written
destination_table : string
Name of table to be written, in the form 'dataset.tablename'
project_id : str
project_id : str (optional when available in environment)
Google BigQuery Account project ID.
chunksize : int (default None)
Number of rows to be inserted in each chunk from the dataframe. Use
Expand Down
54 changes: 21 additions & 33 deletions pandas_gbq/tests/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
TABLE_ID = 'new_test'


def _skip_local_auth_if_in_travis_env():
if _in_travis_environment():
pytest.skip("Cannot run local auth in travis environment")


def _skip_if_no_private_key_path():
if not _get_private_key_path():
pytest.skip("Cannot run integration tests without a "
Expand All @@ -52,12 +47,8 @@ def _get_dataset_prefix_random():


def _get_project_id():

project = os.environ.get('GBQ_PROJECT_ID')
if not project:
pytest.skip(
"Cannot run integration tests without a project id")
return project
return (os.environ.get('GBQ_PROJECT_ID')
or os.environ.get('GOOGLE_CLOUD_PROJECT')) # noqa


def _get_private_key_path():
Expand Down Expand Up @@ -87,9 +78,12 @@ def _test_imports():
gbq._test_google_api_imports()


@pytest.fixture
def project():
return _get_project_id()
@pytest.fixture(params=['env'])
def project(request):
if request.param == 'env':
return _get_project_id()
elif request.param == 'none':
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this didn't get used, but could param over it in the future)

return None


def _check_if_can_get_correct_default_credentials():
Expand All @@ -101,11 +95,13 @@ def _check_if_can_get_correct_default_credentials():
from google.auth.exceptions import DefaultCredentialsError

try:
credentials, _ = google.auth.default(scopes=[gbq.GbqConnector.scope])
credentials, project = google.auth.default(
scopes=[gbq.GbqConnector.scope])
except (DefaultCredentialsError, IOError):
return False

return gbq._try_credentials(_get_project_id(), credentials) is not None
return gbq._try_credentials(
project or _get_project_id(), credentials) is not None


def clean_gbq_environment(dataset_prefix, private_key=None):
Expand Down Expand Up @@ -173,21 +169,18 @@ def test_generate_bq_schema_deprecated():
gbq.generate_bq_schema(df)


@pytest.fixture(params=['local', 'service_path', 'service_creds'])
@pytest.fixture(params=[
pytest.param('local', marks=pytest.mark.local_auth),
pytest.param('service_path', marks=pytest.mark.s_path_auth),
pytest.param('service_creds', marks=pytest.mark.s_cred_auth),
])
def auth_type(request):

auth = request.param

if auth == 'local':

if _in_travis_environment():
pytest.skip("Cannot run local auth in travis environment")

pass
elif auth == 'service_path':

if _in_travis_environment():
pytest.skip("Only run one auth type in Travis to save time")

_skip_if_no_private_key_path()
elif auth == 'service_creds':
_skip_if_no_private_key_contents()
Expand Down Expand Up @@ -238,13 +231,12 @@ def test_should_be_able_to_get_results_from_query(self, gbq_connector):
assert pages is not None


@pytest.mark.local_auth
class TestGBQConnectorIntegrationWithLocalUserAccountAuth(object):

@pytest.fixture(autouse=True)
def setup(self, project):

_skip_local_auth_if_in_travis_env()

self.sut = gbq.GbqConnector(project, auth_local_webserver=True)

def test_get_application_default_credentials_does_not_throw_error(self):
Expand Down Expand Up @@ -310,12 +302,8 @@ def test_to_gbq_should_fail_if_invalid_table_name_passed(self):
with pytest.raises(gbq.NotFoundException):
gbq.to_gbq(DataFrame(), 'invalid_table_name', project_id="1234")

def test_to_gbq_with_no_project_id_given_should_fail(self):
with pytest.raises(TypeError):
gbq.to_gbq(DataFrame(), 'dataset.tablename')

def test_read_gbq_with_no_project_id_given_should_fail(self):
with pytest.raises(TypeError):
def test_read_gbq_with_no_project_id_given_should_pass(self, credentials):
if _check_if_can_get_correct_default_credentials():
gbq.read_gbq('SELECT 1')

def test_that_parse_data_works_properly(self):
Expand Down