Skip to content

Commit d0b590e

Browse files
committed
fix: preserve read_gbq_colab label in anywidget mode
1 parent eceea95 commit d0b590e

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

packages/bigframes/bigframes/session/_io/bigquery/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,14 @@ def create_job_configs_labels(
6868
job_configs_labels = dict(job_configs_labels)
6969

7070
if api_methods and "bigframes-api" not in job_configs_labels:
71-
job_configs_labels["bigframes-api"] = api_methods[0]
72-
del api_methods[0]
71+
colab_idx = next(
72+
(i for i, m in enumerate(api_methods) if "read_gbq_colab" in m), None
73+
)
74+
if colab_idx is not None:
75+
job_configs_labels["bigframes-api"] = api_methods.pop(colab_idx)
76+
else:
77+
job_configs_labels["bigframes-api"] = api_methods[0]
78+
del api_methods[0]
7379

7480
# Make sure we always populate bigframes-api with _something_, even if we
7581
# have a code path which doesn't populate the list of api_methods. See

packages/bigframes/tests/unit/session/test_read_gbq_colab.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@ def test_read_gbq_colab_includes_label():
4747
assert "session-read_gbq_colab" in label_values
4848

4949

50+
def test_read_gbq_colab_includes_label_in_anywidget_mode():
51+
"""Make sure read_gbq_colab label is preserved as the primary bigframes-api label in anywidget mode."""
52+
import bigframes
53+
import bigframes.display.html as bf_html
54+
55+
bqclient = mock.create_autospec(bigquery.Client, instance=True)
56+
bqclient.project = "proj"
57+
session = mocks.create_bigquery_session(bqclient=bqclient)
58+
df = session._read_gbq_colab("SELECT 'read-gbq-colab-test'")
59+
60+
with bigframes.option_context("display.render_mode", "anywidget"):
61+
_ = bf_html.get_anywidget_bundle(df)
62+
63+
label_values = []
64+
bigframes_api_labels = []
65+
for kall in itertools.chain(
66+
bqclient.query_and_wait.call_args_list,
67+
bqclient._query_and_wait_bigframes.call_args_list,
68+
bqclient.query.call_args_list,
69+
):
70+
job_config = kall.kwargs.get("job_config")
71+
if job_config is None:
72+
continue
73+
label_values.extend(job_config.labels.values())
74+
if "bigframes-api" in job_config.labels:
75+
bigframes_api_labels.append(job_config.labels["bigframes-api"])
76+
77+
assert "session-read_gbq_colab" in label_values
78+
assert "session-read_gbq_colab" in bigframes_api_labels
79+
80+
5081
@pytest.mark.parametrize("dry_run", [True, False])
5182
def test_read_gbq_colab_includes_formatted_values_in_dry_run(monkeypatch, dry_run):
5283
bqclient = mock.create_autospec(bigquery.Client, instance=True)

0 commit comments

Comments
 (0)