Skip to content

Commit 5f85e2e

Browse files
Linchinchalmerlowe
andauthored
chore: add warning if storage module not found (#1937)
* chore: add warning if storage module not found * Update tests/unit/test_table.py Co-authored-by: Chalmer Lowe <[email protected]> * Update tests/unit/test_table.py Co-authored-by: Chalmer Lowe <[email protected]> --------- Co-authored-by: Chalmer Lowe <[email protected]>
1 parent f19d398 commit 5f85e2e

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

google/cloud/bigquery/table.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,10 @@ def _should_use_bqstorage(self, bqstorage_client, create_bqstorage_client):
17241724
try:
17251725
_versions_helpers.BQ_STORAGE_VERSIONS.try_import(raise_if_error=True)
17261726
except bq_exceptions.BigQueryStorageNotFoundError:
1727+
warnings.warn(
1728+
"BigQuery Storage module not found, fetch data with the REST "
1729+
"endpoint instead."
1730+
)
17271731
return False
17281732
except bq_exceptions.LegacyBigQueryStorageError as exc:
17291733
warnings.warn(str(exc))

tests/unit/test_table.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,24 +2360,30 @@ def test__should_use_bqstorage_returns_false_if_max_results_set(self):
23602360
)
23612361
self.assertFalse(result)
23622362

2363-
def test__should_use_bqstorage_returns_false_if_missing_dependency(self):
2363+
def test__should_use_bqstorage_returns_false_w_warning_if_missing_dependency(self):
23642364
iterator = self._make_one(first_page_response=None) # not cached
2365-
2365+
23662366
def fail_bqstorage_import(name, globals, locals, fromlist, level):
2367+
"""Returns True if bigquery_storage has been imported."""
23672368
# NOTE: *very* simplified, assuming a straightforward absolute import
23682369
return "bigquery_storage" in name or (
23692370
fromlist is not None and "bigquery_storage" in fromlist
23702371
)
2371-
2372+
# maybe_fail_import() returns ImportError if the predicate is True
23722373
no_bqstorage = maybe_fail_import(predicate=fail_bqstorage_import)
23732374

2374-
with no_bqstorage:
2375+
with no_bqstorage, warnings.catch_warnings(record=True) as warned:
23752376
result = iterator._should_use_bqstorage(
23762377
bqstorage_client=None, create_bqstorage_client=True
23772378
)
23782379

23792380
self.assertFalse(result)
23802381

2382+
matching_warnings = [
2383+
warning for warning in warned if "Storage module not found" in str(warning)
2384+
]
2385+
assert matching_warnings, "Dependency not found warning not raised."
2386+
23812387
def test__should_use_bqstorage_returns_false_w_warning_if_obsolete_version(self):
23822388
pytest.importorskip("google.cloud.bigquery_storage")
23832389
iterator = self._make_one(first_page_response=None) # not cached

0 commit comments

Comments
 (0)