ci: Import numpy before pyarrow in tests to resolve import warning (#… · googleapis/python-bigquery@a3d6bf3 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit a3d6bf3

Browse files
ci: Import numpy before pyarrow in tests to resolve import warning (#2187)
* Fix: Import numpy before pyarrow in tests to resolve import warning A `PytestDeprecationWarning` was occurring in several test files because `pyarrow`, when imported by `pytest.importorskip`, would fail to import `numpy.core.multiarray`. This change addresses the warning by explicitly importing `numpy` before `pytest.importorskip("pyarrow", ...)` in the affected test files. This ensures that numpy is fully initialized before pyarrow attempts to use it, resolving the underlying import error. I also updated the test execution to use `nox -s unit`, which correctly sets up the test environment and dependencies, allowing the tests to pass and confirm the warning is resolved. Pre-existing failures in `tests/unit/test_magics.py` are unrelated to this change. * Update tests/unit/test__pyarrow_helpers.py * revisions to numpy handling * adds import or skip commands to accompany pyarrow import or skips * Update tests/unit/test__pandas_helpers.py * updates an import step and restores gc import * Updates magics.context and removes unneeded? reference to numpy/pyarrow --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent b140fca commit a3d6bf3

File tree

8 files changed

+38
-6
lines changed

8 files changed

+38
-6
lines changed

noxfile.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ def unit_noextras(session):
163163
# so that it continues to be an optional dependency.
164164
# https://github.com/googleapis/python-bigquery/issues/1877
165165
if session.python == UNIT_TEST_PYTHON_VERSIONS[0]:
166-
session.install("pyarrow==4.0.0")
167-
166+
session.install("pyarrow==4.0.0", "numpy==1.20.2")
168167
default(session, install_extras=False)
169168

170169

testing/constraints-3.9.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ipykernel==6.2.0
2020
opentelemetry-api==1.1.0
2121
opentelemetry-instrumentation==0.20b0
2222
opentelemetry-sdk==1.1.0
23+
numpy==1.20.2
2324
packaging==24.2.0
2425
pandas==1.3.0
2526
pandas-gbq==0.26.1

tests/unit/test__pandas_helpers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,7 @@ def test__download_table_bqstorage_shuts_down_workers(
18561856
Make sure that when the top-level iterator goes out of scope (is deleted),
18571857
the child threads are also stopped.
18581858
"""
1859+
pytest.importorskip("google.cloud.bigquery_storage_v1")
18591860
from google.cloud.bigquery import dataset
18601861
from google.cloud.bigquery import table
18611862
import google.cloud.bigquery_storage_v1.reader

tests/unit/test__pyarrow_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import pytest
1616

17-
17+
numpy = pytest.importorskip("numpy")
1818
pyarrow = pytest.importorskip("pyarrow", minversion="3.0.0")
1919

2020

tests/unit/test_dbapi__helpers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def test_empty_iterable(self):
210210
self.assertEqual(list(result), [])
211211

212212
def test_non_empty_iterable(self):
213+
pytest.importorskip("numpy")
213214
pytest.importorskip("pyarrow")
214215
from tests.unit.helpers import _to_pyarrow
215216

tests/unit/test_magics.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,11 @@ def test_bigquery_magic_with_no_query_cache(monkeypatch):
12761276
bigquery.load_ipython_extension(ip)
12771277
conn = make_connection()
12781278
monkeypatch.setattr(magics.context, "_connection", conn)
1279+
monkeypatch.setattr(
1280+
magics.context,
1281+
"credentials",
1282+
mock.create_autospec(google.auth.credentials.Credentials, instance=True),
1283+
)
12791284
monkeypatch.setattr(magics.context, "project", "project-from-context")
12801285

12811286
# --no_query_cache option should override context.

tests/unit/test_table.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,13 +2416,15 @@ def test_to_arrow_error_if_pyarrow_is_none(self):
24162416
row_iterator.to_arrow()
24172417

24182418
def test_to_arrow(self):
2419+
pytest.importorskip("numpy")
24192420
pyarrow = pytest.importorskip("pyarrow")
24202421
row_iterator = self._make_one()
24212422
tbl = row_iterator.to_arrow()
24222423
self.assertIsInstance(tbl, pyarrow.Table)
24232424
self.assertEqual(tbl.num_rows, 0)
24242425

24252426
def test_to_arrow_iterable(self):
2427+
pytest.importorskip("numpy")
24262428
pyarrow = pytest.importorskip(
24272429
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
24282430
)
@@ -3089,6 +3091,7 @@ def test_to_arrow_iterable_w_bqstorage(self):
30893091
bqstorage_client._transport.grpc_channel.close.assert_not_called()
30903092

30913093
def test_to_arrow(self):
3094+
pytest.importorskip("numpy")
30923095
pyarrow = pytest.importorskip(
30933096
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
30943097
)
@@ -3173,6 +3176,7 @@ def test_to_arrow(self):
31733176
)
31743177

31753178
def test_to_arrow_w_nulls(self):
3179+
pytest.importorskip("numpy")
31763180
pyarrow = pytest.importorskip(
31773181
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
31783182
)
@@ -3209,6 +3213,7 @@ def test_to_arrow_w_nulls(self):
32093213
self.assertEqual(ages, [32, 29, None, 111])
32103214

32113215
def test_to_arrow_w_unknown_type(self):
3216+
pytest.importorskip("numpy")
32123217
pyarrow = pytest.importorskip(
32133218
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
32143219
)
@@ -3254,6 +3259,7 @@ def test_to_arrow_w_unknown_type(self):
32543259
self.assertTrue(all("sport" in str(warning) for warning in warned))
32553260

32563261
def test_to_arrow_w_empty_table(self):
3262+
pytest.importorskip("numpy")
32573263
pyarrow = pytest.importorskip(
32583264
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
32593265
)
@@ -3295,6 +3301,7 @@ def test_to_arrow_w_empty_table(self):
32953301
self.assertEqual(child_field.type.value_type[1].name, "age")
32963302

32973303
def test_to_arrow_max_results_w_explicit_bqstorage_client_warning(self):
3304+
pytest.importorskip("numpy")
32983305
pytest.importorskip("pyarrow")
32993306
pytest.importorskip("google.cloud.bigquery_storage")
33003307
from google.cloud.bigquery.schema import SchemaField
@@ -3337,6 +3344,7 @@ def test_to_arrow_max_results_w_explicit_bqstorage_client_warning(self):
33373344
mock_client._ensure_bqstorage_client.assert_not_called()
33383345

33393346
def test_to_arrow_max_results_w_create_bqstorage_client_no_warning(self):
3347+
pytest.importorskip("numpy")
33403348
pytest.importorskip("pyarrow")
33413349
pytest.importorskip("google.cloud.bigquery_storage")
33423350
from google.cloud.bigquery.schema import SchemaField
@@ -3375,6 +3383,7 @@ def test_to_arrow_max_results_w_create_bqstorage_client_no_warning(self):
33753383
mock_client._ensure_bqstorage_client.assert_not_called()
33763384

33773385
def test_to_arrow_w_bqstorage(self):
3386+
pytest.importorskip("numpy")
33783387
pyarrow = pytest.importorskip("pyarrow")
33793388
pytest.importorskip("google.cloud.bigquery_storage")
33803389
from google.cloud.bigquery import schema
@@ -3458,6 +3467,7 @@ def test_to_arrow_w_bqstorage(self):
34583467
bqstorage_client._transport.grpc_channel.close.assert_not_called()
34593468

34603469
def test_to_arrow_w_bqstorage_creates_client(self):
3470+
pytest.importorskip("numpy")
34613471
pytest.importorskip("pyarrow")
34623472
pytest.importorskip("google.cloud.bigquery_storage")
34633473
from google.cloud.bigquery import schema
@@ -3491,6 +3501,7 @@ def test_to_arrow_w_bqstorage_creates_client(self):
34913501
bqstorage_client._transport.grpc_channel.close.assert_called_once()
34923502

34933503
def test_to_arrow_ensure_bqstorage_client_wo_bqstorage(self):
3504+
pytest.importorskip("numpy")
34943505
pyarrow = pytest.importorskip(
34953506
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
34963507
)
@@ -3524,6 +3535,7 @@ def mock_verify_version(raise_if_error: bool = False):
35243535
self.assertEqual(tbl.num_rows, 2)
35253536

35263537
def test_to_arrow_w_bqstorage_no_streams(self):
3538+
pytest.importorskip("numpy")
35273539
pyarrow = pytest.importorskip("pyarrow")
35283540
pytest.importorskip("google.cloud.bigquery_storage")
35293541
from google.cloud.bigquery import schema
@@ -3563,6 +3575,7 @@ def test_to_arrow_w_bqstorage_no_streams(self):
35633575
self.assertEqual(actual_table.schema[2].name, "colB")
35643576

35653577
def test_to_arrow_progress_bar(self):
3578+
pytest.importorskip("numpy")
35663579
pytest.importorskip("pyarrow")
35673580
pytest.importorskip("tqdm")
35683581
pytest.importorskip("tqdm.notebook")
@@ -3696,6 +3709,7 @@ def test_to_dataframe_iterable_with_dtypes(self):
36963709
self.assertEqual(df_2["age"][0], 33)
36973710

36983711
def test_to_dataframe_iterable_w_bqstorage(self):
3712+
pytest.importorskip("numpy")
36993713
pandas = pytest.importorskip("pandas")
37003714
pyarrow = pytest.importorskip("pyarrow")
37013715
pytest.importorskip("google.cloud.bigquery_storage")
@@ -3770,6 +3784,7 @@ def test_to_dataframe_iterable_w_bqstorage(self):
37703784
bqstorage_client._transport.grpc_channel.close.assert_not_called()
37713785

37723786
def test_to_dataframe_iterable_w_bqstorage_max_results_warning(self):
3787+
pytest.importorskip("numpy")
37733788
pandas = pytest.importorskip("pandas")
37743789
pytest.importorskip("google.cloud.bigquery_storage")
37753790
from google.cloud.bigquery import schema
@@ -4513,7 +4528,7 @@ def test_to_dataframe_w_none_dtypes_mapper(self):
45134528

45144529
def test_to_dataframe_w_unsupported_dtypes_mapper(self):
45154530
pytest.importorskip("pandas")
4516-
import numpy
4531+
numpy = pytest.importorskip("numpy")
45174532
from google.cloud.bigquery.schema import SchemaField
45184533

45194534
schema = [
@@ -4797,6 +4812,7 @@ def test_to_dataframe_max_results_w_create_bqstorage_client_no_warning(self):
47974812
mock_client._ensure_bqstorage_client.assert_not_called()
47984813

47994814
def test_to_dataframe_w_bqstorage_creates_client(self):
4815+
pytest.importorskip("numpy")
48004816
pytest.importorskip("pandas")
48014817
pytest.importorskip("google.cloud.bigquery_storage")
48024818
from google.cloud.bigquery import schema
@@ -4830,6 +4846,7 @@ def test_to_dataframe_w_bqstorage_creates_client(self):
48304846
bqstorage_client._transport.grpc_channel.close.assert_called_once()
48314847

48324848
def test_to_dataframe_w_bqstorage_no_streams(self):
4849+
pytest.importorskip("numpy")
48334850
pytest.importorskip("pandas")
48344851
pytest.importorskip("google.cloud.bigquery_storage")
48354852
from google.cloud.bigquery import schema
@@ -4858,6 +4875,7 @@ def test_to_dataframe_w_bqstorage_no_streams(self):
48584875
self.assertTrue(got.empty)
48594876

48604877
def test_to_dataframe_w_bqstorage_logs_session(self):
4878+
pytest.importorskip("numpy")
48614879
pytest.importorskip("google.cloud.bigquery_storage")
48624880
pytest.importorskip("pandas")
48634881
pytest.importorskip("pyarrow")
@@ -4882,6 +4900,7 @@ def test_to_dataframe_w_bqstorage_logs_session(self):
48824900
)
48834901

48844902
def test_to_dataframe_w_bqstorage_empty_streams(self):
4903+
pytest.importorskip("numpy")
48854904
pytest.importorskip("google.cloud.bigquery_storage")
48864905
pytest.importorskip("pandas")
48874906
pyarrow = pytest.importorskip("pyarrow")
@@ -4936,6 +4955,7 @@ def test_to_dataframe_w_bqstorage_empty_streams(self):
49364955
self.assertTrue(got.empty)
49374956

49384957
def test_to_dataframe_w_bqstorage_nonempty(self):
4958+
pytest.importorskip("numpy")
49394959
pytest.importorskip("google.cloud.bigquery_storage")
49404960
pytest.importorskip("pandas")
49414961
pyarrow = pytest.importorskip("pyarrow")
@@ -5018,6 +5038,7 @@ def test_to_dataframe_w_bqstorage_nonempty(self):
50185038
bqstorage_client._transport.grpc_channel.close.assert_not_called()
50195039

50205040
def test_to_dataframe_w_bqstorage_multiple_streams_return_unique_index(self):
5041+
pytest.importorskip("numpy")
50215042
bigquery_storage = pytest.importorskip("google.cloud.bigquery_storage")
50225043
pytest.importorskip("pandas")
50235044
pyarrow = pytest.importorskip("pyarrow")
@@ -5070,6 +5091,7 @@ def test_to_dataframe_w_bqstorage_multiple_streams_return_unique_index(self):
50705091
self.assertTrue(got.index.is_unique)
50715092

50725093
def test_to_dataframe_w_bqstorage_updates_progress_bar(self):
5094+
pytest.importorskip("numpy")
50735095
bigquery_storage = pytest.importorskip("google.cloud.bigquery_storage")
50745096
pytest.importorskip("pandas")
50755097
pyarrow = pytest.importorskip("pyarrow")
@@ -5147,6 +5169,7 @@ def blocking_to_arrow(*args, **kwargs):
51475169
tqdm_mock().close.assert_called_once()
51485170

51495171
def test_to_dataframe_w_bqstorage_exits_on_keyboardinterrupt(self):
5172+
pytest.importorskip("numpy")
51505173
bigquery_storage = pytest.importorskip("google.cloud.bigquery_storage")
51515174
pytest.importorskip("pandas")
51525175
pyarrow = pytest.importorskip("pyarrow")
@@ -5322,6 +5345,7 @@ def test_to_dataframe_w_bqstorage_snapshot(self):
53225345
row_iterator.to_dataframe(bqstorage_client)
53235346

53245347
def test_to_dataframe_concat_categorical_dtype_w_pyarrow(self):
5348+
pytest.importorskip("numpy")
53255349
pytest.importorskip("google.cloud.bigquery_storage")
53265350
pandas = pytest.importorskip("pandas")
53275351
pyarrow = pytest.importorskip("pyarrow")
@@ -5604,7 +5628,7 @@ def test_rowiterator_to_geodataframe_delegation(self, to_dataframe):
56045628
"""
56055629
pandas = pytest.importorskip("pandas")
56065630
geopandas = pytest.importorskip("geopandas")
5607-
import numpy
5631+
numpy = pytest.importorskip("numpy")
56085632
from shapely import wkt
56095633

56105634
row_iterator = self._make_one_from_data(

tests/unit/test_table_arrow.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import google.cloud.bigquery.table
1919

2020

21-
pyarrow = pytest.importorskip("pyarrow", minversion="3.0.0")
21+
pytest.importorskip("numpy")
22+
pytest.importorskip("pyarrow", minversion="3.0.0")
2223

2324

2425
def test_to_arrow_with_jobs_query_response():

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.