Skip to content
Merged
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
36 changes: 19 additions & 17 deletions packages/bigframes/bigframes/core/logging/log_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ def wrapper(*args, **kwargs):
base_name = custom_base_name

full_method_name = f"{base_name.lower()}-{api_method_name}"
# Track directly called methods
if len(_call_stack) == 0:
session = _find_session(*args, **kwargs)
add_api_method(full_method_name, session=session)

_call_stack.append(full_method_name)

try:
# Track directly called methods
if len(_call_stack) == 1:
session = _find_session(*args, **kwargs)
add_api_method(full_method_name, session=session)

return method(*args, **kwargs)
except (NotImplementedError, TypeError) as e:
# Log method parameters that are implemented in pandas but either missing (TypeError)
Expand Down Expand Up @@ -220,12 +220,12 @@ def wrapped(*args, **kwargs):
property_name = prop.__name__
full_property_name = f"{class_name.lower()}-{property_name.lower()}"

if len(_call_stack) == 0:
session = _find_session(*args, **kwargs)
add_api_method(full_property_name, session=session)

_call_stack.append(full_property_name)
try:
if len(_call_stack) == 1:
session = _find_session(*args, **kwargs)
add_api_method(full_property_name, session=session)

return prop(*args, **kwargs)
finally:
_call_stack.pop()
Expand Down Expand Up @@ -309,21 +309,23 @@ def _is_session_initialized(session):
Because the method logger could get called before Session.__init__ has a
chance to run, we use the globals in that case.
"""
return hasattr(session, "_api_methods_lock") and hasattr(session, "_api_methods")
return hasattr(session, "_api_methods_lock") and isinstance(
getattr(session, "_api_methods", None), list
)


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

session = args[0] if args else None
if (
session is not None
and isinstance(session, Session)
and _is_session_initialized(session)
):
return session
for arg in args:
if isinstance(arg, Session) and _is_session_initialized(arg):
return arg
if hasattr(arg, "__dict__") and "_block" in arg.__dict__:
session = getattr(arg, "_session", None)
if isinstance(session, Session) and _is_session_initialized(session):
return session

Comment on lines +322 to +329

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be place this before the kwargs check to preserve the original checking sequence?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Reordered _find_session to check args before kwargs, preserving the original sequence.

session = kwargs.get("session")
if (
Expand Down
2 changes: 1 addition & 1 deletion packages/bigframes/bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ def __getattr__(self, key: str):
# https://github.com/googleapis/python-bigquery-dataframes/issues/728
# and
# https://nedbatchelder.com/blog/201010/surprising_getattr_recursion.html
if key == "_block":
if "_block" not in self.__dict__ or key == "_block":
raise AttributeError(key)

if key in self._block.column_labels:
Expand Down
2 changes: 1 addition & 1 deletion packages/bigframes/bigframes/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,7 @@ def __getattr__(self, key: str):
# https://github.com/googleapis/python-bigquery-dataframes/issues/728
# and
# https://nedbatchelder.com/blog/201010/surprising_getattr_recursion.html
if key == "_block":
if "_block" not in self.__dict__ or key == "_block":
raise AttributeError(key)
elif hasattr(pandas.Series, key):
log_adapter.submit_pandas_labels(
Expand Down
Loading