You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The documented pattern for parametrizing conditional raising leaks a lot of memory #13410
The problem is that the pytest.raises context manager is mutated with a reference to the raised exception and traceback when exiting the context manager:
exc_info=cast(Tuple[Type[E], E, TracebackType], (exc_type, exc_val, exc_tb))
self.excinfo.fill_unfilled(exc_info)
This causes the traceback to be kept in memory until the test session terminates.
If this is indeed the correct way for this kind of parametrization, pytest should do some automatic clean up after the test finishes.
Otherwise, the documented parametrization should be adjusted to not pass in mutable objects, for example:
@pytest.mark.parametrize("example_input,expectation", [ (3, lambda: nullcontext(2)), (2, lambda: nullcontext(3)), (1, lambda: nullcontext(6)), (0, lambda: pytest.raises(ZeroDivisionError)), ],)deftest_division(example_input, expectation):
"""Test how much I know division."""withexpectation() ase:
assert (6/example_input) ==e
The same problem happens if the parametrization involves exception objects which are raised during the test, the objects are mutated by Python which adds a __traceback__ to them:
Hi, I’d like to work on this. My understanding is that the parametrized example keeps the pytest.raises() context manager alive after the test, so it can retain the exception traceback and the test’s local variables.
I plan to verify that on main, then try updating the example to create a fresh context manager inside each test invocation. I’ll check that all four cases still pass and that the traceback is no longer retained through the parameter.
Drafted with assistance from OpenAI Codex (GPT‑6).
Activity
nightcityblade commentedon Aug 10, 2026
nightcityblade commentedon Aug 10, 2026
hariomkhonde108 commentedon Nov 28, 2026
Hi, I’d like to work on this. My understanding is that the parametrized example keeps the
pytest.raises()context manager alive after the test, so it can retain the exception traceback and the test’s local variables.I plan to verify that on
main, then try updating the example to create a fresh context manager inside each test invocation. I’ll check that all four cases still pass and that the traceback is no longer retained through the parameter.Drafted with assistance from OpenAI Codex (GPT‑6).