Skip to content

Commit 1fb6c00

Browse files
authored
BUG: only show verbose warning for new pandas versions. (#158)
* BUG: only show verbose warning for new pandas versions. * TST: add unit tests for verbose deprecation warning
1 parent 9ced97b commit 1fb6c00

File tree

3 files changed

+167
-6
lines changed

3 files changed

+167
-6
lines changed

docs/source/changelog.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
0.4.1 / 2018-04-05
5+
------------------
6+
7+
- Only show ``verbose`` deprecation warning if Pandas version does not
8+
populate it. (:issue:`157`)
9+
410
0.4.0 / 2018-04-03
511
------------------
612

pandas_gbq/gbq.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414

1515

1616
BIGQUERY_INSTALLED_VERSION = None
17+
SHOW_VERBOSE_DEPRECATION = False
1718

1819

1920
def _check_google_client_version():
20-
global BIGQUERY_INSTALLED_VERSION
21+
global BIGQUERY_INSTALLED_VERSION, SHOW_VERBOSE_DEPRECATION
2122

2223
try:
2324
import pkg_resources
@@ -36,6 +37,14 @@ def _check_google_client_version():
3637
'current version {1}'.format(
3738
bigquery_minimum_version, BIGQUERY_INSTALLED_VERSION))
3839

40+
# Add check for Pandas version before showing deprecation warning.
41+
# https://github.com/pydata/pandas-gbq/issues/157
42+
pandas_installed_version = pkg_resources.get_distribution(
43+
'pandas').parsed_version
44+
pandas_version_wo_verbosity = pkg_resources.parse_version('0.23.0')
45+
SHOW_VERBOSE_DEPRECATION = (
46+
pandas_installed_version >= pandas_version_wo_verbosity)
47+
3948

4049
def _test_google_api_imports():
4150

