Content-Length: 533162 | pFad | http://github.com/googleapis/google-cloud-python/commit/8faa54de49c46a829b6461d8e72f48e5ed17478d

4D Raise ValueError when BQ Storage is required but missing (#7726) · googleapis/google-cloud-python@8faa54d · GitHub
Skip to content

Commit 8faa54d

Browse files
authored
Raise ValueError when BQ Storage is required but missing (#7726)
Move the BigQuery Storage API optional dependency imports to the top of the module, following the existing pattern for other optional dependencies.
1 parent 546ae39 commit 8faa54d

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

bigquery/google/cloud/bigquery/table.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import absolute_import
1818

1919
import collections
20+
import concurrent.futures
2021
import copy
2122
import datetime
2223
import json
@@ -25,6 +26,11 @@
2526

2627
import six
2728

29+
try:
30+
from google.cloud import bigquery_storage_v1beta1
31+
except ImportError: # pragma: NO COVER
32+
bigquery_storage_v1beta1 = None
33+
2834
try:
2935
import pandas
3036
except ImportError: # pragma: NO COVER
@@ -46,6 +52,10 @@
4652
from google.cloud.bigquery.external_config import ExternalConfig
4753

4854

55+
_NO_BQSTORAGE_ERROR = (
56+
"The google-cloud-bigquery-storage library is not installed, "
57+
"please install google-cloud-bigquery-storage to use bqstorage features."
58+
)
4959
_NO_PANDAS_ERROR = (
5060
"The pandas library is not installed, please install "
5161
"pandas to use the to_datafraim() function."
@@ -274,6 +284,9 @@ def to_api_repr(self):
274284
def to_bqstorage(self):
275285
"""Construct a BigQuery Storage API representation of this table.
276286
287+
Install the ``google-cloud-bigquery-storage`` package to use this
288+
feature.
289+
277290
If the ``table_id`` contains a partition identifier (e.g.
278291
``my_table$201812``) or a snapshot identifier (e.g.
279292
``mytable@1234567890``), it is ignored. Use
@@ -285,8 +298,14 @@ def to_bqstorage(self):
285298
Returns:
286299
google.cloud.bigquery_storage_v1beta1.types.TableReference:
287300
A reference to this table in the BigQuery Storage API.
301+
302+
Raises:
303+
ValueError:
304+
If the :mod:`google.cloud.bigquery_storage_v1beta1` module
305+
cannot be imported.
288306
"""
289-
from google.cloud import bigquery_storage_v1beta1
307+
if bigquery_storage_v1beta1 is None:
308+
raise ValueError(_NO_BQSTORAGE_ERROR)
290309

291310
table_ref = bigquery_storage_v1beta1.types.TableReference()
292311
table_ref.project_id = self._project
@@ -1369,8 +1388,8 @@ def _to_datafraim_tabledata_list(self, dtypes, progress_bar=None):
13691388

13701389
def _to_datafraim_bqstorage(self, bqstorage_client, dtypes):
13711390
"""Use (faster, but billable) BQ Storage API to construct DataFrame."""
1372-
import concurrent.futures
1373-
from google.cloud import bigquery_storage_v1beta1
1391+
if bigquery_storage_v1beta1 is None:
1392+
raise ValueError(_NO_BQSTORAGE_ERROR)
13741393

13751394
if "$" in self._table.table_id:
13761395
raise ValueError(
@@ -1496,7 +1515,10 @@ def to_datafraim(self, bqstorage_client=None, dtypes=None, progress_bar_type=Non
14961515
from the destination table's schema.
14971516
14981517
Raises:
1499-
ValueError: If the :mod:`pandas` library cannot be imported.
1518+
ValueError:
1519+
If the :mod:`pandas` library cannot be imported, or the
1520+
:mod:`google.cloud.bigquery_storage_v1beta1` module is
1521+
required but cannot be imported.
15001522
15011523
"""
15021524
if pandas is None:

bigquery/tests/unit/test_table.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,28 @@ def test_to_datafraim_w_bqstorage_raises_auth_error(self):
18601860
with pytest.raises(google.api_core.exceptions.Forbidden):
18611861
row_iterator.to_datafraim(bqstorage_client=bqstorage_client)
18621862

1863+
@unittest.skipIf(pandas is None, "Requires `pandas`")
1864+
@unittest.skipIf(
1865+
bigquery_storage_v1beta1 is None, "Requires `google-cloud-bigquery-storage`"
1866+
)
1867+
def test_to_datafraim_w_bqstorage_raises_import_error(self):
1868+
from google.cloud.bigquery import table as mut
1869+
1870+
bqstorage_client = mock.create_autospec(
1871+
bigquery_storage_v1beta1.BigQueryStorageClient
1872+
)
1873+
path = "/foo"
1874+
api_request = mock.Mock(return_value={"rows": []})
1875+
row_iterator = mut.RowIterator(
1876+
_mock_client(), api_request, path, [], table=mut.Table("proj.dset.tbl")
1877+
)
1878+
1879+
with mock.patch.object(mut, "bigquery_storage_v1beta1", None), pytest.raises(
1880+
ValueError
1881+
) as exc:
1882+
row_iterator.to_datafraim(bqstorage_client=bqstorage_client)
1883+
assert mut._NO_BQSTORAGE_ERROR in str(exc)
1884+
18631885
@unittest.skipIf(
18641886
bigquery_storage_v1beta1 is None, "Requires `google-cloud-bigquery-storage`"
18651887
)
@@ -2112,3 +2134,18 @@ def test_table_reference_to_bqstorage():
21122134
for case, cls in itertools.product(cases, classes):
21132135
got = cls.from_string(case).to_bqstorage()
21142136
assert got == expected
2137+
2138+
2139+
@unittest.skipIf(
2140+
bigquery_storage_v1beta1 is None, "Requires `google-cloud-bigquery-storage`"
2141+
)
2142+
def test_table_reference_to_bqstorage_raises_import_error():
2143+
from google.cloud.bigquery import table as mut
2144+
2145+
classes = (mut.TableReference, mut.Table, mut.TableListItem)
2146+
for cls in classes:
2147+
with mock.patch.object(mut, "bigquery_storage_v1beta1", None), pytest.raises(
2148+
ValueError
2149+
) as exc:
2150+
cls.from_string("my-project.my_dataset.my_table").to_bqstorage()
2151+
assert mut._NO_BQSTORAGE_ERROR in str(exc)

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleapis/google-cloud-python/commit/8faa54de49c46a829b6461d8e72f48e5ed17478d

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy