Skip to content

Commit 7d68d53

Browse files
committed
chore: clarify that hasattr is needed due to __init__ not running yet
1 parent cb8e5ae commit 7d68d53

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

bigframes/core/log_adapter.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,26 +302,34 @@ def _get_bq_client(*args, **kwargs):
302302
return None
303303

304304

305+
def _is_session_initialized(session):
306+
"""Return True if fully initialized.
307+
308+
Because the method logger could get called before Session.__init__ has a
309+
chance to run, we use the globals in that case.
310+
"""
311+
return hasattr(session, "_api_methods_lock") and hasattr(session, "_api_methods")
312+
313+
305314
def _find_session(*args, **kwargs):
306315
# This function cannot import Session at the top level because Session
307316
# imports log_adapter.
308-
# We can't import bigframes.session in type checking block either.
309317
from bigframes.session import Session
310318

311-
if args and isinstance(args[0], Session):
312-
# In unit tests, we might be working with a mock Session object that
313-
# passes isinstance but doesn't have the instance attributes set in
314-
# __init__.
315-
session = args[0]
316-
if hasattr(session, "_api_methods_lock") and hasattr(session, "_api_methods"):
317-
return session
319+
session = args[0] if args else None
320+
if (
321+
session is not None
322+
and isinstance(session, Session)
323+
and _is_session_initialized(session)
324+
):
325+
return session
318326

319327
session = kwargs.get("session")
320-
if session is not None and isinstance(session, Session):
321-
# In unit tests, we might be working with a mock Session object that
322-
# passes isinstance but doesn't have the instance attributes set in
323-
# __init__.
324-
if hasattr(session, "_api_methods_lock") and hasattr(session, "_api_methods"):
325-
return session
328+
if (
329+
session is not None
330+
and isinstance(session, Session)
331+
and _is_session_initialized(session)
332+
):
333+
return session
326334

327335
return None

0 commit comments

Comments
 (0)