@@ -791,14 +800,15 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
791800
DataFrame representing results of query
792801
793802
"""
794-
if verbose is not None:
803+
804+
_test_google_api_imports()
805+
806+
if verbose is not None and SHOW_VERBOSE_DEPRECATION:
795807
warnings.warn(
796808
"verbose is deprecated and will be removed in "
797809
"a future version. Set logging level in order to vary "
798810
"verbosity", FutureWarning, stacklevel=1)
799811

800-
_test_google_api_imports()
801-
802812
if not project_id:
803813
raise TypeError("Missing required parameter: project_id")
804814

@@ -920,7 +930,7 @@ def to_gbq(dataframe, destination_table, project_id, chunksize=None,
920930

921931
_test_google_api_imports()
922932

923-
if verbose is not None:
933+
if verbose is not None and SHOW_VERBOSE_DEPRECATION:
924934
warnings.warn(
925935
"verbose is deprecated and will be removed in "
926936
"a future version. Set logging level in order to vary "

pandas_gbq/tests/test_gbq.py

+146-1
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,40 @@ def test_get_user_account_credentials_returns_credentials(self):
282282

283283
class TestGBQUnit(object):
284284

285-
def test_should_return_credentials_path_set_by_env_var(self):
285+
@pytest.fixture(autouse=True)
286+
def mock_bigquery_client(self, monkeypatch):
287+
import google.cloud.bigquery
288+
import google.cloud.bigquery.table
289+
mock_client = mock.create_autospec(google.cloud.bigquery.Client)
290+
# Mock out SELECT 1 query results.
291+
mock_query = mock.create_autospec(google.cloud.bigquery.QueryJob)
292+
mock_query.state = 'DONE'
293+
mock_rows = mock.create_autospec(
294+
google.cloud.bigquery.table.RowIterator)
295+
mock_rows.total_rows = 1
296+
mock_rows.schema = [
297+
google.cloud.bigquery.SchemaField('_f0', 'INTEGER')]
298+
mock_rows.__iter__.return_value = [(1,)]
299+
mock_query.result.return_value = mock_rows
300+
mock_client.query.return_value = mock_query
301+
monkeypatch.setattr(
302+
gbq.GbqConnector, 'get_client', lambda _: mock_client)
303+
304+
@pytest.fixture(autouse=True)
305+
def no_auth(self, monkeypatch):
306+
import google.auth.credentials
307+
mock_credentials = mock.create_autospec(
308+
google.auth.credentials.Credentials)
309+
monkeypatch.setattr(
310+
gbq.GbqConnector,
311+
'get_application_default_credentials',
312+
lambda _: mock_credentials)
313+
monkeypatch.setattr(
314+
gbq.GbqConnector,
315+
'get_user_account_credentials',
316+
lambda _: mock_credentials)
286317

318+
def test_should_return_credentials_path_set_by_env_var(self):
287319
env = {'PANDAS_GBQ_CREDENTIALS_FILE': '/tmp/dummy.dat'}
288320
with mock.patch.dict('os.environ', env):
289321
assert gbq._get_credentials_file() == '/tmp/dummy.dat'
@@ -314,6 +346,75 @@ def test_to_gbq_with_no_project_id_given_should_fail(self):
314346
with pytest.raises(TypeError):
315347
gbq.to_gbq(DataFrame(), 'dataset.tablename')
316348

349+
def test_to_gbq_with_verbose_new_pandas_warns_deprecation(self):
350+
import pkg_resources
351+
min_bq_version = pkg_resources.parse_version('0.29.0')
352+
pandas_version = pkg_resources.parse_version('0.23.0')
353+
with pytest.warns(FutureWarning), \
354+
mock.patch(
355+
'pkg_resources.Distribution.parsed_version',
356+
new_callable=mock.PropertyMock) as mock_version:
357+
mock_version.side_effect = [min_bq_version, pandas_version]
358+
try:
359+
gbq.to_gbq(
360+
DataFrame(),
361+
'dataset.tablename',
362+
project_id='my-project',
363+
verbose=True)
364+
except gbq.TableCreationError:
365+
pass
366+
367+
def test_to_gbq_with_not_verbose_new_pandas_warns_deprecation(self):
368+
import pkg_resources
369+
min_bq_version = pkg_resources.parse_version('0.29.0')
370+
pandas_version = pkg_resources.parse_version('0.23.0')
371+
with pytest.warns(FutureWarning), \
372+
mock.patch(
373+
'pkg_resources.Distribution.parsed_version',
374+
new_callable=mock.PropertyMock) as mock_version:
375+
mock_version.side_effect = [min_bq_version, pandas_version]
376+
try:
377+
gbq.to_gbq(
378+
DataFrame(),
379+
'dataset.tablename',
380+
project_id='my-project',
381+
verbose=False)
382+
except gbq.TableCreationError:
383+
pass
384+
385+
def test_to_gbq_wo_verbose_w_new_pandas_no_warnings(self, recwarn):
386+
import pkg_resources
387+
min_bq_version = pkg_resources.parse_version('0.29.0')
388+
pandas_version = pkg_resources.parse_version('0.23.0')
389+
with mock.patch(
390+
'pkg_resources.Distribution.parsed_version',
391+
new_callable=mock.PropertyMock) as mock_version:
392+
mock_version.side_effect = [min_bq_version, pandas_version]
393+
try:
394+
gbq.to_gbq(
395+
DataFrame(), 'dataset.tablename', project_id='my-project')
396+
except gbq.TableCreationError:
397+
pass
398+
assert len(recwarn) == 0
399+
400+
def test_to_gbq_with_verbose_old_pandas_no_warnings(self, recwarn):
401+
import pkg_resources
402+
min_bq_version = pkg_resources.parse_version('0.29.0')
403+
pandas_version = pkg_resources.parse_version('0.22.0')
404+
with mock.patch(
405+
'pkg_resources.Distribution.parsed_version',
406+
new_callable=mock.PropertyMock) as mock_version:
407+
mock_version.side_effect = [min_bq_version, pandas_version]
408+
try:
409+
gbq.to_gbq(
410+
DataFrame(),
411+
'dataset.tablename',
412+
project_id='my-project',
413+
verbose=True)
414+
except gbq.TableCreationError:
415+
pass
416+
assert len(recwarn) == 0
417+
317418
def test_read_gbq_with_no_project_id_given_should_fail(self):
318419
with pytest.raises(TypeError):
319420
gbq.read_gbq('SELECT 1')
@@ -359,6 +460,50 @@ def test_read_gbq_with_corrupted_private_key_json_should_fail(self):
359460
'SELECT 1', project_id='x',
360461
private_key=re.sub('[a-z]', '9', _get_private_key_contents()))
361462

463+
def test_read_gbq_with_verbose_new_pandas_warns_deprecation(self):
464+
import pkg_resources
465+
min_bq_version = pkg_resources.parse_version('0.29.0')
466+
pandas_version = pkg_resources.parse_version('0.23.0')
467+
with pytest.warns(FutureWarning), \
468+
mock.patch(
469+
'pkg_resources.Distribution.parsed_version',
470+
new_callable=mock.PropertyMock) as mock_version:
471+
mock_version.side_effect = [min_bq_version, pandas_version]
472+
gbq.read_gbq('SELECT 1', project_id='my-project', verbose=True)
473+
474+
def test_read_gbq_with_not_verbose_new_pandas_warns_deprecation(self):
475+
import pkg_resources
476+
min_bq_version = pkg_resources.parse_version('0.29.0')
477+
pandas_version = pkg_resources.parse_version('0.23.0')
478+
with pytest.warns(FutureWarning), \
479+
mock.patch(
480+
'pkg_resources.Distribution.parsed_version',
481+
new_callable=mock.PropertyMock) as mock_version:
482+
mock_version.side_effect = [min_bq_version, pandas_version]
483+
gbq.read_gbq('SELECT 1', project_id='my-project', verbose=False)
484+
485+
def test_read_gbq_wo_verbose_w_new_pandas_no_warnings(self, recwarn):
486+
import pkg_resources
487+
min_bq_version = pkg_resources.parse_version('0.29.0')
488+
pandas_version = pkg_resources.parse_version('0.23.0')
489+
with mock.patch(
490+
'pkg_resources.Distribution.parsed_version',
491+
new_callable=mock.PropertyMock) as mock_version:
492+
mock_version.side_effect = [min_bq_version, pandas_version]
493+
gbq.read_gbq('SELECT 1', project_id='my-project')
494+
assert len(recwarn) == 0
495+
496+
def test_read_gbq_with_verbose_old_pandas_no_warnings(self, recwarn):
497+
import pkg_resources
498+
min_bq_version = pkg_resources.parse_version('0.29.0')
499+
pandas_version = pkg_resources.parse_version('0.22.0')
500+
with mock.patch(
501+
'pkg_resources.Distribution.parsed_version',
502+
new_callable=mock.PropertyMock) as mock_version:
503+
mock_version.side_effect = [min_bq_version, pandas_version]
504+
gbq.read_gbq('SELECT 1', project_id='my-project', verbose=True)
505+
assert len(recwarn) == 0
506+
362507

363508
def test_should_read(project, credentials):
364509

0 commit comments

Comments
 (0)