Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 9b8e13e

Browse files
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.
1 parent 5805066 commit 9b8e13e

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

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+
import 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+
import numpy
213214
pytest.importorskip("pyarrow")
214215
from tests.unit.helpers import _to_pyarrow
215216

tests/unit/test_table.py

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

24182418
def test_to_arrow(self):
2419+
import 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+
import numpy
2428+
import numpy
24262429
pyarrow = pytest.importorskip(
24272430
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
24282431
)
@@ -3096,6 +3099,7 @@ def test_to_arrow_iterable_w_bqstorage(self):
30963099
bqstorage_client._transport.grpc_channel.close.assert_not_called()
30973100

30983101
def test_to_arrow(self):
3102+
import numpy
30993103
pyarrow = pytest.importorskip(
31003104
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
31013105
)
@@ -3180,6 +3184,7 @@ def test_to_arrow(self):
31803184
)
31813185

31823186
def test_to_arrow_w_nulls(self):
3187+
import numpy
31833188
pyarrow = pytest.importorskip(
31843189
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
31853190
)
@@ -3216,6 +3221,7 @@ def test_to_arrow_w_nulls(self):
32163221
self.assertEqual(ages, [32, 29, None, 111])
32173222

32183223
def test_to_arrow_w_unknown_type(self):
3224+
import numpy
32193225
pyarrow = pytest.importorskip(
32203226
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
32213227
)
@@ -3261,6 +3267,7 @@ def test_to_arrow_w_unknown_type(self):
32613267
self.assertTrue(all("sport" in str(warning) for warning in warned))
32623268

32633269
def test_to_arrow_w_empty_table(self):
3270+
import numpy
32643271
pyarrow = pytest.importorskip(
32653272
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
32663273
)
@@ -3302,6 +3309,7 @@ def test_to_arrow_w_empty_table(self):
33023309
self.assertEqual(child_field.type.value_type[1].name, "age")
33033310

33043311
def test_to_arrow_max_results_w_explicit_bqstorage_client_warning(self):
3312+
import numpy
33053313
pytest.importorskip("pyarrow")
33063314
pytest.importorskip("google.cloud.bigquery_storage")
33073315
from google.cloud.bigquery.schema import SchemaField
@@ -3344,6 +3352,7 @@ def test_to_arrow_max_results_w_explicit_bqstorage_client_warning(self):
33443352
mock_client._ensure_bqstorage_client.assert_not_called()
33453353

33463354
def test_to_arrow_max_results_w_create_bqstorage_client_no_warning(self):
3355+
import numpy
33473356
pytest.importorskip("pyarrow")
33483357
pytest.importorskip("google.cloud.bigquery_storage")
33493358
from google.cloud.bigquery.schema import SchemaField
@@ -3382,6 +3391,7 @@ def test_to_arrow_max_results_w_create_bqstorage_client_no_warning(self):
33823391
mock_client._ensure_bqstorage_client.assert_not_called()
33833392

33843393
def test_to_arrow_w_bqstorage(self):
3394+
import numpy
33853395
pyarrow = pytest.importorskip("pyarrow")
33863396
pytest.importorskip("google.cloud.bigquery_storage")
33873397
from google.cloud.bigquery import schema
@@ -3465,6 +3475,7 @@ def test_to_arrow_w_bqstorage(self):
34653475
bqstorage_client._transport.grpc_channel.close.assert_not_called()
34663476

34673477
def test_to_arrow_w_bqstorage_creates_client(self):
3478+
import numpy
34683479
pytest.importorskip("pyarrow")
34693480
pytest.importorskip("google.cloud.bigquery_storage")
34703481
from google.cloud.bigquery import schema
@@ -3498,6 +3509,7 @@ def test_to_arrow_w_bqstorage_creates_client(self):
34983509
bqstorage_client._transport.grpc_channel.close.assert_called_once()
34993510

35003511
def test_to_arrow_ensure_bqstorage_client_wo_bqstorage(self):
3512+
import numpy
35013513
pyarrow = pytest.importorskip(
35023514
"pyarrow", minversion=self.PYARROW_MINIMUM_VERSION
35033515
)
@@ -3531,6 +3543,7 @@ def mock_verify_version(raise_if_error: bool = False):
35313543
self.assertEqual(tbl.num_rows, 2)
35323544

35333545
def test_to_arrow_w_bqstorage_no_streams(self):
3546+
import numpy
35343547
pyarrow = pytest.importorskip("pyarrow")
35353548
pytest.importorskip("google.cloud.bigquery_storage")
35363549
from google.cloud.bigquery import schema
@@ -3570,6 +3583,7 @@ def test_to_arrow_w_bqstorage_no_streams(self):
35703583
self.assertEqual(actual_table.schema[2].name, "colB")
35713584

35723585
def test_to_arrow_progress_bar(self):
3586+
import numpy
35733587
pytest.importorskip("pyarrow")
35743588
pytest.importorskip("tqdm")
35753589
pytest.importorskip("tqdm.notebook")
@@ -3703,6 +3717,7 @@ def test_to_dataframe_iterable_with_dtypes(self):
37033717
self.assertEqual(df_2["age"][0], 33)
37043718

