diff --git a/src/zeal/listeners.py b/src/zeal/listeners.py index 5e8f44f..19e06bb 100644 --- a/src/zeal/listeners.py +++ b/src/zeal/listeners.py @@ -123,7 +123,8 @@ def _alert( if is_allowlisted: return - final_caller = get_caller() + stack = get_stack() + final_caller = get_caller(stack) if should_include_all_callers: message = f"{message} with calls:\n" for i, caller in enumerate(calls): @@ -157,9 +158,10 @@ def notify( context = _nplusone_context.get() if not context.enabled: return - caller = get_caller() + stack = get_stack() + caller = get_caller(stack) key = (model, field, f"{caller.filename}:{caller.lineno}") - context.calls[key].append(get_stack()) + context.calls[key].append(stack) count = len(context.calls[key]) if count >= self._threshold and instance_key not in context.ignored: message = f"N+1 detected on {model._meta.app_label}.{model.__name__}.{field}" diff --git a/src/zeal/util.py b/src/zeal/util.py index f9dfa5c..c80e6f5 100644 --- a/src/zeal/util.py +++ b/src/zeal/util.py @@ -21,12 +21,12 @@ def get_stack() -> list[inspect.FrameInfo]: ] -def get_caller() -> inspect.FrameInfo: +def get_caller(stack: list[inspect.FrameInfo]) -> inspect.FrameInfo: """ Returns the filename and line number of the current caller, excluding any code in site-packages or zeal. """ - return next(frame for frame in get_stack()) + return next(frame for frame in stack) def is_single_query(query: Query):