Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def close(self):

if self._owns_bqstorage_client:
# There is no close() on the BQ Storage client itself.
self._bqstorage_client._transport.grpc_channel.close()
self._bqstorage_client._transport.close()

for cursor_ in self._cursors_created:
if not cursor_._closed:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,4 +773,4 @@ def _close_transports(client, bqstorage_client):
"""
client.close()
if bqstorage_client is not None:
bqstorage_client._transport.grpc_channel.close()
bqstorage_client._transport.close()
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,7 @@ def to_arrow(
progress_bar.close()
finally:
if owns_bqstorage_client:
bqstorage_client._transport.grpc_channel.close() # type: ignore
bqstorage_client._transport.close()

if record_batches and bqstorage_client is not None:
return pyarrow.Table.from_batches(record_batches)
Expand Down
24 changes: 23 additions & 1 deletion packages/google-cloud-bigquery/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

from __future__ import absolute_import

import contextlib
from functools import wraps
import os
import pathlib
from typing import Generator
import re
import shutil
import time
Expand Down Expand Up @@ -80,6 +82,25 @@ def wrapper(*args, **kwargs):
]


@contextlib.contextmanager
def log_package_context(session: nox.Session) -> Generator[None, None, None]:
"""Logs a highly visible package context banner right before a session exits.

Ensures metadata is printed adjacent to Nox's final status log,
even if the session fails or raises an exception.
"""
# Dynamically extract current folder name (e.g., 'google-cloud-bigquery')
package_name = CURRENT_DIRECTORY.name

try:
# Hands control back to the session code block
yield
finally:
# This executes AFTER test output finishes, immediately above Nox's summary line
banner_text = f"Finished session for {package_name.lower()}"
session.log(banner_text)


def default(session, install_extras=True):
"""Default unit test session.

Expand Down Expand Up @@ -193,7 +214,8 @@ def mypy(session):
"types-setuptools",
)
session.run("python", "-m", "pip", "freeze")
session.run("mypy", "-p", "google", "--show-traceback")
with log_package_context(session):
session.run("mypy", "-p", "google", "--show-traceback")


@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _mock_bqstorage_client(self):
from google.cloud import bigquery_storage

mock_client = mock.create_autospec(bigquery_storage.BigQueryReadClient)
mock_client._transport = mock.Mock(spec=["channel"])
mock_client._transport = mock.Mock(spec=["channel", "close"])
mock_client._transport.grpc_channel = mock.Mock(spec=["close"])
return mock_client

Expand Down Expand Up @@ -176,7 +176,7 @@ def test_close_closes_all_created_bigquery_clients(self):
connection.close()

self.assertTrue(client.close.called)
self.assertTrue(bqstorage_client._transport.grpc_channel.close.called)
self.assertTrue(bqstorage_client._transport.close.called)

def test_close_does_not_close_bigquery_clients_passed_to_it(self):
pytest.importorskip("google.cloud.bigquery_storage")
Expand All @@ -187,7 +187,7 @@ def test_close_does_not_close_bigquery_clients_passed_to_it(self):
connection.close()

self.assertFalse(client.close.called)
self.assertFalse(bqstorage_client._transport.grpc_channel.close.called)
self.assertFalse(bqstorage_client._transport.close.called)

def test_close_closes_all_created_cursors(self):
connection = self._make_one(client=self._mock_client())
Expand Down
14 changes: 7 additions & 7 deletions packages/google-cloud-bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3048,7 +3048,7 @@ def test_to_arrow_iterable_w_bqstorage(self):
self.assertEqual(record_batch, expected_record_batch)

# Don't close the client if it was passed in.
bqstorage_client._transport.grpc_channel.close.assert_not_called()
bqstorage_client._transport.close.assert_not_called()

def test_to_arrow(self):
pytest.importorskip("numpy")
Expand Down Expand Up @@ -3424,7 +3424,7 @@ def test_to_arrow_w_bqstorage(self):
self.assertEqual(actual_tbl.num_rows, total_rows)

# Don't close the client if it was passed in.
bqstorage_client._transport.grpc_channel.close.assert_not_called()
bqstorage_client._transport.close.assert_not_called()

def test_to_arrow_w_bqstorage_creates_client(self):
pytest.importorskip("numpy")
Expand Down Expand Up @@ -3458,7 +3458,7 @@ def test_to_arrow_w_bqstorage_creates_client(self):
)
row_iterator.to_arrow(create_bqstorage_client=True)
mock_client._ensure_bqstorage_client.assert_called_once()
bqstorage_client._transport.grpc_channel.close.assert_called_once()
bqstorage_client._transport.close.assert_called_once()

def test_to_arrow_ensure_bqstorage_client_wo_bqstorage(self):
pytest.importorskip("numpy")
Expand Down Expand Up @@ -3741,7 +3741,7 @@ def test_to_dataframe_iterable_w_bqstorage(self):
self.assertEqual(len(got), total_pages)

# Don't close the client if it was passed in.
bqstorage_client._transport.grpc_channel.close.assert_not_called()
bqstorage_client._transport.close.assert_not_called()

def test_to_dataframe_iterable_w_bqstorage_max_results_warning(self):
pytest.importorskip("numpy")
Expand Down Expand Up @@ -4807,7 +4807,7 @@ def test_to_dataframe_w_bqstorage_creates_client(self):
)
row_iterator.to_dataframe(create_bqstorage_client=True)
mock_client._ensure_bqstorage_client.assert_called_once()
bqstorage_client._transport.grpc_channel.close.assert_called_once()
bqstorage_client._transport.close.assert_called_once()

def test_to_dataframe_w_bqstorage_no_streams(self):
pytest.importorskip("numpy")
Expand Down Expand Up @@ -4999,7 +4999,7 @@ def test_to_dataframe_w_bqstorage_nonempty(self):
self.assertEqual(len(got.index), total_rows)

# Don't close the client if it was passed in.
bqstorage_client._transport.grpc_channel.close.assert_not_called()
bqstorage_client._transport.close.assert_not_called()

def test_to_dataframe_w_bqstorage_multiple_streams_return_unique_index(self):
pytest.importorskip("numpy")
Expand Down Expand Up @@ -5421,7 +5421,7 @@ def test_to_dataframe_concat_categorical_dtype_w_pyarrow(self):
)

# Don't close the client if it was passed in.
bqstorage_client._transport.grpc_channel.close.assert_not_called()
bqstorage_client._transport.close.assert_not_called()

def test_to_dataframe_geography_as_object(self):
pandas = pytest.importorskip("pandas")
Expand Down
Loading