Skip to content

Commit

Permalink
Merge pull request #58 from nebuly-ai/feat/avoid-tracking
Browse files Browse the repository at this point in the history
Avoid tracking unnecessary calls
  • Loading branch information
diegofiori authored Feb 29, 2024
2 parents 0635bb2 + 9524b38 commit 2ed9c9d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 84 deletions.
2 changes: 1 addition & 1 deletion nebuly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .init import init

__all__ = ["init", "new_interaction"]
__version__ = "0.3.21"
__version__ = "0.3.22"
13 changes: 13 additions & 0 deletions nebuly/monkey_patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,14 @@ def _is_generator(obj: Any, module: str) -> bool:
return False


def _is_in_context_manager() -> bool:
try:
get_nearest_open_interaction()
return True
except NotInInteractionContext:
return False


def coroutine_wrapper(
f: Callable[[Any], Any],
observer: Observer,
Expand Down Expand Up @@ -633,6 +641,8 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:
result = f(*args, **function_kwargs)
else:
result = await f(*args, **function_kwargs)
if len(nebuly_kwargs) == 0 and not _is_in_context_manager():
return result

if _is_generator(result, module):
logger.debug("Result is a generator")
Expand Down Expand Up @@ -701,6 +711,7 @@ def function_wrapper(
function_name: str,
) -> Callable[[Any], Any]:
@wraps(f)
# pylint: disable=too-many-return-statements
def wrapper(*args: Any, **kwargs: Any) -> Any:
result = None
try:
Expand Down Expand Up @@ -735,6 +746,8 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:

called_start = datetime.now(timezone.utc)
result = f(*args, **function_kwargs)
if len(nebuly_kwargs) == 0 and not _is_in_context_manager():
return result

if _is_generator(result, module):
logger.debug("Result is a generator")
Expand Down
Loading

0 comments on commit 2ed9c9d

Please sign in to comment.