Skip to content

Commit 1e9393b

Browse files
committed
fix(bigframes): fix session-scoped api logging and stabilize label trimming tests
1 parent 89d87b2 commit 1e9393b

2 files changed

Lines changed: 22 additions & 23 deletions

File tree

packages/bigframes/bigframes/core/logging/log_adapter.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,21 +309,25 @@ def _is_session_initialized(session):
309309
Because the method logger could get called before Session.__init__ has a
310310
chance to run, we use the globals in that case.
311311
"""
312-
return hasattr(session, "_api_methods_lock") and hasattr(session, "_api_methods")
312+
return hasattr(session, "_api_methods_lock") and isinstance(
313+
getattr(session, "_api_methods", None), list
314+
)
313315

314316

315317
def _find_session(*args, **kwargs):
316318
# This function cannot import Session at the top level because Session
317319
# imports log_adapter.
318320
from bigframes.session import Session
319321

320-
session = args[0] if args else None
321-
if (
322-
session is not None
323-
and isinstance(session, Session)
324-
and _is_session_initialized(session)
325-
):
326-
return session
322+
for arg in args:
323+
if isinstance(arg, Session) and _is_session_initialized(arg):
324+
return arg
325+
session = getattr(arg, "_session", None)
326+
if isinstance(session, Session) and _is_session_initialized(session):
327+
return session
328+
session = getattr(arg, "session", None)
329+
if isinstance(session, Session) and _is_session_initialized(session):
330+
return session
327331

328332
session = kwargs.get("session")
329333
if (

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ def test_create_job_configs_labels_length_limit_met():
151151

152152

153153
def test_add_and_trim_labels_length_limit_met():
154-
log_adapter.get_and_reset_api_methods()
154+
session = mocks.create_bigquery_session()
155+
log_adapter.get_and_reset_api_methods(session=session)
155156
cur_labels = {
156157
"bigframes-api": "read_pandas",
157158
"source": "bigquery-dataframes-temp",
@@ -161,18 +162,14 @@ def test_add_and_trim_labels_length_limit_met():
161162
value = f"test{i}"
162163
cur_labels[key] = value
163164

164-
df = bpd.DataFrame(
165-
{"col1": [1, 2], "col2": [3, 4]}, session=mocks.create_bigquery_session()
166-
)
167-
168165
job_config = google.cloud.bigquery.job.QueryJobConfig()
169166
job_config.labels = cur_labels
170167

171-
df.max()
168+
log_adapter.add_api_method("dataframe-max", session=session)
172169
for _ in range(52):
173-
df.head()
170+
log_adapter.add_api_method("dataframe-head", session=session)
174171

175-
io_bq.add_and_trim_labels(job_config=job_config, session=df._session)
172+
io_bq.add_and_trim_labels(job_config=job_config, session=session)
176173
assert job_config.labels is not None
177174
assert len(job_config.labels) == 56
178175
assert "dataframe-max" not in job_config.labels.values()
@@ -189,6 +186,8 @@ def test_start_query_with_job_labels_length_limit_met(
189186
mock_bq_client: bigquery.Client, timeout: Optional[float], api_name
190187
):
191188
sql = "select * from abc"
189+
session = mocks.create_bigquery_session()
190+
log_adapter.get_and_reset_api_methods(session=session)
192191
cur_labels = {
193192
"bigframes-api": "read_pandas",
194193
"source": "bigquery-dataframes-temp",
@@ -198,16 +197,12 @@ def test_start_query_with_job_labels_length_limit_met(
198197
value = f"test{i}"
199198
cur_labels[key] = value
200199

201-
df = bpd.DataFrame(
202-
{"col1": [1, 2], "col2": [3, 4]}, session=mocks.create_bigquery_session()
203-
)
204-
205200
job_config = google.cloud.bigquery.job.QueryJobConfig()
206201
job_config.labels = cur_labels
207202

208-
df.max()
203+
log_adapter.add_api_method("dataframe-max", session=session)
209204
for _ in range(52):
210-
df.head()
205+
log_adapter.add_api_method("dataframe-head", session=session)
211206

212207
io_bq.start_query_with_job(
213208
mock_bq_client,
@@ -218,7 +213,7 @@ def test_start_query_with_job_labels_length_limit_met(
218213
timeout=timeout,
219214
metrics=None,
220215
publisher=bigframes.core.events.Publisher(),
221-
session=df._session,
216+
session=session,
222217
)
223218

224219
assert job_config.labels is not None

0 commit comments

Comments
 (0)