37053719
def test_to_dataframe_iterable_w_bqstorage(self):
3720+
import numpy
37063721
pandas = pytest.importorskip("pandas")
37073722
pyarrow = pytest.importorskip("pyarrow")
37083723
pytest.importorskip("google.cloud.bigquery_storage")
@@ -3777,6 +3792,7 @@ def test_to_dataframe_iterable_w_bqstorage(self):
37773792
bqstorage_client._transport.grpc_channel.close.assert_not_called()
37783793

37793794
def test_to_dataframe_iterable_w_bqstorage_max_results_warning(self):
3795+
import numpy
37803796
pandas = pytest.importorskip("pandas")
37813797
pytest.importorskip("google.cloud.bigquery_storage")
37823798
from google.cloud.bigquery import schema
@@ -4804,6 +4820,7 @@ def test_to_dataframe_max_results_w_create_bqstorage_client_no_warning(self):
48044820
mock_client._ensure_bqstorage_client.assert_not_called()
48054821

48064822
def test_to_dataframe_w_bqstorage_creates_client(self):
4823+
import numpy
48074824
pytest.importorskip("pandas")
48084825
pytest.importorskip("google.cloud.bigquery_storage")
48094826
from google.cloud.bigquery import schema
@@ -4837,6 +4854,7 @@ def test_to_dataframe_w_bqstorage_creates_client(self):
48374854
bqstorage_client._transport.grpc_channel.close.assert_called_once()
48384855

48394856
def test_to_dataframe_w_bqstorage_no_streams(self):
4857+
import numpy
48404858
pytest.importorskip("pandas")
48414859
pytest.importorskip("google.cloud.bigquery_storage")
48424860
from google.cloud.bigquery import schema
@@ -4865,6 +4883,7 @@ def test_to_dataframe_w_bqstorage_no_streams(self):
48654883
self.assertTrue(got.empty)
48664884

48674885
def test_to_dataframe_w_bqstorage_logs_session(self):
4886+
import numpy
48684887
pytest.importorskip("google.cloud.bigquery_storage")
48694888
pytest.importorskip("pandas")
48704889
pytest.importorskip("pyarrow")
@@ -4889,6 +4908,7 @@ def test_to_dataframe_w_bqstorage_logs_session(self):
48894908
)
48904909

48914910
def test_to_dataframe_w_bqstorage_empty_streams(self):
4911+
import numpy
48924912
pytest.importorskip("google.cloud.bigquery_storage")
48934913
pytest.importorskip("pandas")
48944914
pyarrow = pytest.importorskip("pyarrow")
@@ -4943,6 +4963,7 @@ def test_to_dataframe_w_bqstorage_empty_streams(self):
49434963
self.assertTrue(got.empty)
49444964

49454965
def test_to_dataframe_w_bqstorage_nonempty(self):
4966+
import numpy
49464967
pytest.importorskip("google.cloud.bigquery_storage")
49474968
pytest.importorskip("pandas")
49484969
pyarrow = pytest.importorskip("pyarrow")
@@ -5025,6 +5046,7 @@ def test_to_dataframe_w_bqstorage_nonempty(self):
50255046
bqstorage_client._transport.grpc_channel.close.assert_not_called()
50265047

50275048
def test_to_dataframe_w_bqstorage_multiple_streams_return_unique_index(self):
5049+
import numpy
50285050
bigquery_storage = pytest.importorskip("google.cloud.bigquery_storage")
50295051
pytest.importorskip("pandas")
50305052
pyarrow = pytest.importorskip("pyarrow")
@@ -5077,6 +5099,7 @@ def test_to_dataframe_w_bqstorage_multiple_streams_return_unique_index(self):
50775099
self.assertTrue(got.index.is_unique)
50785100

50795101
def test_to_dataframe_w_bqstorage_updates_progress_bar(self):
5102+
import numpy
50805103
bigquery_storage = pytest.importorskip("google.cloud.bigquery_storage")
50815104
pytest.importorskip("pandas")
50825105
pyarrow = pytest.importorskip("pyarrow")
@@ -5154,6 +5177,7 @@ def blocking_to_arrow(*args, **kwargs):
51545177
tqdm_mock().close.assert_called_once()
51555178

51565179
def test_to_dataframe_w_bqstorage_exits_on_keyboardinterrupt(self):
5180+
import numpy
51575181
bigquery_storage = pytest.importorskip("google.cloud.bigquery_storage")
51585182
pytest.importorskip("pandas")
51595183
pyarrow = pytest.importorskip("pyarrow")
@@ -5329,6 +5353,7 @@ def test_to_dataframe_w_bqstorage_snapshot(self):
53295353
row_iterator.to_dataframe(bqstorage_client)
53305354

53315355
def test_to_dataframe_concat_categorical_dtype_w_pyarrow(self):
5356+
import numpy
53325357
pytest.importorskip("google.cloud.bigquery_storage")
53335358
pandas = pytest.importorskip("pandas")
53345359
pyarrow = pytest.importorskip("pyarrow")

tests/unit/test_table_arrow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import google.cloud.bigquery.table
1919

2020

21+
import numpy
2122
pyarrow = pytest.importorskip("pyarrow", minversion="3.0.0")
2223

2324

0 commit comments

Comments
 (0)