When using the .limit_leaks marker, you can provide a filter_fn to suppress reports for known leaks (e.g., objects cached by the code under test).
To create a filter, implement a callable that accepts a Stack object and returns a bool.
- Return
True to report the leak. - Return
False to suppress the leak.
The Stack object contains a tuple of StackFrame objects, which include function, filename, and lineno.
from typing import Protocol
from src.pytest_memray.marks import Stack
class MyLeakFilter:
def __call__(self, stack: Stack) -> bool:
# Suppress leaks if they originate from a specific function
for frame in stack.frames:
if frame.function == "known_leaky_function":
return False
return True
# Usage in a test (assuming the marker is available via pytest-memray)
# @pytest.mark.limit_leaks("10MB", filter_fn=MyLeakFilter())
# def test_something():
# ...